[英]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_URL
和OAUTH2_TOKEN_URL
环境变量设置为您可以在身份提供者(即 KeyCloak)中找到的适当 URL。
我一直在用这个摸不着头脑......正如你在你自己的日志中看到的那样,JupyterHub 以某种方式相信它是负责对用户进行身份验证的人,但它也说,在身份验证后,用户应该被重定向回到自身(这就是oauth_callback_url
所说的)。 这导致 JupyterHub 服务器以递归方式调用自己,直到浏览器放弃。
我假设你是在同一个虚拟机,这与你配置的流浪汉同时运行keyCloak和JupyterHub。 如果是这种情况,请考虑将0.0.0.0
位更改为127.0.0.1
。 否则,尝试将0.0.0.0
更改为可以为c.GenericOAuthenticator.userdata_url
和c.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-reload
和systemctl restart jupyterlab
...
现在结束,关键思想是设置适当的环境变量,以便事情按应有的方式运行......
PD:我可能在@default
装饰器中遗漏了一些东西,但我宁愿设置一个环境变量,也不愿用python 解决这类问题......
如果您有任何其他问题,请随时开火!
来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.