Cloud Build 服务账号

Cloud Build 可能会使用特殊的服务帐号代表您执行构建。Cloud Build 服务帐号的电子邮件地址为 [PROJECT_NUMBER]@cloudbuild.gserviceaccount.com。此服务帐号的权限可能对您的使用场景而言过于宽泛。您可以按照最小权限原则改善安全状况。作为此原则的一部分,我们建议您创建自己的服务帐号来代表您执行构建,这样可以降低配置错误或恶意用户的潜在影响。

本页面介绍了 Cloud Build 服务账号默认拥有的所有权限。如需了解如何授予或撤消 Cloud Build 服务账号的权限,请参阅配置 Cloud Build 服务账号的访问权限

Cloud Build 服务账号的默认权限

为 Google Cloud 项目启用 Cloud Build API 时,系统将在项目中自动创建 Cloud Build 服务帐号,并为项目中的资源授予 Cloud Build 服务帐号角色。此角色包含多种权限,比如更新构建或写入日志的能力。该服务账号仅在执行构建时需要执行操作才会使用这些权限。例如,如果构建的源代码位于 Cloud Source Repositories 代码库中,则服务账号使用 source.repos.get 权限从 Cloud Source Repositories 代码库中提取代码。如果您打算在构建过程中执行某项操作,我们建议您从 Cloud Build 服务账号撤消相应的权限以遵守最小权限安全原则

下表列出了 Cloud Build 服务账号角色包含的权限以及 Cloud Build 服务账号使用这些权限的用途。

权限 说明 权限的用途
cloudbuild.builds.create 可以创建构建和触发器 需要这些权限才能执行以下操作:
  • 使用构建触发器。
  • 创建、列出、获取或取消构建。
cloudbuild.builds.update 可以更新构建和触发器
cloudbuild.builds.list 可以列出构建和触发器
cloudbuild.builds.get 可以获取构建和触发器
cloudbuild.workerpools.use 可以使用专用池 在专用池中运行构建时必需。
logging.logEntries.create 可以写入日志 在 Cloud Logging 中创建和列出构建日志所需的权限。
logging.logEntries.list 可以列出日志
logging.views.access 可以查看日志
pubsub.topics.create 可以创建 Pub/Sub 主题 将构建更新推送到 Pub/Sub 时需要这些权限。
pubsub.topics.publish 可以发布到 Pub/Sub
remotebuildexecution.blobs.get 可以获取批准或拒绝构建的权限。 批准或拒绝待处理构建时必需
resourcemanager.projects.get 可以获取项目信息
resourcemanager.projects.list 可以列出项目
source.repos.get 可以从 Cloud Source Repositories 中的代码库读取源代码 需要这些权限才能执行以下操作:
  • 使用 Bitbucket 和 Cloud Source Repositories 触发器。
  • 从 Cloud Source Repositories 中拉取源代码。
source.repos.list 可以列出 Cloud Source Repositories 中的代码库
storage.buckets.create 可以创建 Cloud Storage 存储分区 需要这些权限才能执行以下操作:
  • 在 Container Registry 中存储和获取映像( 已废弃)。
  • 在 Cloud Storage 中存储和获取工件。
  • 通过 gcloud builds submit 手动提交构建。
  • 将构建日志存储在用户创建的日志存储桶中。
