【问题标题】:Cant connect jupyterhub to keycloak and getting ERR_TOO_MANY_REDIRECTS无法将 jupyterhub 连接到 keycloak 并获取 ERR_TOO_MANY_REDIRECTS
【发布时间】:2021-02-06 07:45:37
【问题描述】:

我正在尝试在 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

【问题讨论】:

    标签: python oauth-2.0 vagrant keycloak jupyterhub


    【解决方案1】:

    TL;DR:将 OAUTH2_AUTHORIZE_URLOAUTH2_TOKEN_URL 环境变量设置为您可以在您的身份提供者(即 KeyCloak)中找到的相应 URL。

    我对这个有点摸不着头脑......正如您在自己的日志中看到的那样,JupyterHub 不知何故认为它是负责验证用户身份的人它也说也就是说,在身份验证之后,用户应该被重定向回自己(这就是oauth_callback_url 所说的)。这会导致 JupyterHub 服务器以递归方式调用自身,直到浏览器放弃。

    我假设您在使用 vagrant 配置的同一 VM 上同时运行 keyCloakJupyterHub。如果是这种情况,请考虑将0.0.0.0 位更改为127.0.0.1。否则,尝试将0.0.0.0 更改为c.GenericOAuthenticator.userdata_urlc.GenericOAuthenticator.token_url 可以访问KeyCloak 的IPv4 地址。当事情没有按应有的方式工作时,通常最好避免使用通配符等来缩小问题所在...

    c.GenericOAuthenticator.oauth_callback_url 应包含 JupyterHub 实例所在的地址,除此之外,您应该将其指向 JupyterHub 实例。就我而言,那将是<jupyterhub-ip>:<jupyterhub-port>/hub/oauth_callback。请记住,该成员告诉 KeyCloak 在身份验证流程中它应该在哪里联系 JupyterHub。

    无论如何,我们要做的唯一事情是将 JupyterHub 指向 KeyCloak 实例。如果您查看 JupyterHub 的 OAuthenticator 中的 this snippet,您会发现 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 issue

    确保这些变量对 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,如果这是您的情况...

    现在要结束了,关键思想是设置适当的环境变量,以便事情按应有的方式运行......

    P.D:我可能会在 @default 装饰器上遗漏一些东西,但我宁愿设置一个环境变量,也不愿使用 python 解决这类问题......

    如果您还有任何问题,请随时开火!

    Source.

    【讨论】:

      猜你喜欢
      • 2019-10-25
      • 2018-12-15
      • 2018-12-24
      • 2021-06-21
      • 2018-12-22
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 2022-11-11
      相关资源
      最近更新 更多