GCP 工作负载身份联合 - Github 提供商 - “无法获取模拟凭据”

sc-*_*eds 7 oauth-2.0 google-cloud-platform workload-identity

我严格按照这些说明进行操作,以允许我使用短期令牌身份验证方法从我们的 github 操作工作流程访问 gcloud 资源。

我已经使用上面的确切说明创建了服务帐户、工作负载身份池和 github 提供程序池,但身份验证步骤似乎并未获取正确的令牌(或根本没有任何令牌)。GCP 服务帐户具有正确的 IAM 权限。

在这gcloud compute instances list一步中,我收到错误:

ERROR: (gcloud.compute.instances.list) There was a problem refreshing your current auth tokens: ('Unable to acquire impersonated credentials: No access token or invalid expiration in response.', '{\n  "error": {\n    "code": 403,\n    "message": "The caller does not have permission",\n    "status": "PERMISSION_DENIED"\n  }\n}\n')
Please run:

  $ gcloud auth login

to obtain new credentials.
Run Code Online (Sandbox Code Playgroud)

我的github actions文件如下:

ERROR: (gcloud.compute.instances.list) There was a problem refreshing your current auth tokens: ('Unable to acquire impersonated credentials: No access token or invalid expiration in response.', '{\n  "error": {\n    "code": 403,\n    "message": "The caller does not have permission",\n    "status": "PERMISSION_DENIED"\n  }\n}\n')
Please run:

  $ gcloud auth login

to obtain new credentials.
Run Code Online (Sandbox Code Playgroud)

我启用了令牌交换的日志记录,并且我也可以在 GCP 日志中看到它发生(没有明显的错误)。所以我完全被难住了。

有任何想法吗?

thc*_*ark 7

除了 OP 自己回答的服务帐户根本没有连接(绑定)之外,这可能是由于使用属性映射约束服务帐户绑定造成的。

在Google 博客上讨论的 WIF 的 GitHub Actions 默认设置中,提供程序设置了一组属性映射:

--attribute-mapping="google.subject=assertion.sub,attribute.repository=assertion.repository"
Run Code Online (Sandbox Code Playgroud)

当您连接(绑定)服务帐户时,这些属性可用于限制访问...在下面,成员使用repository属性来限制它,以便只my-org/my-repo允许在 GitHub 上执行的操作。

gcloud iam service-accounts add-iam-policy-binding "my-service-account@${PROJECT_ID}.iam.gserviceaccount.com" \
  --project="${PROJECT_ID}" \
  --role="roles/iam.workloadIdentityUser" \
  --member="principalSet://iam.googleapis.com/projects/1234567890/locations/global/workloadIdentityPools/my-pool/attribute.repository/my-org/my-repo"
Run Code Online (Sandbox Code Playgroud)

跨存储库提供商

当然,您想在这里使用强限制。限制到存储库,甚至特定分支(例如,只有主分支操作才有部署到生产的权限)。没有任何限制允许任何事情,这绝对不是你想要的!

就我而言,我设置了 WIF 提供程序,然后尝试从另一个存储库重用它,导致 OP 遇到错误。

我选择从 GitHub 的 OIDC 令牌中的所有可能属性列表中repository_owner添加属性映射(属性映射可在 google 云控制台中编辑),然后将我的服务帐户绑定到该属性,而不是特定于存储库的主体:

--attribute-mapping="google.subject=assertion.sub,attribute.repository_owner=assertion.repository_owner"
Run Code Online (Sandbox Code Playgroud)

gcloud iam service-accounts add-iam-policy-binding "my-service-account@${PROJECT_ID}.iam.gserviceaccount.com" \
  --project="${PROJECT_ID}" \
  --role="roles/iam.workloadIdentityUser" \
  --member="principalSet://iam.googleapis.com/projects/1234567890/locations/global/workloadIdentityPools/my-pool/attribute.repository_owner/my-org"
Run Code Online (Sandbox Code Playgroud)

宾果,它现在很有魅力了。

不过,请注意考虑您的攻击面,过大地放松此限制会产生真正的漏洞。


sc-*_*eds 5

所以后来我才知道这是什么。尽管运行:

gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_EMAIL \
    --role=roles/iam.workloadIdentityUser \
    --member="MEMBER_EXPRESSION"
Run Code Online (Sandbox Code Playgroud)

根据文档,它没有授予权限 - 我进入控制台并检查“连接的服务帐户”菜单(左侧)下的工作负载身份池,并且服务帐户不在那里,所以我手动添加了它。