搜索

12.2. 启用基于令牌的身份验证

download PDF

安装 Microsoft Azure OpenShift Container Platform 集群后,您可以启用 Microsoft Entra Workload ID 来使用简短凭证。

12.2.1. 配置 Cloud Credential Operator 工具

当 Cloud Credential Operator(CCO)以手动模式运行时,要从集群外部创建和管理云凭证,提取并准备 CCO 实用程序(ccoctl)二进制文件。

注意

ccoctl 工具是在 Linux 环境中运行的 Linux 二进制文件。

先决条件

  • 您可以访问具有集群管理员权限的 OpenShift Container Platform 帐户。
  • 已安装 OpenShift CLI(oc)。

流程

  1. 运行以下命令,为 OpenShift Container Platform 发行镜像设置变量:

    $ RELEASE_IMAGE=$(./openshift-install version | awk '/release image/ {print $3}')
  2. 运行以下命令,从 OpenShift Container Platform 发行镜像获取 CCO 容器镜像:

    $ CCO_IMAGE=$(oc adm release info --image-for='cloud-credential-operator' $RELEASE_IMAGE -a ~/.pull-secret)
    注意

    确保 $RELEASE_IMAGE 的架构与将使用 ccoctl 工具的环境架构相匹配。

  3. 运行以下命令,将 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 的主机指定这个值。
  4. 运行以下命令更改权限以使 ccoctl 可执行:

    $ chmod 775 ccoctl.<rhel_version>

验证

  • 要验证 ccoctl 是否可使用,请运行以下命令来显示帮助文件:

    $ ccoctl --help

    ccoctl --help 的输出

    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
      nutanix      Manage credentials objects for Nutanix
    
    Flags:
      -h, --help   help for ccoctl
    
    Use "ccoctl [command] --help" for more information about a command.

12.2.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 帐户。

流程

  1. ccoctl 工具生成的清单创建一个输出目录。此流程使用 ./output_dir 作为示例。
  2. 运行以下命令,将集群的服务帐户公钥密钥提取到输出目录中:

    $ oc get configmap \
      --namespace openshift-kube-apiserver bound-sa-token-signing-certs \
      --output 'go-template={{index .data "service-account-001.pub"}}' > ./output_dir/serviceaccount-signer.public 1
    1
    此流程使用名为 serviceaccount-signer.public 的文件作为示例。
  3. 运行以下命令,使用提取的服务帐户公钥创建 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.public 4
    1
    name 参数的值用于创建 Azure 资源组。要使用现有的 Azure 资源组而不是创建新 Azure 资源组,请使用现有组名称指定 --oidc-resource-group-name 参数作为其值。
    2
    指定现有集群的区域。
    3
    指定现有集群的订阅 ID。
    4
    指定包含集群服务帐户公钥的文件。
  4. 运行以下命令,验证 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.yaml 1
    -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 配置。
  5. 运行以下命令,使用输出目录中生成的清单中的 OIDC_ISSUER_URL 变量设置 OIDC_ISSUER_URL 变量:

    $ OIDC_ISSUER_URL=`awk '/serviceAccountIssuer/ { print $2 }' ./output_dir/manifests/cluster-authentication-02-config.yaml`
  6. 运行以下命令,更新集群 身份验证 配置的 spec.serviceAccountIssuer 参数:

    $ oc patch authentication cluster \
      --type=merge \
      -p "{\"spec\":{\"serviceAccountIssuer\":\"${OIDC_ISSUER_URL}\"}}"
  7. 运行以下命令监控配置更新进度:

    $ oc adm wait-for-stable-cluster

    这个过程可能需要 15 分钟或更长时间。以下输出表示进程已完成:

    All clusteroperators are stable
  8. 运行以下命令重启集群中的所有 pod:

    $ oc adm reboot-machine-config-pool mcp/worker mcp/master

    重启 pod 会更新 serviceAccountIssuer 字段,并刷新服务帐户公钥。

  9. 运行以下命令监控重启和更新进程:

    $ oc adm wait-for-node-reboot nodes --all

    这个过程可能需要 15 分钟或更长时间。以下输出表示进程已完成:

    All nodes rebooted
  10. 运行以下命令,将 Cloud Credential Operator spec.credentialsMode 参数更新为 Manual

    $ oc patch cloudcredential cluster \
      --type=merge \
      --patch '{"spec":{"credentialsMode":"Manual"}}'
  11. 运行以下命令,从 OpenShift Container Platform 发行镜像中提取 CredentialsRequest 对象列表:

    $ oc adm release extract \
      --credentials-requests \
      --included \
      --to <path_to_directory_for_credentials_requests> \
      --registry-config ~/.pull-secret
    注意

    此命令可能需要一些时间才能运行。

  12. 运行以下命令,使用 Azure 资源组名称设置 AZURE_INSTALL_RG 变量:

    $ AZURE_INSTALL_RG=`oc get infrastructure cluster -o jsonpath --template '{ .status.platformStatus.azure.resourceGroupName }'`
  13. 运行以下命令,使用 ccoctl 工具为所有 CredentialsRequest 对象创建受管身份:

    $ 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}"
    1
    指定包含 DNS 区的资源组的名称。
  14. 运行以下命令,为 Workload ID 应用 Azure pod 身份 Webhook 配置:

    $ oc apply -f ./output_dir/manifests/azure-ad-pod-identity-webhook-config.yaml
  15. 运行以下命令应用 ccoctl 工具生成的 secret:

    $ find ./output_dir/manifests -iname "openshift*yaml" -print0 | xargs -I {} -0 -t oc replace -f {}

    这可能需要几分钟。

  16. 运行以下命令重启集群中的所有 pod:

    $ oc adm reboot-machine-config-pool mcp/worker mcp/master

    重启 pod 会更新 serviceAccountIssuer 字段,并刷新服务帐户公钥。

  17. 运行以下命令监控重启和更新进程:

    $ oc adm wait-for-node-reboot nodes --all

    这个过程可能需要 15 分钟或更长时间。以下输出表示进程已完成:

    All nodes rebooted
  18. 运行以下命令监控配置更新进度:

    $ oc adm wait-for-stable-cluster

    这个过程可能需要 15 分钟或更长时间。以下输出表示进程已完成:

    All clusteroperators are stable
  19. 可选:运行以下命令来删除 Azure root 凭证 secret:

    $ oc delete secret -n kube-system azure-credentials

12.2.3. 验证集群是否使用短期凭证

您可以通过检查集群中的 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 的名称。

    平台Secret 名称

    Amazon Web Services (AWS)

    aws-creds

    Microsoft Azure

    azure-credentials

    Google Cloud Platform (GCP)

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

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.