您好,登錄后才能下訂單哦!
這篇文章主要介紹“SonarQube中IdentityProvider賬戶互斥現(xiàn)象實(shí)例分析”的相關(guān)知識,小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“SonarQube中IdentityProvider賬戶互斥現(xiàn)象實(shí)例分析”文章能幫助大家解決問題。
soanr 是一個代碼質(zhì)量管理系統(tǒng),代碼是開源的。在公司統(tǒng)一認(rèn)證平臺還沒出來時,sonar 已接入 ldap 提供系統(tǒng)登錄認(rèn)證功能,現(xiàn)在使用 sonar-auth-oidc 插件以 OIDC 協(xié)議接入集中認(rèn)證平臺時,發(fā)現(xiàn)用戶的賬戶是互斥的(如果現(xiàn)有用戶之前采用 ldap 登錄,使用 oidc 登錄后會創(chuàng)建一個新的用戶,沒法關(guān)聯(lián)之前的賬戶),即使用戶的所有信息一致也是如此。
以 sonar-auth-oidc 為例,實(shí)現(xiàn)一個 soanr 的插件,需要如下步驟:
soanr 將可以擴(kuò)展的通用的功能抽象定義放到了 sonar-plugin-api 模塊,實(shí)現(xiàn)插件首先需要依賴這個模塊,然后需要實(shí)現(xiàn)什么功能,找到對應(yīng)的接口定義,以 sonar-auth-oidc 為例,需要實(shí)現(xiàn) OAuth3IdentityProvider 接口。
@ServerSide public class OidcIdentityProvider implements OAuth3IdentityProvider { private static final Logger LOGGER = Loggers.get(OidcIdentityProvider.class); public static final String KEY = "oidc"; private final OidcConfiguration config; private final OidcClient client; private final UserIdentityFactory userIdentityFactory; public OidcIdentityProvider(OidcConfiguration config, OidcClient client, UserIdentityFactory userIdentityFactory) { this.config = config; this.client = client; this.userIdentityFactory = userIdentityFactory; } //省略非關(guān)鍵邏輯 @Override public void init(InitContext context) { LOGGER.trace("Starting authentication workflow"); if (!isEnabled()) { throw new IllegalStateException("OpenID Connect authentication is disabled"); } String state = context.generateCsrfState(); AuthenticationRequest authenticationRequest = client.getAuthenticationRequest(context.getCallbackUrl(), state); LOGGER.trace("Redirecting to authentication endpoint"); context.redirectTo(authenticationRequest.toURI().toString()); } @Override public void callback(CallbackContext context) { LOGGER.trace("Handling authentication response"); context.verifyCsrfState(); AuthorizationCode authorizationCode = client.getAuthorizationCode(context.getRequest()); UserInfo userInfo = client.getUserInfo(authorizationCode, context.getCallbackUrl()); UserIdentity userIdentity = userIdentityFactory.create(userInfo); LOGGER.debug("Authenticating user '{}' with groups {}", userIdentity.getProviderLogin(), userIdentity.getGroups()); context.authenticate(userIdentity); LOGGER.trace("Redirecting to requested page"); context.redirectToRequestedPage(); } }
public class AuthOidcPlugin implements Plugin { @Override public void define(Context context) { context.addExtensions(OidcConfiguration.class, OidcClient.class, OidcIdentityProvider.class, UserIdentityFactory.class); context.addExtensions(OidcConfiguration.definitions()); } }
<plugin> <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId> <artifactId>sonar-packaging-maven-plugin</artifactId> <version>1.18.0.372</version> <extensions>true</extensions> <configuration> <pluginClass>org.vaulttec.sonarqube.auth.oidc.AuthOidcPlugin</pluginClass> </configuration> </plugin>
插件完成后,最終會打成 jar 包,在應(yīng)用市場安裝插件,相當(dāng)于下載了一個 jar 放到了 soanrqube 安裝目錄的 ./extensions/plugins 路徑下。上面的打包工具會給 jar 包清單 META-INF/MANIFEST.MF 里加上
Plugin-Class: org.vaulttec.sonarqube.auth.oidc.AuthOidcPlugin 。
soanrqube 啟動時,插件掃描器會掃到這個 jar ,解析拿到 plugin-class 并加載插件。
從sonar 插件實(shí)現(xiàn)分析了解到,sonar-auth-oidc 實(shí)現(xiàn) Oauth 認(rèn)證的關(guān)鍵邏輯在 OidcIdentityProvider 類里,關(guān)鍵代碼如下:
集成 oidc 插件后,訪問 soanr 會顯示如下登錄入口
該登錄按鈕的鏈接為:http://172.26.202.128:9000/sessions/init/oidc?return_to=%2F ,點(diǎn)擊這個鏈接,就會觸發(fā)插件里 init 方法,init 方法里拼裝了 oidc 授權(quán)服務(wù)器的接口地址并重定向到這地址,等待用戶授權(quán)
用戶在授權(quán)服務(wù)器授權(quán)頁面授權(quán)完成后,授權(quán)服務(wù)器會回調(diào) soanr 服務(wù),最終觸發(fā) callback 函數(shù)。在 callback 函數(shù)里,首先從 request 里拿到了 authorizationCode ,然后使用 code 請求 oidc 授權(quán)服務(wù)器,拿到了 UserInfo,使用 UserInfo 里的信息,組裝了 soanr 內(nèi)置的認(rèn)證類 UserIdentity 。然后調(diào)用了 authenticate 完成認(rèn)證,如果認(rèn)證成功,沒有異常,則重定向到了登錄成功的頁面??吹竭@里,最關(guān)鍵的地方是 UserIdentity ,前面一大堆邏輯,最終就為了組裝出這個實(shí)體,這個類定義如下:
sonar-auth-oidc 插件里有用的信息,就這些了,關(guān)鍵的是 UserIdentity ,接下來需要從 sonarqube 里找到 CallBackContext 的實(shí)現(xiàn)類,看 authenticate() 方法里是怎么處理 UserIdentity 的。
soanr 的web 登錄認(rèn)證功能在 sonar-webserver-auth 模塊,上文在 oidc 插件里出現(xiàn)的 InitContext 、 CallBackContxt 均在 OAuth3ContextFactory 類里被初始化,關(guān)鍵邏輯如下:
1、執(zhí)行用戶注冊邏輯(內(nèi)部控制了是注冊還是更新)
2、生成登錄用的 jwt 串
3、設(shè)置本地用戶登錄的 session
1、通過 externalId 和 IdentityProvider 查詢用戶,如果為空,則通過 externalLogin 和 IdentityProvider 查詢用戶
2、如果 user 不存在或者已禁用,則創(chuàng)建一個新的用戶,否則注冊一個已存在的用戶(更新用戶信息)
關(guān)鍵點(diǎn)開始凸顯出來了,關(guān)鍵邏輯在這里,通過 externalId 和 IdentityProvider、externalLogin 等條件沒有查詢到用戶信息,所以才創(chuàng)建了新的用戶,接下來看下數(shù)據(jù)庫里 User 存儲了哪些信息。
本地 sonar 嘗試對接過 ldap 、gitlab 、oidc ,所以一個賬戶有三條用戶數(shù)據(jù),其中 ldap 賬戶的 identityProvider 被標(biāo)記為 sonarqube 了,而使用 OIDC 登錄被標(biāo)記為 oidc(插件的 key),所以才導(dǎo)致通過 externalId 和 IdentityProvider、externalLogin 等信息都查詢不到用戶信息
經(jīng)過分析后,知道了 oidc 和 ldap 賬戶互斥的根本原因?yàn)椋煌?identityProvider 的用戶會被加上 provider_key 區(qū)分,導(dǎo)致同一個用戶,即使用戶信息一樣,從不同的 identityProvider 認(rèn)證也會生成不同的用戶。所以,可以從如下兩個方案出發(fā),解決問題
方案一:重寫插件,將 identityProvider 標(biāo)識和 LDAP 的同步。
優(yōu)點(diǎn):兼容 LDAP 和 identityProvider 賬戶,可以設(shè)計(jì)下兼容所以的 identityProvider
缺點(diǎn):實(shí)施比較困難,需要自己維護(hù)插件或者尋求官方倉庫合并 pr
方案二:修改數(shù)據(jù)庫中存量 LDAP 用戶的 identityProvider 的標(biāo)識為 oidc。
優(yōu)點(diǎn):改動較簡單
缺點(diǎn):LDAP 和 OIDC 不兼容,集成 OIDC 后,只能被動的關(guān)閉 LDAP,而原 LDAP 的輸入框和本地用戶密碼框是一起的,沒法移除,對用戶使用習(xí)慣會有影響
關(guān)于“SonarQube中IdentityProvider賬戶互斥現(xiàn)象實(shí)例分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。