簡體   English   中英

無法將 jupyterhub 連接到 keycloak 並獲得 ERR_TOO_MANY_REDIRECTS

[英]Cant connect jupyterhub to keycloak and getting ERR_TOO_MANY_REDIRECTS

我正在嘗試在 vagrant 上設置 jupyterhub,但是在我將(我認為)身份驗證連接到 keycloak 之后,單擊 oauth2 按鈕后,我的瀏覽器中出現 ERR_TOO_MANY_REDIRECTS 錯誤。 是我的 jupyterhub_config.py 還是錯誤的 oauth_callback_url 有問題?

我的密鑰斗篷初始化:

cd $KEYCLOAK_PATH/bin && sudo bash add-user-keycloak.sh -u admin -p admin
cd $KEYCLOAK_PATH/bin && sudo nohup ./standalone.sh -Djboss.bind.address.management=0.0.0.0 -Djboss.socket.binding.port-offset=100 -Djboss.bind.address=0.0.0.0 > $KEYCLOAK_LOGS/keycloak.log 2>&1 &

我的 jupyterhub_config.py:

c.Authenticator.admin_users = {'vagrant'}
from oauthenticator.generic import GenericOAuthenticator
c.JupyterHub.authenticator_class = GenericOAuthenticator
c.GenericOAuthenticator.oauth_callback_url = 'http://0.0.0.0:8180/auth/realms/testrealm/protocol/openid-connect/auth'
c.GenericOAuthenticator.client_id = 'oauth-secret'
c.GenericOAuthenticator.client_secret = ''
c.GenericOAuthenticator.token_url = 'http://0.0.0.0:8180/auth/realms/testrealm/protocol/openid-connect/token'
c.GenericOAuthenticator.userdata_url = 'http://0.0.0.0:8180/auth/realms/testrealm/protocol/openid-connect/userinfo'

從 Vagrantfile 轉發的端口:

config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.network "forwarded_port", guest: 8001, host: 8001
config.vm.network "forwarded_port", guest: 8081, host: 8081

一些重定向:

[I 2020-10-23 08:25:51.287 JupyterHub oauth2:103] OAuth redirect: 'http://0.0.0.0:8180/auth/realms/testrealm/protocol/openid-connect/auth'
[I 2020-10-23 08:25:51.290 JupyterHub log:174] 302 GET /hub/oauth_login?response_type=code&redirect_uri=http%3A%2F%2F0.0.0.0%3A8180%2Fauth%2Frealms%2Ftestrealm%2Fprotocol%2Fopenid-connect%2Fauth&client_id=oauth-secret&state=[secret] -> ?response_type=code&redirect_uri=http%3A%2F%2F0.0.0.0%3A8180%2Fauth%2Frealms%2Ftestrealm%2Fprotocol%2Fopenid-connect%2Fauth&client_id=oauth-secret&state=[secret] (@10.0.2.2) 3.66ms
[I 2020-10-23 08:25:51.307 JupyterHub oauth2:103] OAuth redirect: 'http://0.0.0.0:8180/auth/realms/testrealm/protocol/openid-connect/auth'
[I 2020-10-23 08:25:51.311 JupyterHub log:174] 302 GET /hub/oauth_login?response_type=code&redirect_uri=http%3A%2F%2F0.0.0.0%3A8180%2Fauth%2Frealms%2Ftestrealm%2Fprotocol%2Fopenid-connect%2Fauth&client_id=oauth-secret&state=[secret] -> ?response_type=code&redirect_uri=http%3A%2F%2F0.0.0.0%3A8180%2Fauth%2Frealms%2Ftestrealm%2Fprotocol%2Fopenid-connect%2Fauth&client_id=oauth-secret&state=[secret] (@10.0.2.2) 4.66ms
[I 2020-10-23 08:25:51.326 JupyterHub oauth2:103] OAuth redirect: 'http://0.0.0.0:8180/auth/realms/testrealm/protocol/openid-connect/auth'
[I 2020-10-23 08:25:51.329 JupyterHub log:174] 302 GET /hub/oauth_login?response_type=code&redirect_uri=http%3A%2F%2F0.0.0.0%3A8180%2Fauth%2Frealms%2Ftestrealm%2Fprotocol%2Fopenid-connect%2Fauth&client_id=oauth-secret&state=[secret] -> ?response_type=code&redirect_uri=http%3A%2F%2F0.0.0.0%3A8180%2Fauth%2Frealms%2Ftestrealm%2Fprotocol%2Fopenid-connect%2Fauth&client_id=oauth-secret&state=[secret] (@10.0.2.2) 4.80ms

