问题描述
我的 Google Cloud Platform (GCP) 项目中有 2 个 ServiceAccounts
- 所有者
- 执行者
所有者 ServiceAccount 附加了 1 个项目范围的角色:
- “所有者”- 项目
executor ServiceAccount 只附加了 2 个特定角色(如下所示):
- “服务帐户令牌创建者”- 在所有者 ServiceAccount 上
- “服务帐户用户”- 在所有者 ServiceAccount 上
现在,我有一个 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.
我在这里错过了什么?我确实已授予 Executor ServiceAccount 对 Owner ServiceAccount 的 roles/iam.serviceAccountTokenCreator
角色。
那为什么不能模拟呢?
解决方法
事实证明,我需要添加 gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS
命令 (more details here)。对于 $GOOGLE_APPLICATION_CREDENTIALS
CLI 工具,仅设置 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 等客户端库使用。
我在 google 的问题跟踪器中有 filed a new case,以便 Google 帮助改进他们的文档以避免混淆/混淆。