【问题标题】:Cannot impersonate GCP ServiceAccount even after granting "Service Account Token Creator" role即使在授予“服务帐户令牌创建者”角色后也无法模拟 GCP ServiceAccount
【发布时间】:2021-03-31 06:53:09
【问题描述】:

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

  • 所有者
  • 执行者

所有者 ServiceAccount 附加了 1 个项目范围的角色:

  • “所有者”- 用于项目

执行者 ServiceAccount 只附加了 2 个特定角色(如下所示):

  • “服务帐户令牌创建者” - 在所有者服务帐户上
  • “服务帐户用户” - 在所有者服务帐户上


现在,我有一个 Executor ServiceAccount 的 JSON 密钥文件。我将使用该凭据文件来“模拟”所有者 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.

我在这里缺少什么?我确实已在 Owner ServiceAccount 上授予 Executor ServiceAccount roles/iam.serviceAccountTokenCreator 角色。

那为什么不能冒充呢?

【问题讨论】:

标签: google-cloud-platform gcloud impersonation service-accounts google-cloud-iam


【解决方案1】:

原来,我需要添加gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS 命令(more details here)。单独设置$GOOGLE_APPLICATION_CREDENTIALS 环境变量对于gcloud CLI 工具是不够的。

一直以来,我都知道在 $GOOGLE_APPLICATION_CREDENTIALS 中拥有 ServiceAccount 路径就足够了。在我阅读的所有地方,我都看到 ADC 为此使用 $GOOGLE_APPLICATION_CREDENTIALS

然而,事实证明gcloud 不使用 ADC。 ADC 仅由 C#、Java、Python、Go、Ruby 等客户端库使用。

我在谷歌的问题跟踪器中有filed a new case,用于帮助改进他们的文档以避免混淆/混淆。

【讨论】:

  • 谢谢,为我解决了这个问题。希望文档将得到更新,提供更多关于此的指导。
猜你喜欢
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
  • 2019-05-21
相关资源
最近更新 更多