10.4. 启用基于令牌的身份验证
在 Microsoft Azure 或 Amazon Web Services (AWS) 上安装 OpenShift Container Platform 集群后,您可以启用 Microsoft Entra Workload ID 或 Security Token Service (STS) 来使用短期凭证。
10.4.1. 配置 Cloud Credential Operator 工具 复制链接链接已复制到粘贴板!
要将现有集群配置为从集群外部创建和管理云凭证,请提取并准备 Cloud Credential Operator 实用程序(ccoctl)二进制文件。
ccoctl 工具是在 Linux 环境中运行的 Linux 二进制文件。
先决条件
- 您可以访问具有集群管理员权限的 OpenShift Container Platform 帐户。
-
已安装 OpenShift CLI(
oc)。
流程
运行以下命令,为 OpenShift Container Platform 发行镜像设置变量:
$ RELEASE_IMAGE=$(oc get clusterversion -o jsonpath={..desired.image})运行以下命令,从 OpenShift Container Platform 发行镜像获取 CCO 容器镜像:
$ CCO_IMAGE=$(oc adm release info --image-for='cloud-credential-operator' $RELEASE_IMAGE -a ~/.pull-secret)注意确保
$RELEASE_IMAGE的架构与将使用ccoctl工具的环境架构相匹配。运行以下命令,将 CCO 容器镜像中的
ccoctl二进制文件提取到 OpenShift Container Platform 发行镜像中:$ oc image extract $CCO_IMAGE \ --file="/usr/bin/ccoctl.<rhel_version>" \1 -a ~/.pull-secret- 1
- 对于
<rhel_version>,请指定与主机使用的 Red Hat Enterprise Linux (RHEL) 版本对应的值。如果没有指定值,则默认使用ccoctl.rhel8。以下值有效:-
rhel8: 为使用 RHEL 8 的主机指定这个值。 -
rhel9:为使用 RHEL 9 的主机指定这个值。
-
注意ccoctl二进制文件在您执行此命令的目录中创建,而不是在/usr/bin/中创建。您必须重命名目录或将ccoctl.<rhel_version>二进制文件移到ccoctl。运行以下命令更改权限以使
ccoctl可执行:$ chmod 775 ccoctl
验证
要验证
ccoctl是否准备就绪,可以尝试显示帮助文件。运行命令时使用相对文件名,例如:$ ./ccoctl输出示例
OpenShift credentials provisioning tool Usage: ccoctl [command] Available Commands: aws Manage credentials objects for AWS cloud azure Manage credentials objects for Azure gcp Manage credentials objects for Google cloud help Help about any command ibmcloud Manage credentials objects for {ibm-cloud-title} nutanix Manage credentials objects for Nutanix Flags: -h, --help help for ccoctl Use "ccoctl [command] --help" for more information about a command.
10.4.2. 在现有集群中启用 Microsoft Entra Workload ID 复制链接链接已复制到粘贴板!
如果您没有将 Microsoft Azure OpenShift Container Platform 集群配置为在安装过程中使用 Microsoft Entra Workload ID,您可以在现有集群上启用此验证方法。
在现有集群中启用工作负载 ID 的过程具有破坏性,需要花费大量时间。在继续操作前,请观察以下注意事项:
- 阅读以下步骤,并确保您了解并接受时间要求。具体的时间要求因单个集群而异,但可能需要至少一个小时。
- 在此过程中,您必须刷新所有服务帐户并重启集群中的所有 pod。这些操作对工作负载具有破坏性。要缓解这种影响,您可以临时停止这些服务,然后在集群就绪时重新部署它们。
- 启动此过程后,在完成前不要尝试更新集群。如果触发更新,在现有集群中启用 Workload ID 的过程会失败。
先决条件
- 您已在 Microsoft Azure 上安装了 OpenShift Container Platform 集群。
-
您可以使用具有
cluster-admin权限的账户访问集群。 -
已安装 OpenShift CLI(
oc)。 -
您已提取并准备 Cloud Credential Operator 实用程序 (
ccoctl) 二进制文件。 -
您可以使用 Azure CLI (
az) 访问 Azure 帐户。
流程
-
为
ccoctl工具生成的清单创建一个输出目录。此流程使用./output_dir作为示例。 运行以下命令,将集群的服务帐户公钥密钥提取到输出目录中:
$ oc get secret/next-bound-service-account-signing-key \ -n openshift-kube-apiserver-operator \ -ojsonpath='{ .data.service-account\.pub }' | base64 -d \ > output_dir/serviceaccount-signer.public1 - 1
- 此流程使用名为
serviceaccount-signer.public的文件作为示例。
运行以下命令,使用提取的服务帐户公钥创建 OpenID Connect (OIDC) 签发者和 Azure blob 存储容器:
$ ./ccoctl azure create-oidc-issuer \ --name <azure_infra_name> \1 --output-dir ./output_dir \ --region <azure_region> \2 --subscription-id <azure_subscription_id> \3 --tenant-id <azure_tenant_id> \ --public-key-file ./output_dir/serviceaccount-signer.public4 运行以下命令,验证 Azure pod 身份 Webhook 的配置文件是否已创建:
$ ll ./output_dir/manifests输出示例
total 8 -rw-------. 1 cloud-user cloud-user 193 May 22 02:29 azure-ad-pod-identity-webhook-config.yaml1 -rw-------. 1 cloud-user cloud-user 165 May 22 02:29 cluster-authentication-02-config.yaml- 1
- 文件
azure-ad-pod-identity-webhook-config.yaml包含 Azure pod 身份 Webhook 配置。
运行以下命令,使用输出目录中生成的清单中的
OIDC_ISSUER_URL变量设置 OIDC_ISSUER_URL 变量:$ OIDC_ISSUER_URL=`awk '/serviceAccountIssuer/ { print $2 }' ./output_dir/manifests/cluster-authentication-02-config.yaml`运行以下命令,更新集群
身份验证配置的spec.serviceAccountIssuer参数:$ oc patch authentication cluster \ --type=merge \ -p "{\"spec\":{\"serviceAccountIssuer\":\"${OIDC_ISSUER_URL}\"}}"运行以下命令监控配置更新进度:
$ oc adm wait-for-stable-cluster这个过程可能需要 15 分钟或更长时间。以下输出表示进程已完成:
All clusteroperators are stable运行以下命令重启集群中的所有 pod:
$ oc adm reboot-machine-config-pool mcp/worker mcp/master重启 pod 会更新
serviceAccountIssuer字段,并刷新服务帐户公钥。运行以下命令监控重启和更新进程:
$ oc adm wait-for-node-reboot nodes --all这个过程可能需要 15 分钟或更长时间。以下输出表示进程已完成:
All nodes rebooted运行以下命令,将 Cloud Credential Operator
spec.credentialsMode参数更新为Manual:$ oc patch cloudcredential cluster \ --type=merge \ --patch '{"spec":{"credentialsMode":"Manual"}}'运行以下命令,从 OpenShift Container Platform 发行镜像中提取
CredentialsRequest对象列表:$ oc adm release extract \ --credentials-requests \ --included \ --to <path_to_directory_for_credentials_requests> \ --registry-config ~/.pull-secret注意此命令可能需要一些时间才能运行。
运行以下命令,使用 Azure 资源组名称设置
AZURE_INSTALL_RG变量:$ AZURE_INSTALL_RG=`oc get infrastructure cluster -o jsonpath --template '{ .status.platformStatus.azure.resourceGroupName }'`运行以下命令,使用
ccoctl工具为所有CredentialsRequest对象创建受管身份:注意以下命令没有显示所有可用的选项。如需完整的选项列表,包括可能需要特定用例的选项列表,请运行
$ ccoctl azure create-managed-identities --help。$ ccoctl azure create-managed-identities \ --name <azure_infra_name> \ --output-dir ./output_dir \ --region <azure_region> \ --subscription-id <azure_subscription_id> \ --credentials-requests-dir <path_to_directory_for_credentials_requests> \ --issuer-url "${OIDC_ISSUER_URL}" \ --dnszone-resource-group-name <azure_dns_zone_resourcegroup_name> \1 --installation-resource-group-name "${AZURE_INSTALL_RG}" \ --network-resource-group-name <azure_resource_group>2 运行以下命令,为 Workload ID 应用 Azure pod 身份 Webhook 配置:
$ oc apply -f ./output_dir/manifests/azure-ad-pod-identity-webhook-config.yaml运行以下命令应用
ccoctl工具生成的 secret:$ find ./output_dir/manifests -iname "openshift*yaml" -print0 | xargs -I {} -0 -t oc replace -f {}这可能需要几分钟。
运行以下命令重启集群中的所有 pod:
$ oc adm reboot-machine-config-pool mcp/worker mcp/master重启 pod 会更新
serviceAccountIssuer字段,并刷新服务帐户公钥。运行以下命令监控重启和更新进程:
$ oc adm wait-for-node-reboot nodes --all这个过程可能需要 15 分钟或更长时间。以下输出表示进程已完成:
All nodes rebooted运行以下命令监控配置更新进度:
$ oc adm wait-for-stable-cluster这个过程可能需要 15 分钟或更长时间。以下输出表示进程已完成:
All clusteroperators are stable可选:运行以下命令来删除 Azure root 凭证 secret:
$ oc delete secret -n kube-system azure-credentials
10.4.3. 在现有集群中启用 AWS 安全令牌服务(STS) 复制链接链接已复制到粘贴板!
如果您没有在安装过程中将 Amazon Web Services (AWS) OpenShift Container Platform 集群配置为使用安全令牌服务(STS),您可以在现有集群上启用此验证方法。
在现有集群中启用 STS 的过程具有破坏性,并需要大量时间。在继续操作前,请观察以下注意事项:
- 阅读以下步骤,并确保您了解并接受时间要求。具体的时间要求因单个集群而异,但可能需要至少一个小时。
- 在此过程中,您必须刷新所有服务帐户并重启集群中的所有 pod。这些操作对工作负载具有破坏性。要缓解这种影响,您可以临时停止这些服务,然后在集群就绪时重新部署它们。
- 在此过程完成前,不要更新集群。
先决条件
- 您已在 AWS 上安装了 OpenShift Container Platform 集群。
-
您可以使用具有
cluster-admin权限的账户访问集群。 -
已安装 OpenShift CLI(
oc)。 -
您已提取并准备 Cloud Credential Operator 实用程序 (
ccoctl) 二进制文件。 - 您可以使用 AWS CLI (aws)访问 AWS 帐户。
流程
为
ccoctl生成的清单创建一个输出目录。$ mkdir ./output_dir创建 AWS Identity and Access Management (IAM) OpenID Connect (OIDC)供应商。
运行以下命令,为集群提取服务帐户公钥:
$ oc get secret/next-bound-service-account-signing-key \ -n openshift-kube-apiserver-operator \ -ojsonpath='{ .data.service-account\.pub }' | base64 -d \ > output_dir/serviceaccount-signer.public1 - 1
- 此流程使用名为
serviceaccount-signer.public的文件作为示例。
运行以下命令来创建 AWS IAM 身份提供程序和 S3 存储桶:
$ ./ccoctl aws create-identity-provider \ --output-dir output_dir \1 --name <name_you_choose> \2 --region us-east-2 \3 --public-key-file output_dir/serviceaccount-signer.public4 - 保存或记下 IAM 身份提供程序的 Amazon 资源名称(ARN)。您可以在上一命令输出的最后一行中找到此信息。
更新集群身份验证配置。
运行以下命令提取 OIDC 签发者 URL 并更新集群的身份验证配置:
$ OIDC_ISSUER_URL=`awk '/serviceAccountIssuer/ { print $2 }' output_dir/manifests/cluster-authentication-02-config.yaml` $ oc patch authentication cluster --type=merge -p "{\"spec\":{\"serviceAccountIssuer\":\"${OIDC_ISSUER_URL}\"}}"运行以下命令监控配置更新进度:
$ oc adm wait-for-stable-cluster这个过程可能需要 15 分钟或更长时间。以下输出表示进程已完成:
All clusteroperators are stable
重启 pod 以应用签发者更新。
运行以下命令重启集群中的所有 pod:
$ oc adm reboot-machine-config-pool mcp/worker mcp/master重启 pod 会更新
serviceAccountIssuer字段,并刷新服务帐户公钥。运行以下命令监控重启和更新进程:
$ oc adm wait-for-node-reboot nodes --all这个过程可能需要 15 分钟或更长时间。以下输出表示进程已完成:
All nodes rebooted
运行以下命令,将 Cloud Credential Operator
spec.credentialsMode参数更新为Manual:$ oc patch cloudcredential cluster \ --type=merge \ --patch '{"spec":{"credentialsMode":"Manual"}}'提取
CredentialsRequests对象。运行以下命令来创建
CLUSTER_VERSION环境变量:$ CLUSTER_VERSION=$(oc get clusterversion version -o json | jq -r '.status.desired.version')运行以下命令来创建
CLUSTER_IMAGE环境变量:$ CLUSTER_IMAGE=$(oc get clusterversion version -o json | jq -r ".status.history[] | select(.version == \"${CLUSTER_VERSION}\") | .image")运行以下命令,从发行镜像中提取
CredentialsRequests对象:$ oc adm release extract \ --credentials-requests \ --cloud=aws \ --from ${CLUSTER_IMAGE} \ --to output_dir/cred-reqs
创建 AWS IAM 角色并应用 secret。
运行以下命令,为每个
CredentialsRequests对象创建一个 IAM 角色:$ ./ccoctl aws create-iam-roles \ --output-dir ./output_dir/ \1 --name <name_you_choose> \2 --identity-provider-arn <identity_provider_arn> \3 --region us-east-2 \4 --credentials-requests-dir ./output_dir/cred-reqs/5 运行以下命令来应用生成的 secret:
$ find ./output_dir/manifests -iname "openshift*yaml" -print0 | xargs -I {} -0 -t oc replace -f {}
通过重启集群来完成配置过程。
运行以下命令重启集群中的所有 pod:
$ oc adm reboot-machine-config-pool mcp/worker mcp/master运行以下命令监控重启和更新进程:
$ oc adm wait-for-node-reboot nodes --all这个过程可能需要 15 分钟或更长时间。以下输出表示进程已完成:
All nodes rebooted运行以下命令监控配置更新进度:
$ oc adm wait-for-stable-cluster这个过程可能需要 15 分钟或更长时间。以下输出表示进程已完成:
All clusteroperators are stable
可选:运行以下命令来删除 AWS 根凭证 secret:
$ oc delete secret -n kube-system aws-creds
10.4.4. 验证集群是否使用短期凭证 复制链接链接已复制到粘贴板!
您可以通过检查集群中的 Cloud Credential Operator (CCO) 配置和其他值来验证集群是否对各个组件使用简短安全凭证。
先决条件
-
已使用 Cloud Credential Operator 实用程序(
ccoctl)部署了 OpenShift Container Platform 集群来实现短期凭证。 -
已安装 OpenShift CLI(
oc)。 -
您以具有
cluster-admin权限的用户身份登录。
流程
运行以下命令,验证 CCO 是否配置为以手动模式运行:
$ oc get cloudcredentials cluster \ -o=jsonpath={.spec.credentialsMode}以下输出确认 CCO 以手动模式运行:
输出示例
Manual运行以下命令,验证集群没有
root凭证:$ oc get secrets \ -n kube-system <secret_name>其中
<secret_name>是云供应商的 root secret 的名称。Expand 平台 Secret 名称 Amazon Web Services (AWS)
aws-credsMicrosoft Azure
azure-credentialsGoogle Cloud
gcp-credentials一个错误确认集群中不存在 root secret。
AWS 集群的输出示例
Error from server (NotFound): secrets "aws-creds" not found运行以下命令,验证组件是否在单个组件中使用短期安全凭证:
$ oc get authentication cluster \ -o jsonpath \ --template='{ .spec.serviceAccountIssuer }'此命令显示集群
Authentication对象中.spec.serviceAccountIssuer参数的值。与云供应商关联的 URL 的输出表示集群使用从集群外部创建和管理的简短凭证的手动模式。Azure 集群:通过运行以下命令,验证组件假定 secret 清单中指定的 Azure 客户端 ID:
$ oc get secrets \ -n openshift-image-registry installer-cloud-credentials \ -o jsonpath='{.data}'输出中包含了
azure_client_id和azure_federated_token_file字段代表组件假定 Azure 客户端 ID。Azure 集群 :运行以下命令来验证 pod 身份 Webhook 是否正在运行:
$ oc get pods \ -n openshift-cloud-credential-operator输出示例
NAME READY STATUS RESTARTS AGE cloud-credential-operator-59cf744f78-r8pbq 2/2 Running 2 71m pod-identity-webhook-548f977b4c-859lz 1/1 Running 1 70m