跳到主要内容
版本:Latest

配置 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 Connect
    Client authenticationON
    Valid Redirect URIhttps://yourRancherHostURL/verify-auth
  • 在新的 OIDC 客户端中,创建 Mappers 来公开用户字段。

    1. In the navigation menu, click Clients.

    2. Click the Clients list tab.

    3. Find and click the client you created.

    4. Click the Client scopes tab.

    5. Find and click the link labeled <client-name>-dedicated. For example, if you named your client rancher, look for the link named rancher-dedicated.

    6. Click the Mappers tab.

    7. 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.

        SettingValue
        NameGroups Mapper
        Mapper TypeGroup Membership
        Token Claim Namegroups
        Full group pathOFF
        Add to ID tokenOFF
        Add to access tokenOFF
        Add 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.

        SettingValue
        NameClient Audience
        Mapper TypeAudience
        Included Client Audience<client-name>
        Add to ID tokenOFF
        Add 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.

        SettingValue
        NameGroup Path
        Mapper TypeGroup Membership
        Token Claim Namefull_group_path
        Full group pathON
        Add to ID tokenON
        Add to access tokenON
        Add to user infoON
  • Add the following role mappings to all users or groups that need to query the Keycloak users.

  1. In the navigation menu, click Users.
  2. Click the user you want to add role mappings to.
  3. Click the Role mapping tab.
  4. Click Assign role.
  5. Select the following roles:
    • query-users
    • query-groups
    • view-users
  6. Click Assign.
## 在 Rancher 中配置 Keycloak
  1. 在 Rancher UI 中,单击 ☰ > 用户 & 认证

  2. 单击左侧导航栏的认证

  3. 选择 Keycloak (OIDC)

  4. 填写配置 Keycloak OIDC 账号表单。有关填写表单的帮助,请参见配置参考

    备注

    When configuring the Endpoints section using the Generate option, Rancher includes /auth as 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.

  5. 完成配置 Keycloak OIDC 账号表单后,单击启用

    Rancher 会将你重定向到 IdP 登录页面。输入使用 Keycloak IdP 进行身份验证的凭证,来验证你的 Rancher Keycloak 配置。

    备注

    你可能需要禁用弹出窗口阻止程序才能看到 IdP 登录页面。

结果:已将 Rancher 配置为使用 OIDC 协议与 Keycloak 一起工作。你的用户现在可以使用 Keycloak 登录名登录 Rancher。

配置参考

字段描述
客户端 ID你的 Keycloak 客户端的 Client ID
客户端密码你的 Keycloak 客户端生成的 Secret。在 Keycloak 控制台中,单击 Clients,选择你创建的客户端,选择 Credentials 选项卡,然后复制 Secret 字段的值。
私钥/证书在 Rancher 和你的 IdP 之间创建安全外壳(SSH)的密钥/证书对。如果你的 Keycloak 服务器上启用了 HTTPS/SSL,则为必填。
端点选择为 Rancher URL发行者Auth 端点字段使用生成的值,还是在不正确时进行手动覆盖。
Keycloak URL你的 Keycloak 服务器的 URL。
Keycloak Realm创建 Keycloak 客户端的 Realm 的名称。
Rancher URLRancher Server 的 URL。
Issuer你的 IdP 的 URL。
Auth 端点重定向用户进行身份验证的 URL。

从 SAML 迁移到 OIDC

本节描述了将使用 Keycloak (SAML) 的 Rancher 过渡到 Keycloak (OIDC) 的过程。

  1. Reconfigure Keycloak.
    1. Configure a new OpenID Connect client according to the Prerequisites. Ensure the same Valid Redirect URIs are set.
    2. Configure mappers for the new client according to the Prerequisites.
  2. Before configuring Rancher to use Keycloak (OIDC), Keycloak (SAML) must be first disabled.
    1. In the Rancher UI, click ☰ > Users & Authentication.
    2. In the left navigation bar, click Auth Provider.
    3. Select Keycloak (SAML).
    4. Click Disable.
  3. Follow the steps in Configuring Keycloak in Rancher.
警告

配置完成后,由于用户权限不会自动迁移,你需要重新申请 Rancher 用户权限。

附录:故障排除

如果你在测试与 Keycloak 服务器的连接时遇到问题,请先检查 OIDC 客户端的配置选项。你还可以检查 Rancher 日志来查明问题的原因。调试日志可能包含有关错误的更详细信息。详情请参见如何启用调试日志

所有与 Keycloak 相关的日志条目都将添加 [generic oidc][keycloak oidc]

不能重定向到 Keycloak

完成配置 Keycloak OIDC 账号表单并单击启用后,你没有被重定向到你的 IdP。

验证你的 Keycloak 客户端配置。

生成的 IssuerAuth 端点不正确

配置 Keycloak OIDC 账号表单中,将端点更改为指定(高级设置)并覆盖发行者Auth 端点的值。要查找这些值,前往 Keycloak 控制台并选择 Realm Settings,选择 General 选项卡,然后单击 OpenID Endpoint Configuration。JSON 输出将显示 issuerauthorization_endpoint 的值。

Keycloak 错误:"Invalid grant_type"

在某些情况下,这条错误提示信息可能有误导性,实际上造成错误的原因是 Valid Redirect URI 配置错误。