預設 Cloud Build 服務帳戶

視貴機構的設定而定,Cloud Build 可能會使用 Compute Engine 預設服務帳戶或舊版 Cloud Build 服務帳戶,代表您執行建構作業。

預設服務帳戶的權限可能對您的用途來說過於廣泛。您可以遵循最低權限原則,強化安全防護機制。根據這項原則,建議您建立自己的服務帳戶,以便代表您執行建構作業。這麼做可以降低設定錯誤或惡意使用者造成的潛在影響。

如要瞭解如何授予或撤銷 Cloud Build 預設服務帳戶的權限,請參閱「設定 Cloud Build 預設服務帳戶的存取權」。

關於 Compute Engine 預設服務帳戶

貴機構的政策可能會將預設 Cloud Build 服務帳戶定義為 Compute Engine 預設服務帳戶。這個服務帳戶的電子郵件地址為
PROJECT_NUMBER[email protected]

如要瞭解 Compute Engine 預設服務帳戶,請參閱「Compute Engine 預設服務帳戶」。

關於舊版 Cloud Build 服務帳戶

您的組織政策可能會將預設 Cloud Build 服務帳戶定義為舊版服務帳戶。舊版 Cloud Build 服務帳戶的電子郵件地址為
PROJECT_NUMBER@cloudbuild.gserviceaccount.com

本節說明舊版 Cloud Build 服務帳戶預設具備的所有權限。

舊版 Cloud Build 服務帳戶的預設權限

如果專案設定允許使用舊版 Cloud Build 服務帳戶,系統會為專案中的資源授予舊版服務帳戶 Cloud Build Service Account 角色 (roles/cloudbuild.builds.builder)。這個角色包含多項權限,例如更新版本或寫入記錄的權限。服務帳戶只會在執行建構作業時,在執行動作時需要使用這些權限。舉例來說,如果建構作業已設定為使用 artifactregistry.dockerimages.get 權限,服務帳戶就會透過該權限從 Artifact Registry 取得 Docker 映像檔。

如果您不打算在建構程序中執行任何動作,建議您撤銷服務帳戶的對應權限,以符合最低權限安全原則

下表列出 Cloud Build 服務帳戶角色 (roles/cloudbuild.builds.builder) 所含權限,以及舊版 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可取得核准或拒絕版本的存取權。rowspan=3>必須核准或拒絕待處理的建構作業
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 值區適用對象:
  • 在 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可刪除成果分析例項
containeranalysis.occurrences.get可取得 Artifact Analysis 例項
containeranalysis.occurrences.list可列出 Artifact Analysis 發生頻率
containeranalysis.occurrences.update可更新 Artifact Analysis 發生頻率

自動建構觸發條件

建立建構觸發事件時,您必須選擇用於執行建構作業的服務帳戶。您可以為每個觸發條件設定不同的服務帳戶。唯一的例外狀況是,如果專案已啟用舊版 Cloud Build 服務帳戶,在未選取其他帳戶的情況下,建構觸發事件預設會使用舊版服務帳戶。

使用者對觸發事件的存取權

觸發事件的使用者存取權取決於為觸發事件設定的服務帳戶類型:

  • 舊版 Cloud Build 服務帳戶 (如果已啟用):任何具備 Cloud Build 編輯者角色的使用者都可以建立並直接執行觸發事件。舉例來說,使用者可以手動執行觸發條件。只要觸發條件使用 Cloud Build 舊版服務帳戶,具備 Cloud Build 編輯者角色的使用者就能更新觸發條件。

  • 使用者指定的服務帳戶或 Compute Engine 預設服務帳戶:任何具備 Cloud Build 編輯者角色且具有 iam.serviceAccounts.actAs 權限的使用者,都可以建立並直接執行觸發事件。舉例來說,使用者可以手動執行觸發條件。

    只要使用者具備先前設定的服務帳戶和觸發事件中指定的新服務帳戶的 iam.serviceAccounts.actAs 權限,就能更新觸發事件。如要授予使用者這項權限,您可以授予他們具有權限的預先定義角色,例如「服務帳戶使用者」角色 (roles/iam.serviceAccountUser)。或者,您也可以建立具有 iam.serviceAccounts.actAs 權限的自訂 IAM 角色,然後將該角色授予使用者。如要進一步瞭解服務帳戶權限,請參閱「服務帳戶驗證的角色」。

觸發條件的建構時間權限

為建構觸發條件設定的服務帳戶,可為使用觸發條件叫用建構作業的使用者提供提升的建構時間權限。這項做法適用於舊版服務帳戶和使用者指定的服務帳戶。使用建構觸發事件時,請留意以下安全性影響:

  • 如果使用者沒有您的 Google Cloud 專案存取權,但有權存取與專案中建構觸發事件相關聯的存放區,則可變更正在建構的程式碼。舉例來說,當使用者將新原始碼推送至已連結的存放區時,可以間接叫用觸發條件。

  • 如果您使用 提取要求觸發條件,凡是有權讀取這個存放區的使用者,都能提交提取要求,並有可能藉此觸發建構作業,部署透過提取要求提出的程式碼變更。建立 提取要求觸發條件時,選擇「註解控制項」選項,即可停用這項行為。選取這個選項可確保只有在存放區擁有者或協作者留言 /gcbrun 時才會啟動建構作業。如要瞭解如何搭配使用 留言控制項觸發條件,請參閱「建立 觸發條件」一文。

舊版 Cloud Build 服務帳戶的限制

  • 如果您需要使用 ID 權杖在服務之間進行驗證,則必須使用使用者指定的服務帳戶執行建構作業。您無法使用舊版 Cloud Build 服務帳戶產生 ID 權杖。

    舉例來說,如果您使用 Cloud Run 函式、Cloud Run 或 App Engine 等無伺服器平台應用程式,且想要從 Cloud Build 叫用應用程式,就必須使用使用者指定的服務帳戶,並為服務間驗證設定必要權限。

    如需操作說明,請參閱「授權服務對服務存取權」。

  • 您無法在舊版服務帳戶上新增 IAM 政策繫結。舉例來說,您無法建立 IAM 政策繫結,將 roles/iam.serviceAccountTokenCreator 角色授予另一個服務帳戶,這會影響舊版服務帳戶。

後續步驟