機構政策服務可讓您透過程式以集中方式控管機構資源。身為機構政策管理員,您可以定義機構政策,也就是一組稱為限制的限制,適用於 Google Cloud 資源階層中的 Cloud Build 資源和這些資源的子系。您可以在機構、資料夾或專案層級強制執行組織政策。
機構政策服務為各種 Cloud Build 服務提供預先定義的限制。不過,如要進一步自訂機構政策中受限制的特定欄位,也可以建立自訂機構政策。
優點
您可以透過自訂機構政策允許或拒絕特定 Cloud Build 資源。舉例來說,如果建立或更新建構觸發程序的要求,無法滿足機構政策設定的自訂限制驗證,要求就會失敗,並向呼叫端傳回錯誤。
政策繼承
根據預設,機構政策會由您強制執行政策的資源子系繼承。舉例來說,如果您在資料夾上強制執行政策,Cloud Build 會對該資料夾中的所有專案強制執行政策。如要進一步瞭解這項行為及如何變更,請參閱「階層評估規則」。
定價
機構政策服務 (包括預先定義和自訂機構政策) 免費提供。
事前準備
- 設定專案
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
gcloud init - 請確認您知道機構 ID。
ORGANIZATION_ID:您的機構 ID,例如123456789。CONSTRAINT_NAME:新自訂限制的名稱。自訂限制必須以custom.開頭,且只能包含大寫英文字母、小寫英文字母或數字,例如custom.enableBuildTrigger。這個欄位的長度上限為 70 個字元,不含前置字元,例如organizations/123456789/customConstraints/custom.allowConstraint。RESOURCE_NAME:Cloud Build 資源的名稱 (而非 URI),其中包含您要限制的物件和欄位。例如:BuildTrigger。CONDITION:針對支援服務資源的代表項目編寫的 CEL 條件。這個欄位的長度上限為 1000 個字元。如要進一步瞭解可編寫條件的資源,請參閱「支援的資源」。例如:"resource.github.name.contains('cloudbuild')"。ACTION:如果符合condition,則要採取的動作。可以是ALLOW或DENY。DISPLAY_NAME:限制條件的易記名稱。這個欄位的長度上限為 200 個字元。DESCRIPTION:違反政策時,要以錯誤訊息形式顯示的限制說明。這個欄位的長度上限為 2000 個字元。- 前往 Google Cloud 控制台的「Organization policies」(機構政策) 頁面。
- 在專案選擇工具中,選取要設定機構政策的專案。
- 在「Organization policies」(機構政策) 頁面上的清單中選取限制條件,即可查看該限制條件的「Policy details」(政策詳情) 頁面。
- 如要設定資源的機構政策,請按一下「管理政策」。
- 在「編輯政策」頁面中,選取「覆寫上層政策」。
- 按一下「新增規則」。
- 在「Enforcement」(強制執行) 區段中,選取是否要強制執行這項機構政策。
- 選用:如要根據標記設定機構政策條件,請按一下「新增條件」。請注意,如果為組織政策新增條件式規則,您必須至少新增一項無條件規則,否則無法儲存政策。詳情請參閱「使用標記設定組織政策」。
- 按一下「測試變更」,模擬機構政策的影響。舊版管理限制不支援政策模擬。詳情請參閱「 使用 Policy Simulator 測試組織政策變更」。
- 如要完成並套用機構政策,請按一下「設定政策」。這項政策最多需要 15 分鐘才會生效。
-
PROJECT_ID:您要強制執行限制的專案。 -
CONSTRAINT_NAME:您為自訂限制定義的名稱。例如:custom.enableBuildTrigger。 - Cloud Build 建構設定
resource.source.storageSource.bucketresource.source.storageSource.objectresource.source.storageSource.generationresource.source.storageSource.sourceFetcherresource.source.repoSource.projectIdresource.source.repoSource.repoNameresource.source.repoSource.branchNameresource.source.repoSource.tagNameresource.source.repoSource.commitSharesource.source.repoSource.dirresource.source.repoSource.invertRegexresource.source.repoSource.substitutionsresource.source.gitSource.urlresource.source.gitSource.dirresource.source.gitSource.revisionresource.source.storageSourceManifest.bucketresource.source.storageSourceManifest.objectresource.source.storageSourceManifest.generationresource.source.connectedRepository.repositoryresource.source.connectedRepository.dirresource.source.connectedRepository.revisionresource.source.developerConnectConfig.gitRepositoryLinkresource.source.developerConnectConfig.dirresource.source.developerConnectConfig.revisionresource.steps.nameresource.steps.envresource.steps.argsresource.steps.dirresource.steps.idresource.steps.waitForresource.steps.entryPointsresource.steps.secretEnvresource.steps.volumes.nameresource.steps.volumes.pathresource.steps.timeoutresource.steps.allowFailureresource.steps.allowExitCodesresource.steps.scriptresource.steps.automapSubstitutionsresource.timeoutresource.imagesresource.queueTtlresource.artifacts.imagesresource.artifacts.objects.locationresource.artifacts.objects.pathsresource.logsBucketresource.options.sourceProvenanceHashresource.options.requestedVerifyOptionresource.options.machineTyperesource.options.diskSizeGbresource.options.substitutionOptionresource.options.dynamicSubstitutionsresource.options.autoMapSubstitutionsresource.options.logStreamingOptionresource.options.pool.nameresource.options.loggingresource.options.envresource.options.secretEnvresource.options.volumes.nameresource.options.volumes.pathresource.options.defaultLogsBucketBehaviorresource.substitutionsresource.tagsresource.secrets.kmsKeyNameresource.secrets.secretEnvresource.serviceAccountresource.availableSecrets.secretManager.versionNameresource.availableSecrets.secretManager.envresource.availableSecrets.inline.kmsKeyNameresource.availableSecrets.inline.envMapresource.gitConfig.proxySecretVersionNameresource.gitConfig.proxySslCaInforesource.gitConfig.http
- Cloud Build BitbucketServerConfig
resource.nameresource.hostUrlresource.secrets.adminAccessTokenVersionNameresource.secrets.readAccessTokenVersionNameresource.secrets.webhookSecretVersionNameresource.usernameresource.apiKeyresource.peeredNetworkresource.sslCaresource.peeredNetworkIpRange
- Cloud Build WorkerPool 設定
resource.displayNameresource.annotationsresource.privatePoolV1Config.workerConfig.machineTyperesource.privatePoolV1Config.workerConfig.diskSizeGbresource.privatePoolV1Config.networkConfig.peeredNetworkresource.privatePoolV1Config.networkConfig.egressOptionresource.privatePoolV1Config.networkConfig.peeredNetworkIpRange
- Cloud Build BuildTrigger 設定
resource.tagsresource.resourceNameresource.descriptionresource.nameresource.tagsresource.triggerTemplate.projectIdresource.triggerTemplate.repoNameresource.triggerTemplate.branchNameresource.triggerTemplate.tagNameresource.triggerTemplate.commitSharesource.triggerTemplate.dirresource.triggerTemplate.invertRegexresource.triggerTemplate.substitutionsresource.github.ownerresource.github.nameresource.github.enterpriseConfigResourceNameresource.pubsubConfig.topicresource.pubsubConfig.serviceAccountEmailresource.webhookConfig.secretresource.bitbucketServerTriggerConfig.repoSlugresource.bitbucketServerTriggerConfig.projectKeyresource.bitbucketServerTriggerConfig.pullRequest.branchresource.bitbucketServerTriggerConfig.pullRequest.commentControlresource.bitbucketServerTriggerConfig.pullRequest.invertRegexresource.bitbucketServerTriggerConfig.push.branchresource.bitbucketServerTriggerConfig.push.tagresource.bitbucketServerTriggerConfig.push.invertRegexresource.gitlabEnterpriseEventsConfig.projectNamespaceresource.gitlabEnterpriseEventsConfig.pullRequest.branchresource.gitlabEnterpriseEventsConfig.pullRequest.commentControlresource.gitlabEnterpriseEventsConfig.pullRequest.invertRegexresource.gitlabEnterpriseEventsConfig.push.branchresource.gitlabEnterpriseEventsConfig.push.tagresource.gitlabEnterpriseEventsConfig.push.invertRegexresource.gitlabEnterpriseEventsConfig.gitlabConfigResourceresource.disabledresource.substitutionsresource.ignoredFilesresource.includedFilesresource.sourceToBuild.uriresource.sourceToBuild.repositoryresource.sourceToBuild.refresource.sourceToBuild.repoTyperesource.sourceToBuild.githubEnterpriseConfigresource.approvalConfig.approvalRequiredresource.filterresource.serviceAccountresource.eventTyperesource.includeBuildLogsresource.repositoryEventConfig.repositoryresource.repositoryEventConfig.pullRequest.branchresource.repositoryEventConfig.pullRequest.commentControlresource.repositoryEventConfig.pullRequest.invertRegexresource.repositoryEventConfig.push.branchresource.repositoryEventConfig.push.tagresource.repositoryEventConfig.push.invertRegex
- Cloud Build GitHubEnterpriseConfig 設定
resource.nameresource.hostUrlresource.appIdresource.nameresource.webhookKeyresource.peeredNetworkresource.secrets.privateKeyVersionNameresource.secrets.webhookSecretVersionNameresource.secrets.oauthSecretVersionNameresource.secrets.oauthClientIdVersionNameresource.displayNameresource.sslCa
- Cloud Build 連線設定
resource.nameresource.githubConfig.authorizerCredential.oauthTokenSecretVersionNameresource.githubConfig.appInstallationIdresource.githubEnterpriseConfig.hostUriresource.githubEnterpriseConfig.apiKeyresource.githubEnterpriseConfig.appIdresource.githubEnterpriseConfig.appSlugresource.githubEnterpriseConfig.privateKeySecretVersionresource.githubEnterpriseConfig.webhookSecretSecretVersionresource.githubEnterpriseConfig.oauthSecretSecretVersionresource.githubEnterpriseConfig.oauthClientIdSecretVersionresource.githubEnterpriseConfig.authorizerCredential.oauthTokenSecretVersionNameresource.githubEnterpriseConfig.appInstallationIdresource.githubEnterpriseConfig.serviceDirectoryConfig.serviceresource.githubEnterpriseConfig.sslCaresource.gitlabConfig.hostUriresource.gitlabConfig.webhookSecretSecretVersionresource.gitlabConfig.readAuthorizerCredential.oauthTokenSecretVersionNameresource.gitlabConfig.authorizerCredential.oauthTokenSecretVersionNameresource.gitlabConfig.serviceDirectoryConfig.serviceresource.gitlabConfig.sslCaresource.gitlabConfig.serviceDirectoryConfig.serviceresource.bitbucketDataCenterConfig.hostUriresource.bitbucketDataCenterConfig.webhookSecretSecretVersionresource.bitbucketDataCenterConfig.readAuthorizerCredential.oauthTokenSecretVersionNameresource.bitbucketDataCenterConfig.authorizerCredential.oauthTokenSecretVersionNameresource.bitbucketDataCenterConfig.sslCaresource.bitbucketDataCenterConfig.serviceDirectoryConfig.serviceresource.bitbucketCloudConfig.workspaceresource.bitbucketCloudConfig.webhookSecretSecretVersionresource.bitbucketCloudConfig.readAuthorizerCredential.oauthTokenSecretVersionNameresource.bitbucketCloudConfig.authorizerCredential.oauthTokenSecretVersionName
- Cloud Build 存放區設定
resource.nameresource.remoteUriresource.annotations
ORGANIZATION_ID:您的機構 ID,例如123456789。
必要的角色
如要取得管理機構政策所需的權限,請要求管理員為您授予機構資源的機構政策管理員 (
roles/orgpolicy.policyAdmin) 身分與存取權管理角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。您也必須將建立 Cloud Build 的必要角色新增至使用者帳戶。請參閱「設定 Cloud Build 資源的存取權」。如要進一步瞭解與 Cloud Build 相關聯的 IAM 角色,請參閱「IAM 角色和權限」。
建立自訂限制
您可以透過 YAML 檔案定義受限的資源、方法、條件和動作,藉此建立自訂限制。這些限制會因您要強制執行機構政策的服務而異。自訂限制的條件必須使用一般運算語言定義。請參閱 GitHub 頁面,瞭解一般運算語言 (CEL)。如要進一步瞭解如何使用 CEL 在自訂限制中建構條件,請參閱「建立及管理自訂限制」一文的 CEL 一節。
使用下列範本為自訂限制建立 YAML 檔案:
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME resourceTypes: - cloudbuild.googleapis.com/RESOURCE_NAME methodTypes: - CREATE - UPDATE condition: "CONDITION" actionType: ACTION displayName: DISPLAY_NAME description: DESCRIPTION更改下列內容:
如要進一步瞭解如何建立自訂限制,請參閱「建立及管理自訂機構政策」。
設定自訂限制
為新的自訂限制建立 YAML 檔案後,您必須進行設定,才能在貴機構的機構政策中使用該檔案。如要設定自訂限制,請使用gcloud org-policies set-custom-constraint指令: 將gcloud org-policies set-custom-constraint CONSTRAINT_PATH
CONSTRAINT_PATH替換為自訂限制檔案的完整路徑。例如:/home/user/customconstraint.yaml。 完成後,自訂限制就會顯示在 Google Cloud 機構政策清單中,做為機構政策使用。如要確認自訂限制條件是否存在,請使用gcloud org-policies list-custom-constraints指令: 將gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
ORGANIZATION_ID替換為機構資源的 ID。 詳情請參閱「查看組織政策」。強制執行自訂機構政策
如要強制執行限制,請建立參照該限制的機構政策,然後將該政策套用至 Google Cloud 資源。控制台
gcloud
如要建立含有布林值規則的機構政策,請建立參照限制的政策 YAML 檔案:
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME spec: rules: - enforce: true
取代下列項目:
如要強制執行包含限制的機構政策,請執行下列指令:
gcloud org-policies set-policy POLICY_PATH
將
POLICY_PATH替換為機構政策 YAML 檔案的完整路徑。這項政策最多需要 15 分鐘才會生效。測試自訂限制
如要測試自訂限制,請執行 gcloud 指令,嘗試完成受限制封鎖的工作。
舉例來說,假設限制條件規定 GitHub 觸發條件名稱必須包含「cloudbuild」。您可以執行
gcloud builds triggers create github指令,並使用其他觸發條件名稱,測試這項限制,如下列程式碼片段所示:gcloud builds triggers create github --name=github-trigger \ --repo-owner=some-owner \ --repo-name=some-repo \ --branch-pattern=main \ --build-config=cloudbuild.yaml \ --project=my-project \輸出結果會與下列內容相似:
Operation denied by custom org policies: ["customConstraints/custom.enableBuildTrigger": "GitHub trigger name must include "cloudbuild"."]Cloud Build 支援的資源和作業
建立或更新 Cloud Build 資源時,可以使用下列 Cloud Build 自訂限制欄位。
請注意,系統不會對由觸發程序觸發的建構作業強制執行建構作業限制。
常見用途的自訂機構政策範例
下表提供一些實用的自訂機構政策語法:
說明 限制語法 只允許建立監聽含有「cloud-builds」的主題的 Pub/Sub 觸發程序 name: organizations/ORGANIZATION_ID/customConstraints/custom.enableCloudBuildPubsubTrigger resourceTypes: - cloudbuild.googleapis.com/BuildTrigger methodTypes: - CREATE condition: "resource.pubsubConfig != null && resource.pubsubConfig.topic.contains('cloud-builds')" actionType: ALLOW displayName: Enable creating Pub/Sub trigger that listens on topics that contain "cloud-builds". description: Only allow creating Pub/Sub trigger that listens on topics that contain "cloud-builds".
後續步驟