$ htpasswd -c -B -b </path/to/users.htpasswd> <username> <password>
配置htpasswd
身份提供程序,允许用户使用 htpasswd 文件中的凭据登录 OpenShift Container Platform。
要定义 htpasswd 身份提供程序,请执行以下任务:
创建一个htpasswd
文件来存储用户和密码信息。
创建一个密钥来表示htpasswd
文件。
应用该资源到默认 OAuth 配置中以添加身份提供程序。
默认情况下,集群中只存在一个kubeadmin
用户。要指定身份提供程序,必须创建一个自定义资源 (CR),用于描述该身份提供程序并将其添加到集群。
OpenShift Container Platform 不支持包含 |
在 OpenShift Container Platform 中使用 htpasswd 身份验证允许您基于 htpasswd 文件识别用户。htpasswd 文件是一个平面文件,包含每个用户的用户名和哈希密码。您可以使用htpasswd
实用程序创建此文件。
不要在 OpenShift Container Platform 的生产环境中使用 htpasswd 身份验证。仅在开发环境中使用 htpasswd 身份验证。 |
有关如何创建 htpasswd 文件的说明,请参阅以下部分之一:
要使用 htpasswd 身份提供程序,必须使用htpasswd
生成一个包含集群用户名和密码的平面文件。
可以访问htpasswd
实用程序。在 Red Hat Enterprise Linux 上,可以通过安装httpd-tools
包来获得此程序。
使用用户名和哈希密码创建或更新平面文件。
$ htpasswd -c -B -b </path/to/users.htpasswd> <username> <password>
该命令会生成密码的哈希版本。
例如:
$ htpasswd -c -B -b users.htpasswd <username> <password>
Adding password for user user1
继续向文件中添加或更新凭据。
$ htpasswd -B -b </path/to/users.htpasswd> <user_name> <password>
要使用 htpasswd 身份提供程序,必须使用htpasswd
生成一个包含集群用户名和密码的平面文件。
可以访问htpasswd.exe
。此文件包含在许多 Apache httpd 发行版的\bin
目录中。
使用用户名和哈希密码创建或更新平面文件。
> 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
继续向文件中添加或更新凭据。
> htpasswd.exe -b <\path\to\users.htpasswd> <username> <password>
要使用 htpasswd 身份提供程序,必须定义一个包含 htpasswd 用户文件的密钥。
创建一个 htpasswd 文件。
创建一个包含 htpasswd 用户文件的Secret
对象。
$ oc create secret generic htpass-secret --from-file=htpasswd=<path_to_users.htpasswd> -n openshift-config (1)
1 | 包含用于--from-file 参数的用户文件的密钥名称必须为htpasswd ,如上述命令所示。 |
或者,您可以应用以下 YAML 来创建密钥:
|
以下自定义资源 (CR) 显示了 htpasswd 身份提供程序的参数和可接受的值。
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 生成的文件。 |
有关所有身份提供程序共有的参数(例如mappingMethod
)的信息,请参阅身份提供程序参数。
安装集群后,向其中添加身份提供程序,以便用户可以进行身份验证。
创建一个 OpenShift Container Platform 集群。
为您的身份提供程序创建自定义资源 (CR)。
您必须以管理员身份登录。
应用已定义的 CR
$ oc apply -f </path/to/CR>
如果 CR 不存在, |
使用身份提供程序中的用户登录到集群,并在提示时输入密码。
$ oc login -u <username>
确认用户已成功登录,并显示用户名。
$ oc whoami
您可以向现有的 htpasswd 身份提供程序添加或删除用户。
您已创建包含 htpasswd 用户文件的Secret
对象。此过程假设其名称为htpass-secret
。
您已配置 htpasswd 身份提供程序。此过程假设其名称为my_htpasswd_provider
。
可以访问htpasswd
实用程序。在 Red Hat Enterprise Linux 上,可以通过安装httpd-tools
包来获得此程序。
您具有集群管理员权限。
从htpass-secret
Secret
对象检索 htpasswd 文件,并将文件保存到您的文件系统。
$ oc get secret htpass-secret -ojsonpath={.data.htpasswd} -n openshift-config | base64 --decode > users.htpasswd
向users.htpasswd
文件添加或删除用户。
要添加新用户:
$ htpasswd -bB users.htpasswd <username> <password>
Adding password for user <username>
要删除现有用户:
$ htpasswd -D users.htpasswd <username>
Deleting password for user <username>
使用users.htpasswd
文件中更新的用户替换htpass-secret
Secret
对象。
$ oc create secret generic htpass-secret --from-file=htpasswd=users.htpasswd --dry-run=client -o yaml -n openshift-config | oc replace -f -
或者,您可以应用以下 YAML 来替换密钥:
|
如果您删除了一个或多个用户,则还必须删除每个用户的现有资源。
删除User
对象。
$ oc delete user <username>
user.user.openshift.io "<username>" deleted
请务必删除用户,否则用户可以在其令牌未过期的情况下继续使用其令牌。
删除用户的Identity
对象。
$ oc delete identity my_htpasswd_provider:<username>
identity.user.openshift.io "my_htpasswd_provider:<username>" deleted