機構政策服務可讓您透過程式以集中方式控管機構資源。身為機構政策管理員,您可以定義機構政策,也就是一組稱為限制的限制,適用於 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build API.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build API.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
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.bucket
resource.source.storageSource.object
resource.source.storageSource.generation
resource.source.storageSource.sourceFetcher
resource.source.repoSource.projectId
resource.source.repoSource.repoName
resource.source.repoSource.branchName
resource.source.repoSource.tagName
resource.source.repoSource.commitSha
resource.source.repoSource.dir
resource.source.repoSource.invertRegex
resource.source.repoSource.substitutions
resource.source.gitSource.url
resource.source.gitSource.dir
resource.source.gitSource.revision
resource.source.storageSourceManifest.bucket
resource.source.storageSourceManifest.object
resource.source.storageSourceManifest.generation
resource.source.connectedRepository.repository
resource.source.connectedRepository.dir
resource.source.connectedRepository.revision
resource.source.developerConnectConfig.gitRepositoryLink
resource.source.developerConnectConfig.dir
resource.source.developerConnectConfig.revision
resource.steps.name
resource.steps.env
resource.steps.args
resource.steps.dir
resource.steps.id
resource.steps.waitFor
resource.steps.entryPoints
resource.steps.secretEnv
resource.steps.volumes.name
resource.steps.volumes.path
resource.steps.timeout
resource.steps.allowFailure
resource.steps.allowExitCodes
resource.steps.script
resource.steps.automapSubstitutions
resource.timeout
resource.images
resource.queueTtl
resource.artifacts.images
resource.artifacts.objects.location
resource.artifacts.objects.paths
resource.logsBucket
resource.options.sourceProvenanceHash
resource.options.requestedVerifyOption
resource.options.machineType
resource.options.diskSizeGb
resource.options.substitutionOption
resource.options.dynamicSubstitutions
resource.options.autoMapSubstitutions
resource.options.logStreamingOption
resource.options.pool.name
resource.options.logging
resource.options.env
resource.options.secretEnv
resource.options.volumes.name
resource.options.volumes.path
resource.options.defaultLogsBucketBehavior
resource.substitutions
resource.tags
resource.secrets.kmsKeyName
resource.secrets.secretEnv
resource.serviceAccount
resource.availableSecrets.secretManager.versionName
resource.availableSecrets.secretManager.env
resource.availableSecrets.inline.kmsKeyName
resource.availableSecrets.inline.envMap
resource.gitConfig.proxySecretVersionName
resource.gitConfig.proxySslCaInfo
resource.gitConfig.http
- Cloud Build BitbucketServerConfig
resource.name
resource.hostUrl
resource.secrets.adminAccessTokenVersionName
resource.secrets.readAccessTokenVersionName
resource.secrets.webhookSecretVersionName
resource.username
resource.apiKey
resource.peeredNetwork
resource.sslCa
resource.peeredNetworkIpRange
- Cloud Build WorkerPool 設定
resource.displayName
resource.annotations
resource.privatePoolV1Config.workerConfig.machineType
resource.privatePoolV1Config.workerConfig.diskSizeGb
resource.privatePoolV1Config.networkConfig.peeredNetwork
resource.privatePoolV1Config.networkConfig.egressOption
resource.privatePoolV1Config.networkConfig.peeredNetworkIpRange
- Cloud Build BuildTrigger 設定
resource.tags
resource.resourceName
resource.description
resource.name
resource.tags
resource.triggerTemplate.projectId
resource.triggerTemplate.repoName
resource.triggerTemplate.branchName
resource.triggerTemplate.tagName
resource.triggerTemplate.commitSha
resource.triggerTemplate.dir
resource.triggerTemplate.invertRegex
resource.triggerTemplate.substitutions
resource.github.owner
resource.github.name
resource.github.enterpriseConfigResourceName
resource.pubsubConfig.topic
resource.pubsubConfig.serviceAccountEmail
resource.webhookConfig.secret
resource.bitbucketServerTriggerConfig.repoSlug
resource.bitbucketServerTriggerConfig.projectKey
resource.bitbucketServerTriggerConfig.pullRequest.branch
resource.bitbucketServerTriggerConfig.pullRequest.commentControl
resource.bitbucketServerTriggerConfig.pullRequest.invertRegex
resource.bitbucketServerTriggerConfig.push.branch
resource.bitbucketServerTriggerConfig.push.tag
resource.bitbucketServerTriggerConfig.push.invertRegex
resource.gitlabEnterpriseEventsConfig.projectNamespace
resource.gitlabEnterpriseEventsConfig.pullRequest.branch
resource.gitlabEnterpriseEventsConfig.pullRequest.commentControl
resource.gitlabEnterpriseEventsConfig.pullRequest.invertRegex
resource.gitlabEnterpriseEventsConfig.push.branch
resource.gitlabEnterpriseEventsConfig.push.tag
resource.gitlabEnterpriseEventsConfig.push.invertRegex
resource.gitlabEnterpriseEventsConfig.gitlabConfigResource
resource.disabled
resource.substitutions
resource.ignoredFiles
resource.includedFiles
resource.sourceToBuild.uri
resource.sourceToBuild.repository
resource.sourceToBuild.ref
resource.sourceToBuild.repoType
resource.sourceToBuild.githubEnterpriseConfig
resource.approvalConfig.approvalRequired
resource.filter
resource.serviceAccount
resource.eventType
resource.includeBuildLogs
resource.repositoryEventConfig.repository
resource.repositoryEventConfig.pullRequest.branch
resource.repositoryEventConfig.pullRequest.commentControl
resource.repositoryEventConfig.pullRequest.invertRegex
resource.repositoryEventConfig.push.branch
resource.repositoryEventConfig.push.tag
resource.repositoryEventConfig.push.invertRegex
- Cloud Build GitHubEnterpriseConfig 設定
resource.name
resource.hostUrl
resource.appId
resource.name
resource.webhookKey
resource.peeredNetwork
resource.secrets.privateKeyVersionName
resource.secrets.webhookSecretVersionName
resource.secrets.oauthSecretVersionName
resource.secrets.oauthClientIdVersionName
resource.displayName
resource.sslCa
- Cloud Build 連線設定
resource.name
resource.githubConfig.authorizerCredential.oauthTokenSecretVersionName
resource.githubConfig.appInstallationId
resource.githubEnterpriseConfig.hostUrl
resource.githubEnterpriseConfig.apiKey
resource.githubEnterpriseConfig.appId
resource.githubEnterpriseConfig.appSlug
resource.githubEnterpriseConfig.privateKeySecretVersion
resource.githubEnterpriseConfig.webhookSecretSecretVersion
resource.githubEnterpriseConfig.oauthSecretSecretVersion
resource.githubEnterpriseConfig.oauthClientIdSecretVersion
resource.githubEnterpriseConfig.authorizerCredential.oauthTokenSecretVersionName
resource.githubEnterpriseConfig.appInstallationId
resource.githubEnterpriseConfig.serviceDirectoryConfig.service
resource.githubEnterpriseConfig.sslCa
resource.gitlabConfig.hostUri
resource.gitlabConfig.webhookSecretSecretVersion
resource.gitlabConfig.readAuthorizerCredential.oauthTokenSecretVersionName
resource.gitlabConfig.authorizerCredential.oauthTokenSecretVersionName
resource.gitlabConfig.serviceDirectoryConfig.service
resource.gitlabConfig.sslCa
resource.gitlabConfig.serviceDirectoryConfig.service
resource.bitbucketDataCenterConfig.hostUri
resource.bitbucketDataCenterConfig.webhookSecretSecretVersion
resource.bitbucketDataCenterConfig.readAuthorizerCredential.oauthTokenSecretVersionName
resource.bitbucketDataCenterConfig.authorizerCredential.oauthTokenSecretVersionName
resource.bitbucketDataCenterConfig.sslCa
resource.bitbucketDataCenterConfig.serviceDirectoryConfig.service
resource.bitbucketCloudConfig.workspace
resource.bitbucketCloudConfig.webhookSecretSecretVersion
resource.bitbucketCloudConfig.readAuthorizerCredential.oauthTokenSecretVersionName
resource.bitbucketCloudConfig.authorizerCredential.oauthTokenSecretVersionName
- Cloud Build 存放區設定
resource.name
resource.remoteUri
resource.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".
後續步驟