由于权限错误,无法将构建提交到 Cloud Build

问题描述 投票:0回答:4

通过

gcloud builds submit ...
提交 Cloud Build 运行时,我收到一条禁止错误,指出我无权访问存储桶。提交 Cloud Build 通常会涉及到存储桶的两个地方:暂存存储桶和日志存储桶。我将每个存储桶指定为我也有权访问的存储桶(相同的存储桶,只是不同的文件夹),因此命令如下所示:

gcloud builds submit 
--gcs-log-dir $my_bucket/logs 
--gcs-source-staging-dir $my_bucket/source  

我得到的错误是:

ERROR: (gcloud.builds.submit) 403: The user is forbidden from accessing the bucket [$my_bucket]: Please check your organization's policy.

我用

--log-http
--verbosity debug
重新运行,扩展的错误显示了真正的原因:

DEBUG: https://storageapis.google.com "GET /storage/v1/b/$my_bucket?alt=json"
... 
{
  "error": {
    "code": 403,
    "message": "$user does not have serviceusage.services.use access to the Google Cloud Project."
  }
}

我做了一些挖掘,发现在向请求提供配额/计费项目时会出现此错误(除了没有服务使用者角色之外)。我在检查请求的 HTTP 标头(其中包括

X-Goog-User-Project: $my_project
)时确认了这一点。

奇怪的是,我可以访问此存储桶中的对象,并且可以很好地运行

gsutil
/HTTP 命令,这些命令使用相同的 API 端点,不同之处在于
gsutil
在请求中不包含该用户项目。

有没有办法提交不包含该项目的构建,这样我就不需要

serviceusage.services.use
权限?我尝试在 gcloud 配置中取消设置该项目,但它提示我需要设置它或使用
--project
标志传递它。

编辑:存储桶也未启用“请求者付费”,这就是 gsutil 和客户端库工作正常的原因

google-cloud-platform google-cloud-storage google-cloud-build
4个回答
4
投票

出现此错误的唯一原因是您必须启用计费才能构建存储桶。

我在尝试本教程时通过单击仪表板左侧“入门”下的“创建云存储桶”启用了它。只需按照说明操作,您就会看到“启用计费”。启用计费后,您无需完成教程。返回您的工作并运行

$ gcloud build submit

就完成了!


3
投票

根据 GCP 文档:

要运行 gcloud builds 命令,仅具有 cloudbuild.builds.viewer 或 cloudbuild.builds.editor 角色的用户还需要 serviceusage.services.use 权限。要向用户授予此权限,请授予他们 serviceusage.serviceUsageConsumer 角色。

在 IAM 和管理员上编辑您的用户,选择您的用户并输入“服务使用消费者”。

但是,请检查您的策略和角色,因为我相信此选项适用于从头开始创建的干净用户,除了对象存储角色之外没有任何其他权限。


2
投票

我不确定您是否可以在不指定项目的情况下运行云构建。据我所知,

gcloud
命令在项目中运行,所以它是需要的。

如果您想使用不同的服务帐户,您可以使用服务帐户模拟添加

--impersonate-service-account
标志。

对于此 gcloud 调用,所有 API 请求都将作为给定的服务帐户而不是当前选择的帐户发出。


0
投票

我有类似的错误日志,我正在使用 docker 镜像运行

gcloud
google/cloud-sdk:alpine
:

googlecloudsdk.command_lib.builds.submit_util.BucketForbiddenError: The user is forbidden from accessing the bucket [xxxx]. Please check your organization's policy or if the user has the "serviceusage.services.use" permission. Giving the user Owner, Editor, or Viewer roles may also fix this issue. Alternatively, use the --no-source option and access your source code via a different method.

我已将

Role
Cloud Build Service Agent
添加到我的服务帐户。

服务帐户角色的完整列表:

  • 云运行管理
  • 存储洞察收集服务
  • 存储对象创建器
  • 存储对象查看器

Gcloud版本:

Google Cloud SDK 453.0.0
bq 2.0.98
bundled-python3-unix 3.9.17
core 2023.10.27
gcloud-crc32c 1.0.0
gsutil 5.27
© www.soinside.com 2019 - 2024. All rights reserved.