TL;DR :將OAUTH2_AUTHORIZE_URLOAUTH2_TOKEN_URL環境變量設置為您可以在身份提供者(即 KeyCloak)中找到的適當 URL。

我一直在用這個摸不着頭腦......正如你在你自己的日志中看到的那樣,JupyterHub 以某種方式相信它是負責對用戶進行身份驗證的人,它也說,在身份驗證后,用戶應該被重定向回到自身(這就是oauth_callback_url所說的)。 這導致 JupyterHub 服務器以遞歸方式調用自己,直到瀏覽器放棄。

我假設你是在同一個虛擬機,這與你配置的流浪漢同時運行keyCloakJupyterHub。 如果是這種情況,請考慮將0.0.0.0位更改為127.0.0.1 否則,嘗試將0.0.0.0更改為可以為c.GenericOAuthenticator.userdata_urlc.GenericOAuthenticator.token_url的 IPv4 地址。 當事情不能正常工作時,避免使用通配符等來縮小問題所在通常是一個好主意......

JupyterHub 實例所在的地址應該包含在c.GenericOAuthenticator.oauth_callback_url ,最重要的是,您應該將其指向 JupyterHub 實例。 就我而言,這將是<jupyterhub-ip>:<jupyterhub-port>/hub/oauth_callback 請記住,該成員告訴 KeyCloak 在身份驗證流程中它應該在哪里聯系 JupyterHub。

無論如何,我們唯一要做的就是將 JupyterHub 指向 KeyCloak 實例。 如果您查看 JupyterHub 的 OAuthenticator 中的這個片段,您會發現authorize_url的默認值是從名為OAUTH2_AUTHORIZE_URL的環境變量中提取的。 我對@default裝飾器的作用不是很自信,但我現在做的是在配置文件中手動設置authorize_url的值對我不起作用......

您只需要導出上述環境值,這應該就結束了。 你可以這樣做:

export OAUTH2_AUTHORIZE_URL="https://<keycloak-ip>:<keycloak-port>auth/realms/testrealm/protocol/openid-connect/auth"

我還以幾乎相同的方式通過OAUTH2_TOKEN_URL變量導出了令牌 url,其值與您在配置中的值相同(一旦您更改了0.0.0.0 IP 地址)。 幫助我的GitHub 問題

確保這些變量對 JupyterHub 進程可見。 我正在運行The Littlest JupyterHub ,我必須將它們包含在適當的單元文件( /etc/systemd/system/jupyterhub.service )中,如下所示:

Environment=OAUTH2_AUTHORIZE_URL=https://<keycloak-ip>:<keycloak-port>/auth/realms/<realm-name>/protocol/openid-connect/auth
Environment=OAUTH2_TOKEN_URL=https://<keycloak-ip>:<keycloak-port>/auth/realms/<realm-name>/protocol/openid-connect/token

如果這是您的情況,請記住運行systemctl daemon-reloadsystemctl restart jupyterlab ...

現在結束,關鍵思想是設置適當的環境變量,以便事情按應有的方式運行......

PD:我可能在@default裝飾器中遺漏了一些東西,但我寧願設置一個環境變量,也不願用python 解決這類問題......

如果您有任何其他問題,請隨時開火!

來源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM