即使在授予 "Service Account Token Creator" 角色后也无法模拟 GCP ServiceAccount
Cannot impersonate GCP ServiceAccount even after granting "Service Account Token Creator" role
我的 Google 云平台 (GCP) 项目中有 2 个 ServiceAccount
- 所有者
- 执行者
所有者 ServiceAccount 附加了 1 个项目范围的角色:
- “所有者”- 项目
执行者 ServiceAccount 仅附加了 2 个特定角色(如下所示):
- “服务帐户令牌创建者”- 在所有者服务帐户上
- “服务帐户用户”- 在所有者 ServiceAccount 上
现在,我有一个 Executor ServiceAccount 的 JSON 密钥文件。我将使用该凭证文件来“模拟”Owner 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
环境变量对于 gcloud
CLI 工具是不够的。
一直以来,我都知道 $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 的 Google 问题跟踪器中有 filed a new case 来帮助改进他们的文档以避免混淆/混淆。
我的 Google 云平台 (GCP) 项目中有 2 个 ServiceAccount
- 所有者
- 执行者
所有者 ServiceAccount 附加了 1 个项目范围的角色:
- “所有者”- 项目
执行者 ServiceAccount 仅附加了 2 个特定角色(如下所示):
- “服务帐户令牌创建者”- 在所有者服务帐户上
- “服务帐户用户”- 在所有者 ServiceAccount 上
现在,我有一个 Executor ServiceAccount 的 JSON 密钥文件。我将使用该凭证文件来“模拟”Owner 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
环境变量对于 gcloud
CLI 工具是不够的。
一直以来,我都知道 $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 的 Google 问题跟踪器中有 filed a new case 来帮助改进他们的文档以避免混淆/混淆。