storage.buckets.get 可以获取 Cloud Storage 存储分区
storage.buckets.list 可以列出 Cloud Storage 存储分区
storage.objects.list 可以列出 Cloud Storage 对象
storage.objects.update 可以更新 Cloud Storage 对象
storage.objects.create 可以写入 Cloud Storage 对象
storage.objects.delete 可以删除 Cloud Storage 对象
storage.objects.get 可以读取 Cloud Storage 对象
artifactregistry.repositories.uploadArtifacts 可以将工件上传到 Artifact Registry 中的代码库 管理 Artifact Registry 中的工件是必需的。
artifactregistry.repositories.downloadArtifacts 可以从 Artifact Registry 中的代码库下载工件
artifactregistry.aptartifacts.create 可以将 Apt 工件上传到 Artifact Registry
artifactregistry.dockerimages.get 可以从 Artifact Registry 获取 Docker 映像
artifactregistry.dockerimages.list 可以列出存储在 Artifact Registry 中的 Docker 映像
artifactregistry.kfpartifacts.create 可以将 KFP 工件上传到 Artifact Registry
artifactregistry.locations.get 可以获取 Artifact Registry 中某个资源的位置相关信息
artifactregistry.locations.list 可以列出 Artifact Registry 支持的位置
artifactregistry.mavenartifacts.get 可以从 Artifact Registry 获取 Maven 软件包
artifactregistry.mavenartifacts.list 可以从 Artifact Registry 中列出 Maven 软件包
artifactregistry.npmpackages.get 可以从 Artifact Registry 获取 npm 软件包
artifactregistry.npmpackages.list 可以从 Artifact Registry 中列出 npm 软件包
artifactregistry.projectsettings.get 可以从 Artifact Registry 获取项目设置
artifactregistry.pythonpackages.get 可以从 Artifact Registry 获取 Python 软件包
artifactregistry.pythonpackages.list 可以从 Artifact Registry 中列出 Python 软件包
artifactregistry.yumartifacts.create 可以将 Yum 工件上传到 Artifact Registry
artifactregistry.repositories.createOnPush 可以在首次将映像推送到项目中的 gcr.io 主机名时,在 Artifact Registry 中创建 gcr.io 代码库。
artifactregistry.repositories.get 可以从 Artifact Registry 获取代码库
artifactregistry.repositories.list 可以列出 Artifact Registry 中的代码库
artifactregistry.repositories.listEffectiveTags 可以列出 Artifact Registry 中工件的标记 管理 Artifact Registry 中工件的标记所需的权限。
artifactregistry.repositories.listTagBindings 可以列出 Artifact Registry 中工件的标记绑定信息
artifactregistry.tags.create 可以在 Artifact Registry 中创建标记
artifactregistry.tags.get 可以从 Artifact Registry 获取标记
artifactregistry.tags.list 可以列出 Artifact Registry 中的标记
artifactregistry.tags.update 可以更新 Artifact Registry 中的标记
artifactregistry.versions.list 可以列出 Artifact Registry 中的版本
artifactregistry.versions.get 可以获取 Artifact Registry 中的版本
containeranalysis.occurrences.create 可以创建 Artifact Analysis 发生实例 Cloud Build 服务账号不使用这些权限,但它们是为了向后兼容性而添加的。
containeranalysis.occurrences.delete 可以删除 Artifact Analysis 发生实例
containeranalysis.occurrences.get 可以获取 Artifact Analysis 发生实例
containeranalysis.occurrences.list 可以列出 Artifact Analysis 发生实例
containeranalysis.occurrences.update 可以更新 Artifact Analysis 事件

构建触发器

默认情况下,构建触发器使用 Cloud Build 服务账号执行构建。或者,您可以配置构建触发器以使用您选择的服务账号执行构建。您可以使用不同的服务账号配置每个触发器。

在选择要为构建触发器指定的服务账号时,请牢记以下注意事项:

  • 默认 Cloud Build 服务账号:具有 Cloud Build Editor 角色的任何用户都可以创建并直接运行触发器。例如,用户可以手动运行触发器。任何用户都可以间接运行触发器。例如,用户可以在将新源代码推送到连接的代码库时间接调用触发器。具有 Cloud Build Editor 角色的任何用户都可以更新触发器,只要先前的服务账号和触发器上指定的新服务账号是默认 Cloud Build 账号即可。

  • 用户指定的服务帐号:具有 Cloud Build Editor 角色且拥有 iam.serviceAccounts.actAs 权限的任何用户都可以创建并直接运行触发器。例如,用户可以手动运行触发器。任何用户也可以间接运行触发器。例如,用户可以在将新来源推送到已连接的代码库时间接调用触发器。任何具有 Cloud Build Editor 角色的用户都可以更新触发器,前提是他们对之前配置的服务帐号和触发器上指定的新服务帐号拥有 iam.serviceAccounts.actAs 权限。如需授予用户此权限,您可以授予他们具有相应权限的预定义角色,如 Service Account User 角色 (roles/iam.serviceAccountUser)。或者,您也可以创建具有 iam.serviceAccounts.actAs 权限的自定义 IAM 角色,然后将该角色授予用户。如需详细了解服务帐号权限,请参阅服务帐号身份验证的角色

此外,默认的 Cloud Build 服务账号和用户指定的服务账号可以为使用触发器调用构建的用户提供提升的构建时间权限。使用与默认 Cloud Build 服务账号关联的构建触发器时,请注意以下安全隐患:

  • 如果用户无权访问您的 Google Cloud 项目,但对与项目中的构建触发器相关联的代码库具有写入权限,则将有权更改正在构建的代码。
  • 如果您使用的是 GitHub 拉取请求触发器,则有权读取代码库的任何用户都可以提交拉取请求,这可能会触发其中包含拉取请求中的代码更改的构建。要停用此行为,您可以在创建 GitHub 拉取请求触发器时选择评论控制选项。选择此选项可确保仅在代码库所有者或协作者评论 /gcbrun 时才启动构建。如需了解如何将评论控制GitHub 触发器配合使用,请参阅创建 GitHub 触发器

限制

如果您需要使用 ID 令牌在服务之间进行身份验证,则必须使用用户指定的服务帐号运行构建。默认的 Cloud Build 服务帐号不能用于生成 ID 令牌。

例如,如果您使用 Cloud Functions、Cloud Run 或 App Engine 等无服务器平台应用,并且希望从 Cloud Build 调用您的应用,则需要用户指定的服务帐号,该帐号配置了服务到服务身份验证所需的权限。

有关说明,请参阅授权服务间访问

后续步骤