第 7 章 配置身份提供程序

7.1. 配置 htpasswd 身份提供程序

配置 htpasswd 身份提供程序,允许用户使用 htpasswd 文件中的凭证登录 OpenShift Container Platform。

要定义 htpasswd 身份提供程序,请执行以下任务:

  1. 创建一个 htpasswd 文件来存储 用户和密码信息。
  2. 创建一个 secret 来代表 htpasswd 文件。
  3. 定义一个htpasswd 身份提供程序资源用于引用该 secret。
  4. 将资源应用到 默认的 OAuth 配置,以添加身份提供程序。

7.1.1. 关于 OpenShift Container Platform 中的身份提供程序

默认情况下,集群中只有 kubeadmin 用户。要指定身份提供程序,您必须创建一个自定义资源(CR) 来描述该身份提供程序并把它添加到集群中。

注意

OpenShift Container Platform 用户名不能包括 /:%

7.1.2. 关于 htpasswd 身份验证

在 OpenShift Container Platform 中使用 htpasswd 身份验证允许您根据 htpasswd 文件识别用户。htpasswd 文件是一个平面文件,其中包含每个用户的用户名和密码。您可以使用 htpasswd 实用程序创建此文件。

警告

不要在 OpenShift Container Platform 中使用 htpasswd 身份验证用于生产环境。仅对开发环境使用 htpasswd 身份验证。

7.1.3. 创建 htpasswd 文件

有关如何创建 htpasswd 文件的说明,请参见以下小节之一:

7.1.3.1. 使用 Linux 创建 htpasswd 文件

要使用 htpasswd 身份提供程序,您必须使用 htpasswd 生成一个包含集群用户名和密码的文件。

先决条件

  • 能够访问 htpasswd 实用程序。在 Red Hat Enterprise Linux 上,安装 httpd-tools 软件包即可使用该实用程序。

流程

  1. 创建或更新含有用户名和散列密码的平面文件:

    $ htpasswd -c -B -b </path/to/users.htpasswd> <username> <password>

    该命令将生成散列版本的密码。

    例如:

    $ htpasswd -c -B -b users.htpasswd <username> <password>

    输出示例

    Adding password for user user1

  2. 继续向文件中添加或更新凭证:

    $ htpasswd -B -b </path/to/users.htpasswd> <user_name> <password>

7.1.3.2. 使用 Windows 创建 htpasswd 文件

要使用 htpasswd 身份提供程序,您必须使用 htpasswd 生成一个包含集群用户名和密码的文件。

先决条件

  • 能够访问 htpasswd.exe。许多 Apache httpd 发行版本的 \bin 目录中均包含此文件。

流程

  1. 创建或更新含有用户名和散列密码的平面文件:

    > htpasswd.exe -c -B -b <\path\to\users.htpasswd> <username> <password>

    该命令将生成散列版本的密码。

    例如:

    > htpasswd.exe -c -B -b users.htpasswd <username> <password>

    输出示例

    Adding password for user user1

  2. 继续向文件中添加或更新凭证:

    > htpasswd.exe -b <\path\to\users.htpasswd> <username> <password>

7.1.4. 创建 htpasswd secret

要使用 htpasswd 身份提供程序,您必须定义一个包含 htpasswd 用户文件的 secret。

先决条件

  • 创建 htpasswd 文件。

流程

  • 创建包含 htpasswd 用户文件的 Secret 对象:

    $ oc create secret generic htpass-secret --from-file=htpasswd=<path_to_users.htpasswd> -n openshift-config 1
    1
    包含 --from-file 参数的用户文件的 secret 键必须命名为 htpasswd,如上述命令所示。
    提示

    您还可以应用以下 YAML 来创建 secret:

    apiVersion: v1
    kind: Secret
    metadata:
      name: htpass-secret
      namespace: openshift-config
    type: Opaque
    data:
      htpasswd: <base64_encoded_htpasswd_file_contents>

7.1.5. Sample htpasswd CR

以下自定义资源 (CR) 显示 htpasswd 身份提供程序的参数和可接受值。

htpasswd CR

apiVersion: config.openshift.io/v1
kind: OAuth
metadata:
  name: cluster
