視組織設定而定,Cloud Build 可能會使用 Compute Engine 預設服務帳戶或舊版 Cloud Build 服務帳戶,代表您執行建構作業。
預設服務帳戶的權限可能過於廣泛,超出您的用途所需。您可以遵循最小權限原則,強化資安態勢。根據這項原則,建議您建立自己的服務帳戶,代表您執行建構作業。這可降低設定錯誤或惡意使用者可能造成的影響。
如要瞭解如何授予或撤銷 Cloud Build 預設服務帳戶的權限,請參閱「設定 Cloud Build 預設服務帳戶的存取權」。
關於 Compute Engine 預設服務帳戶
貴組織的政策可能會將預設 Cloud Build 服務帳戶定義為 Compute Engine 預設服務帳戶。這個服務帳戶的電子郵件地址為 PROJECT_NUMBER-compute@developer.gserviceaccount.com。
如要瞭解 Compute Engine 預設服務帳戶,請參閱「Compute Engine 預設服務帳戶」。
關於舊版 Cloud Build 服務帳戶
組織政策可能會將預設 Cloud Build 服務帳戶定義為舊版服務帳戶。舊版 Cloud Build 服務帳戶的電子郵件地址為 PROJECT_NUMBER@cloudbuild.gserviceaccount.com
本節說明舊版 Cloud Build 服務帳戶預設具備的所有權限。
舊版 Cloud Build 服務帳戶的預設權限
如果專案設定允許使用舊版 Cloud Build 服務帳戶,則舊版服務帳戶會獲得專案資源的 Cloud Build 服務帳戶角色 (roles/cloudbuild.builds.builder)。這個角色包含多項權限,例如更新建構作業或寫入記錄檔。服務帳戶只會在執行建構作業時,視需要使用這些權限執行動作。舉例來說,如果建構作業設定為從 Artifact Registry 取得 Docker 映像檔,服務帳戶會使用 artifactregistry.dockerimages.get 權限執行這項操作。
如果您不打算在建構程序中執行某項動作,建議您撤銷服務帳戶的對應權限,以遵守最小權限原則。如果您使用自動建構觸發條件,舊版 Cloud Build 服務帳戶必須保留 cloudbuild.builds.create 權限,才能啟動觸發的建構作業。
下表列出 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 存放區中的原始碼 | 必要條件:
|
source.repos.list |
可列出 Cloud Source Repositories 中的存放區 | |
storage.buckets.create |
可建立 Cloud Storage bucket | 必要條件:
|
storage.buckets.get |
可取得 Cloud Storage bucket | |
storage.buckets.list |
可列出 Cloud Storage bucket | |
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 編輯者角色的任何使用者,都能建立並直接執行觸發條件。例如,使用者可以手動執行觸發條件。只要觸發條件使用 Cloud Build 舊版服務帳戶,具備 Cloud Build 編輯者角色的使用者都能更新觸發條件。
使用者指定的服務帳戶或 Compute Engine 預設服務帳戶:任何具備 Cloud Build 編輯者角色和
iam.serviceAccounts.actAs權限的使用者,都可以建立並直接執行觸發條件。例如,使用者可以手動執行觸發條件。只要具備 Cloud Build 編輯者角色,且對先前設定的服務帳戶和觸發條件指定的新服務帳戶,都擁有
iam.serviceAccounts.actAs權限,任何使用者都能更新觸發條件。如要將這項權限授予使用者,您可以授予具備該權限的預先定義角色,例如服務帳戶使用者角色 (roles/iam.serviceAccountUser)。或者,您也可以建立具備iam.serviceAccounts.actAs權限的自訂 IAM 角色,然後將該角色授予使用者。如要進一步瞭解服務帳戶權限,請參閱「服務帳戶驗證角色」。
觸發條件的建構時間權限
為自動建構觸發條件設定的服務帳戶,可為使用觸發條件叫用建構作業的使用者,提供更高的建構階段權限。這項規定適用於舊版服務帳戶和使用者指定的服務帳戶。使用建構觸發條件時,請注意下列安全性影響:
如果使用者無法存取您的 Google Cloud 專案,但對專案中與自動建構觸發條件相關聯的存放區具有寫入權限,則有權變更建構中的程式碼。舉例來說,使用者將新的原始碼推送至連結的存放區時,可以間接叫用觸發條件。
如果您使用 GitHub 提取要求觸發條件,凡是有權讀取這個存放區的使用者,都能提交提取要求,並有可能藉此觸發建構作業,部署透過提取要求提出的程式碼變更。如要停用這項行為,請在建立 GitHub 提取要求觸發條件時,選擇「Comment control」(留言控制) 選項。選取這個選項,即可確保只有在存放區擁有者或協作者留言
/gcbrun時,才會啟動建構作業。如要瞭解如何搭配使用留言控制項和 GitHub 觸發條件,請參閱「建立 GitHub 觸發條件」。
舊版 Cloud Build 服務帳戶的限制
如需使用 ID 權杖在服務之間進行驗證,您必須使用使用者指定的服務帳戶執行建構作業。您無法使用舊版 Cloud Build 服務帳戶產生 ID 權杖。
舉例來說,如果您使用 Cloud Run functions、Cloud Run 或 App Engine 等無伺服器平台應用程式,並想從 Cloud Build 叫用應用程式,就必須使用使用者指定的服務帳戶,並設定服務對服務驗證所需的權限。
如需操作說明,請參閱「授權服務對服務存取權」。
您無法在舊版服務帳戶上新增 IAM 政策繫結。舉例來說,您無法建立 IAM 政策繫結,將舊版服務帳戶的
roles/iam.serviceAccountTokenCreator角色授予其他服務帳戶。
後續步驟
- 瞭解使用者指定的服務帳戶。
- 瞭解如何設定 Cloud Build 預設服務帳戶的存取權。
- 瞭解如何設定 Cloud Build 資源的存取權。
- 瞭解查看建構記錄所需的權限。