如何解决从默认 Cloud Build 服务帐户在 Google Cloud Build 中执行时,GSuite 的 Terraform 提供程序能否访问 Admin SDK Directory API?
我决定使用 Terraform 自动创建 GC 项目。 Terraform 将在运行期间创建的一项资源是新的 GSuite 用户。这是使用 terraform-provider-gsuite 完成的。因此,当我从命令行运行 Terraform 步骤时,我设置了所有内容(服务帐户、域范围委派等)并且一切正常。
接下来,我决定使用一个 Cloud Build 触发器来执行 Terraform init-plan-apply,而不是依赖我的命令行。众所周知,云构建以 GCB 服务帐户的身份运行。这意味着我们需要为 SA 授予 Terraform 在执行期间可能需要的权限。到现在为止还挺好。 所以我运行构建,我看到 Terraform 无法创建的唯一资源是 GSuite 用户。翻阅日志,我发现了这 2 个请求(及其响应):
GET /admin/directory/v1/users?alt=json&customer=my_customer&prettyPrint=false&query=email%3Alolloso-admin%40codedby.pm HTTP/1.1
Host: www.googleapis.com
User-Agent: google-api-go-client/0.5 (linux amd64) Terraform/0.14.7
X-Goog-Api-Client: gl-go/1.15.6 gdcl/20200514
Accept-Encoding: gzip
HTTP/2.0 400 Bad Request
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Date: Sun,28 Feb 2021 12:58:25 GMT
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0
{
"error": {
"code": 400,"message": "Invalid Input","errors": [
{
"domain": "global","reason": "invalid"
}
]
}
}
POST /admin/directory/v1/users?alt=json&prettyPrint=false HTTP/1.1
Host: www.googleapis.com
User-Agent: google-api-go-client/0.5 (linux amd64) Terraform/0.14.7
Content-Length: 276
Content-Type: application/json
X-Goog-Api-Client: gl-go/1.15.6 gdcl/20200514
Accept-Encoding: gzip
{
"changePasswordAtNextLogin": true,"externalIds": [],"includeInGlobalAddressList": true,"name": {
"familyName": "********","givenName": "*******"
},"orgUnitPath": "/","password": "********","primaryEmail": "*********","sshPublicKeys": []
}
HTTP/2.0 403 Forbidden
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Date: Sun,28 Feb 2021 12:58:25 GMT
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
Www-Authenticate: Bearer realm="https://accounts.google.com/",error="insufficient_scope",scope="https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/directory.user"
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0
{
"error": {
"code": 403,"message": "Request had insufficient authentication scopes.","errors": [
{
"message": "Insufficient Permission","domain": "global","reason": "insufficientPermissions"
}
],"status": "PERMISSION_DENIED"
}
}
我认为这是 API 抱怨 Cloud Build 服务帐户没有足够的权限访问 Directory API。这就是情况变得疯狂的地方。
为此,我想向 Cloud Build SA 授予全域委托。但是那个 SA 很特别,我找不到授予它的方法。
然后我尝试将角色 serviceAccountUser
赋予我 SA 上的 Cloud Build SA(具有域范围委派的那个)。但我没有成功。事实上,构建仍然会出现权限不足的相同错误。
然后我尝试使用我的 SA(具有域范围的委托)作为自定义 Cloud Build 服务帐户。也有,运气不好。
甚至可以从 Cloud Build 访问某些通常会使用域范围委派的资源吗?
谢谢
更新 1(使用自定义构建服务帐户)
根据 John 的评论,我尝试使用用户指定的服务帐户来执行我的构建。已从 official guide 获取必要的设置信息。
这是我的 cloudbuild.yaml
文件
steps:
- id: 'tf init'
name: 'hashicorp/terraform'
entrypoint: 'sh'
args:
- '-c'
- |
terraform init
- id: 'tf plan'
name: 'hashicorp/terraform'
entrypoint: 'sh'
args:
- '-c'
- |
terraform plan
- id: 'tf apply'
name: 'hashicorp/terraform'
entrypoint: 'sh'
args:
- '-c'
- |
terraform apply -auto-approve
logsBucket: 'gs://tf-project-creator-cloudbuild-logs'
serviceAccount: 'projects/tf-project-creator/serviceAccounts/sa-terraform-project-creator@tf-project-creator.iam.gserviceaccount.com'
options:
env:
- 'TF_LOG=DEBUG'
其中 sa-terraform-project-creator@tf-project-creator.iam.gserviceaccount.com
是在我的 Google Workspace 上具有域范围委托的服务帐户。
然后我手动执行构建
export GOOGLE_APPLICATION_CREDENTIALS=.secrets/sa-terraform-project-creator.json; gcloud builds submit --config cloudbuild.yaml
指定上述相同SA的json私钥。 我本来希望构建通过,但我仍然遇到与上述相同的错误
POST /admin/directory/v1/users?alt=json&prettyPrint=false HTTP/1.1
Host: www.googleapis.com
User-Agent: google-api-go-client/0.5 (linux amd64) Terraform/0.14.7
Content-Length: 276
Content-Type: application/json
X-Goog-Api-Client: gl-go/1.15.6 gdcl/20200514
Accept-Encoding: gzip
{
"changePasswordAtNextLogin": true,"name": {
"familyName": "REDACTED","givenName": "REDACTED"
},"organizations": [],"password": "REDACTED","primaryEmail": "REDACTED","sshPublicKeys": []
}
-----------------------------------------------------
2021/03/06 17:26:19 [DEBUG] Google API Response Details:
---[ RESPONSE ]--------------------------------------
HTTP/2.0 403 Forbidden
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Date: Sat,06 Mar 2021 17:26:19 GMT
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
Www-Authenticate: Bearer realm="https://accounts.google.com/",www.googleapis.com/auth/directory.user"
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0
{
"error": {
"code": 403,{
"message": "Insufficient Permission","status": "PERMISSION_DENIED"
}
}
有什么我遗漏的吗?
更新 2(提交构建时检查活动身份) 正如 deviavir 在他们的评论中指出的那样,我尝试了
-
在 GCB 设置中启用“服务帐户”,但怀疑它不起作用。
-
在提交构建时仔细检查活动身份。使用自定义构建 SA 的限制之一是构建必须手动触发。所以使用 gcloud,这意味着
gcloud builds submit --config cloudbuild.yaml
直到现在,在执行此命令时,我总是通过像这样设置 GOOGLE_APPLICATION_CREDENTIALS
var 来添加它
export GOOGLE_APPLICATION_CREDENTIALS=.secrets/sa-terraform-project-creator.json
指定的私钥是我构建 SA(具有域范围委托的)的密钥。这样做时,我总是使用另一个没有域范围委派权限的帐户(项目的所有者)登录 gcloud。但我认为通过设置 GOOGLE_APPLICATION_CREDENTIALS,gcloud 会获取该凭据。我仍然认为是这种情况,但我尝试在使用相同的构建 SA 登录 gcloud 的同时提交构建。 所以我做到了
gcloud auth activate-service-account sa-terraform-project-creator@tf-project-creator.iam.gserviceaccount.com --key-file='.secrets/sa-terraform-project-creator.json'
紧随其后
gcloud builds submit --config cloudbuild.yaml
再一次,我在访问 Directory API 时遇到了同样的权限问题。
正如 deviavir 所怀疑的那样,我开始认为在构建执行期间,对 Directory API 的调用是使用错误的凭据完成的。 有没有办法记录在执行某些 Terraform 插件 API 调用时使用的身份?那会有很大帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。