spec:
  identityProviders:
  - name: my_htpasswd_provider 1
    mappingMethod: claim 2
    type: HTPasswd
    htpasswd:
      fileData:
        name: htpass-secret 3

1
此提供程序名称作为前缀放在提供程序用户名前,以此组成身份名称。
2
控制如何在此提供程序的身份和 User 对象之间建立映射。
3
包含使用 htpasswd 生成的文件的现有 secret。

其他资源

  • 如需了解所有身份提供程序通用的参数(如 mappingMethod )的信息,请参阅身份提供程序参数

7.1.6. 在集群中添加身份提供程序

安装集群之后,请在其中添加一个身份提供程序,以便您的用户可以进行身份验证。

先决条件

  • 创建 OpenShift Container Platform 集群。
  • 为身份提供程序创建自定义资源(CR)。
  • 必须已经以管理员身份登录。

流程

  1. 应用定义的 CR:

    $ oc apply -f </path/to/CR>
    注意

    如果一个 CR 不存在,oc apply 会创建一个新的 CR,并可能会触发以下警告 Warning: oc apply should be used on resources created by either oc create --save-config or oc apply。在这种情况下,您可以忽略这个警告。

  2. 以来自身份提供程序的用户身份登录集群,并在提示时输入密码。

    $ oc login -u <username>
  3. 确认用户登录成功,并显示用户名。

    $ oc whoami

7.1.7. 为 htpasswd 身份提供程序更新用户

您可以从现有的 htpasswd 身份提供程序中添加或删除用户。

先决条件

  • 您已创建了包含 htpasswd 用户文件的 Secret 对象。这里假定它名为 htpass-secret
  • 您已配置了 htpasswd 身份提供程序。这里假定它名为 my_htpasswd_provider
  • 您可以使用 htpasswd 工具程序。在 Red Hat Enterprise Linux 上,安装 httpd-tools 软件包即可使用该实用程序。
  • 您需要有集群管理员特权。

流程

  1. htpass-secret Secret 对象中检索 htpasswd 文件,并将该文件保存到您的文件系统中:

    $ oc get secret htpass-secret -ojsonpath={.data.htpasswd} -n openshift-config | base64 --decode > users.htpasswd
  2. users.htpasswd 文件中添加或删除用户。

    • 添加一个新用户:

      $ htpasswd -bB users.htpasswd <username> <password>

      输出示例

      Adding password for user <username>

    • 删除一个现有用户:

      $ htpasswd -D users.htpasswd <username>

      输出示例

      Deleting password for user <username>

  3. htpass-secret Secret 对象替换为 users.htpasswd 文件中更新的用户:

    $ oc create secret generic htpass-secret --from-file=htpasswd=users.htpasswd --dry-run=client -o yaml -n openshift-config | oc replace -f -
    提示

    您还可以应用以下 YAML 来替换 secret:

    apiVersion: v1
    kind: Secret
    metadata:
      name: htpass-secret
      namespace: openshift-config
    type: Opaque
    data:
      htpasswd: <base64_encoded_htpasswd_file_contents>
  4. 如果删除了一个或多个用户,您还需要为每个用户删除其现有资源。

    1. 删除 User 对象:

      $ oc delete user <username>

      输出示例

      user.user.openshift.io "<username>" deleted

      请确认已删除了用户,否则如果用户的令牌还没有过期,则用户还可以继续使用其令牌。

    2. 删除用户的 Identity 对象:

      $ oc delete identity my_htpasswd_provider:<username>

      输出示例

      identity.user.openshift.io "my_htpasswd_provider:<username>" deleted

7.1.8. 使用 web 控制台配置身份提供程序

通过 web 控制台而非 CLI 配置身份提供程序 (IDP)。

先决条件

  • 您必须以集群管理员身份登录到 web 控制台。

流程

  1. 导航至 AdministrationCluster Settings
  2. Configuration 选项卡下,单击 OAuth
  3. Identity Providers 部分中,从 Add 下拉菜单中选择您的身份提供程序。
注意

您可以通过 web 控制台来指定多个 IDP,而不会覆盖现有的 IDP。