配置 Keycloak (OIDC)
如果你的组织使用 Keycloak Identity Provider (IdP) 进行用户身份验证,你可以通过配置 Rancher 来允许用户使用 IdP 凭证登录。Rancher 支持使用 OpenID Connect (OIDC) 协议和 SAML 协议来集成 Keycloak。与 Rancher 一起使用时,这两种实现在功能上是等效的。本文描述了配置 Rancher 以通过 OIDC 协议与 Keycloak 一起使用的流程。
如果你更喜欢将 Keycloak 与 SAML 协议一起使用,请参见此页面。
如果你有使用 SAML 协议的现有配置并希望切换到 OIDC 协议,请参见本节。
先决条件
-
已在 Rancher 上禁用 Keycloak (SAML)。
-
你必须配置了 Keycloak IdP 服务器。
-
Follow the Keycloak documentation to create a new OIDC client with the settings below.。
设置 值 Client ID<client-id> (例如
rancher)Name<client-name> (例如
rancher)Client typeOpenID ConnectClient authenticationONValid Redirect URIhttps://yourRancherHostURL/verify-auth -
在新的 OIDC 客户端中,创建 Mappers 来公开用户字段。
-
In the navigation menu, click Clients.
-
Click the Clients list tab.
-
Find and click the client you created.
-
Click the Client scopes tab.
-
Find and click the link labeled
<client-name>-dedicated. For example, if you named your clientrancher, look for the link namedrancher-dedicated. -
Click the Mappers tab.
-
Click Configure a new mapper. If you already have existing mappers configured, click the arrow next to Add mapper and select By configuration. Repeat this process and create these mappers:
-
From the mappings table, select Group Membership and configure a new "Groups Mapper" with the settings below. For settings that are not mentioned, use the default value.
Setting Value NameGroups MapperMapper TypeGroup MembershipToken Claim NamegroupsFull group pathOFFAdd to ID tokenOFFAdd to access tokenOFFAdd to user infoON -
From the mappings table, select Audience and configure a new "Client Audience" with the settings below. For settings that are not mentioned, use the default value.
Setting Value NameClient AudienceMapper TypeAudienceIncluded Client Audience<client-name>
Add to ID tokenOFFAdd to access tokenON -
From the mappings table, select Group Membership and configure a new "Groups Path" with the settings below. For settings that are not mentioned, use the default value.
Setting Value NameGroup PathMapper TypeGroup MembershipToken Claim Namefull_group_pathFull group pathONAdd to ID tokenONAdd to access tokenONAdd to user infoON
-
-
-
Add the following role mappings to all users or groups that need to query the Keycloak users.
-
Users
-
Groups
-
In the navigation menu, click Users.
-
Click the user you want to add role mappings to.
-
Click the Role mapping tab.
-
Click Assign role.
-
Select the following roles:
-
query-users
-
query-groups
-
view-users
-
-
Click Assign.
-
In the navigation menu, click Groups.
-
Click the group you want to add role mappings to.
-
Click the Role mapping tab.
-
Click Assign role.
-
Select the following roles:
-
query-users
-
query-groups
-
view-users
-
-
Click Assign.
-
在 Rancher 中配置 Keycloak
-
在 Rancher UI 中,单击 ☰ > 用户 & 认证。
-
单击左侧导航栏的认证。
-
选择 Keycloak (OIDC)
-
Select Keycloak (OIDC).。
-
填写配置 Keycloak OIDC 账号表单。有关填写表单的帮助,请参见配置参考。
When configuring the Endpoints section using the Generate option, Rancher includes
/authas part of the context path in the Issuer and Auth Endpoint fields, which is only valid for Keycloak 16 or older. You must configure endpoints using the Specify option for Keycloak 17 and newer, which have migrated to Quarkus. -
完成配置 Keycloak OIDC 账号表单后,单击启用。
Rancher 会将你重定向到 IdP 登录页面。输入使用 Keycloak IdP 进行身份验证的凭证,来验证你的 Rancher Keycloak 配置。
你可能需要禁用弹出窗口阻止程序才能看到 IdP 登录页面。
结果:已将 Rancher 配置为使用 OIDC 协议与 Keycloak 一起工作。你的用户现在可以使用 Keycloak 登录名登录 Rancher。
配置参考
| 字段 | 描述 |
|---|---|
客户端 ID |
你的 Keycloak 客户端的 |
客户端密码 |
你的 Keycloak 客户端生成的 |
私钥/证书 |
在 Rancher 和你的 IdP 之间创建安全外壳(SSH)的密钥/证书对。如果你的 Keycloak 服务器上启用了 HTTPS/SSL,则为必填。 |
端点 |
选择为 |
Keycloak URL |
你的 Keycloak 服务器的 URL。 |
Keycloak Realm |
创建 Keycloak 客户端的 Realm 的名称。 |
Rancher URL |
Rancher Server 的 URL。 |
Issuer |
你的 IdP 的 URL。 |
Auth 端点 |
重定向用户进行身份验证的 URL。 |
从 SAML 迁移到 OIDC
本节描述了将使用 Keycloak (SAML) 的 Rancher 过渡到 Keycloak (OIDC) 的过程。
-
Reconfigure Keycloak.
-
Before configuring Rancher to use Keycloak (OIDC), Keycloak (SAML) must be first disabled.
-
In the Rancher UI, click ☰ > Users & Authentication.
-
In the left navigation bar, click Auth Provider.
-
Select Keycloak (SAML).
-
Click Disable.
-
-
Follow the steps in 在 Rancher 中配置 Keycloak.
配置完成后,由于用户权限不会自动迁移,你需要重新申请 Rancher 用户权限。
附录:故障排除
如果你在测试与 Keycloak 服务器的连接时遇到问题,请先检查 OIDC 客户端的配置选项。你还可以检查 Rancher 日志来查明问题的原因。调试日志可能包含有关错误的更详细信息。详情请参见如何启用调试日志。
所有与 Keycloak 相关的日志条目都将添加 [generic oidc] 或 [keycloak oidc]。
生成的 Issuer 和 Auth 端点不正确
在配置 Keycloak OIDC 账号表单中,将端点更改为指定(高级设置)并覆盖发行者 和 Auth 端点的值。要查找这些值,前往 Keycloak 控制台并选择 Realm Settings,选择 General 选项卡,然后单击 OpenID Endpoint Configuration。JSON 输出将显示 issuer 和 authorization_endpoint 的值。
Unable to See Groups When Assigning Global Roles
If you use a user that is not part of any groups for initial setup, then you cannot search for groups when trying to assign a global role. To resolve this, you can either:
-
Manually edit the
authconfig/keycloakoidcobject to enable group search.-
On the Rancher server:
kubectl edit authconfigs.management.cattle.io keycloakoidc -
Set
groupSearchEnabled: true. -
Save your changes.
-
-
Reconfigure your Keycloak OIDC setup using a user that is assigned to at least one group in Keycloak.
Configuring OIDC Single Logout (SLO)
Rancher supports the ability to configure OIDC Single Logout (SLO). Options include logging out of the Rancher application only, logging out of Rancher and registered applications tied to the external authentication provider, or a prompt asking the user to choose between the previous options.
Prerequisites
Before configuring OIDC SLO, ensure the following is set up on your IdP:
-
SLO Support: The Log Out behavior configuration section only appears if your OIDC IdP allows for
OIDC SLO. -
Post-Logout Redirect URI: Your Rancher Server URL must be configured as an authorized post-logout redirect URI in your IdP’s OIDC client settings. This URL is used by the IdP to redirect a user back to Rancher after a successful external logout.
OIDC SLO Configuration
Configure the SLO settings when setting up or editing your OIDC authentication provider.
-
Sign in to Rancher using a standard user or an administrator role.
-
In the top left corner, select ☰ > Users & Authentication.
-
In the left navigation menu, select Auth Provider.
-
Under the section Log Out behavior, choose the appropriate SLO setting as described below:
Setting Description Log out of Rancher and not authentication provider
Choosing this option will only logout the Rancher application and not external authentication providers.
Log out of Rancher and authentication provider (includes all other applications registered with authentication provider)
Choosing this option will logout Rancher and all external authentication providers along with any registered applications linked to the provider.
Allow the user to choose one of the above in an additional log out step
Choosing this option presents users with a choice of logout method as described above.
-
If you choose to log out of your IdP, provide an End Session Endpoint. Rancher uses this URL to initiate the external logout.
How to get the End Session Endpoint
The end_session_endpoint is one of the specific URLs published within a standardized JSON object containing the IdP’s metadata and is retrieved from the OIDC Discovery URL. To get the end_session_endpoint from the OIDC Discovery URL, follow these steps:
-
Obtain the Discovery URL by appending the IdP Issuer URL with the well-known path (
.well-known/openid-configuration). -
Send an HTTP
GETrequest to the Discovery URL. -
In the JSON object, look for the key named
end_session_endpointand retrieve the URL.You can also use a
curlcommand to retrieveend_session_endpoint:curl -s <ISSUER_URL>/.well-known/openid-configuration | jq '.end_session_endpoint'