gpt4 book ai didi

google-cloud-platform - 即使授予 "Service Account Token Creator"角色后也无法模拟 GCP ServiceAccount

转载 作者:行者123 更新时间:2023-12-03 23:41:38 25 4
gpt4 key购买 nike

我的 Google Cloud Platform (GCP) 项目中有 2 个 ServiceAccounts

  • 店主
  • 执行人

  • 所有者 ServiceAccount 附加了 1 个项目范围的角色:
  • “所有者” - 用于项目

  • executor ServiceAccount 仅附加了 2 个特定角色(如下所示):
  • “服务帐户 token 创建者”- 所有者服务帐户
  • “服务帐户用户” - 所有者服务帐户

  • enter image description here

    现在,我有一个 Executor ServiceAccount 的 JSON key 文件。我将使用该凭证文件来“模拟”所有者 ServiceAccount。然后我会跑 gcloud命令。

    这是我在做什么。
    #!/bin/bash

    # --------------------------------------------------------------

    export GOOGLE_APPLICATION_CREDENTIALS="$(pwd)/my-executor-sa-key.json"
    echo $GOOGLE_APPLICATION_CREDENTIALS
    cat $GOOGLE_APPLICATION_CREDENTIALS

    OWNER_EMAIL="owner@my-gcp-project.iam.gserviceaccount.com"
    echo $OWNER_EMAIL

    # --------------------------------------------------------------

    CLUSTER="my-k8s-cluster"
    ZONE="asia-east1-a"
    PROJECT="my-gcp-project"

    MY_COMMAND="gcloud container clusters get-credentials ${CLUSTER} --zone ${ZONE} --project ${PROJECT} --impersonate-service-account=${OWNER_EMAIL}"
    echo $MY_COMMAND

    # --------------------------------------------------------------

    `$MY_COMMAND`

    # --------------------------------------------------------------
    运行上述程序后,这是我得到的输出

    # --------------------------------------------------------------

    /Users/rakib/tmp/my-executor-sa-key.json
    {
    "type": "service_account",
    "project_id": "my-gcp-project",
    "private_key_id": "3208--------------------------------5d63",
    "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEv\n----\nEAE9S\n-----END PRIVATE KEY-----\n",
    "client_email": "executor@my-gcp-project.iam.gserviceaccount.com",
    "client_id": "1099--------------5533",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/executor%40my-gcp-project.iam.gserviceaccount.com"
    }

    owner@my-gcp-project.iam.gserviceaccount.com

    # --------------------------------------------------------------

    gcloud container clusters get-credentials my-k8s-cluster --zone asia-east1-a --project my-gcp-project --impersonate-service-account=owner@my-gcp-project.iam.gserviceaccount.com

    # --------------------------------------------------------------

    WARNING: This command is using service account impersonation. All API calls will be executed as [owner@my-gcp-project.iam.gserviceaccount.com].
    ERROR: (gcloud.container.clusters.get-credentials) Failed to impersonate [owner@my-gcp-project.iam.gserviceaccount.com]. Make sure the account that's trying to impersonate it has access to the service account itself and the "roles/iam.serviceAccountTokenCreator" role.

    我在这里缺少什么?我确实授予了 Executor ServiceAccount roles/iam.serviceAccountTokenCreator Owner ServiceAccount 上的角色。
    那为什么不能模仿呢?

    最佳答案

    原来,我需要添加 gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS命令( more details here )。设置 $GOOGLE_APPLICATION_CREDENTIALS gcloud 仅靠环境变量是不够的命令行工具。
    一直以来,我都知道在 $GOOGLE_APPLICATION_CREDENTIALS 中有 ServiceAccount 路径。就足够了。在我读到的任何地方,我都能看到 ADC 用途 $GOOGLE_APPLICATION_CREDENTIALS为了那个原因:

  • https://cloud.google.com/docs/authentication/production#automatically
  • https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable

  • 然而,事实证明 gcloud然后不使用ADC。 ADC 仅由 C#、Java、Python、Go、Ruby 等客户端库使用。
    我有 filed a new case在 google 的问题跟踪器中为 Google 提供帮助,以帮助改进他们的文档以避免混淆/混淆。

    关于google-cloud-platform - 即使授予 "Service Account Token Creator"角色后也无法模拟 GCP ServiceAccount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65393957/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com