借助组织政策服务,您可以对组织的资源进行程序化集中控制。作为组织政策管理员,您可以定义组织政策,这是一组称为限制条件的限制,会应用于 Google Cloud 资源层次结构中的 Cloud Build 资源及其后代。您可以在组织、文件夹或项目级强制执行组织政策。
组织政策服务为各种 Cloud Build 服务提供预定义限制条件。但是,如果您想要更精细地控制和自定义组织政策中受限的特定字段,还可以创建自定义组织政策。
优势
您可以使用自定义组织政策来允许或拒绝特定 Cloud Build 资源。例如,如果创建或更新 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.
-
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.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 请确保您知道自己的组织 ID。
所需的角色
如需获得管理组织政策所需的权限,请让您的管理员为您授予组织资源的 Organization Policy Administrator (roles/orgpolicy.policyAdmin
) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
您还需要向用户账号添加创建 Cloud Build 所需的角色。请参阅配置对 Cloud Build 资源的访问权限。如需详细了解与 Cloud Build 关联的 IAM 角色,请参阅 IAM 角色和权限。
创建自定义限制条件
您可以使用 YAML 文件来定义受限制条件约束的资源、方法、条件和操作,从而创建自定义限制条件。这些参数特定于您要强制执行组织政策的服务。自定义限制条件的条件必须使用通用表达式语言进行定义。请参阅介绍通用表达式语言 (CEL) 的 GitHub 页面。如需详细了解如何使用 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
替换以下内容:
ORGANIZATION_ID
:您的组织 ID,例如123456789
。CONSTRAINT_NAME
:新的自定义限制条件的名称。 自定义限制条件必须以custom.
开头,只能包含大写字母、小写字母或数字,例如custom.enableBuildTrigger
。该字段的长度上限为 70 个字符,不计算前缀(例如organizations/123456789/customConstraints/custom.allowConstraint
)。RESOURCE_NAME
:包含要限制的对象和字段的 Cloud Build 资源的名称(而非 URI)。例如BuildTrigger
。CONDITION
:针对受支持的服务资源的表示法编写的 CEL 条件。此字段的长度上限为 1,000 个字符。 如需详细了解可用于针对其编写条件的资源,请参阅支持的资源。 例如"resource.github.name.contains('cloudbuild')"
。ACTION
:满足condition
时要执行的操作。可以是ALLOW
或DENY
。DISPLAY_NAME
:限制条件的直观易记名称。 此字段的最大长度为 200 个字符。DESCRIPTION
:直观易懂的限制条件说明,在违反政策时显示为错误消息。 此字段的长度上限为 2,000 个字符。
如需详细了解如何创建自定义限制条件,请参阅创建和管理自定义组织政策。
设置自定义限制条件
为新的自定义限制条件创建 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 资源。控制台
- 在 Google Cloud 控制台中,转到组织政策页面。
- 在项目选择器中,选择要设置组织政策的项目。
- 从组织政策页面上的列表中选择您的限制条件,以查看该限制条件的政策详情页面。
- 如需为该资源配置组织政策,请点击管理政策。
- 在修改政策页面,选择覆盖父级政策。
- 点击添加规则。
- 在强制执行部分中,选择开启还是关闭此组织政策的强制执行。
- (可选)如需使组织政策成为基于某个标记的条件性政策,请点击添加条件。请注意,如果您向组织政策添加条件规则,则必须至少添加一个无条件规则,否则无法保存政策。如需了解详情,请参阅设置带有标记的组织政策。
- 如果是自定义限制条件,您可以点击测试更改来模拟此组织政策的效果。如需了解详情,请参阅使用 Policy Simulator 测试组织政策更改。
- 若要完成并应用组织政策,请点击设置政策。该政策最长需要 15 分钟才能生效。
gcloud
如需创建强制执行布尔值限制条件的组织政策,请创建引用该限制条件的 YAML 政策文件:
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME spec: rules: - enforce: true
请替换以下内容:
-
PROJECT_ID
:要对其实施限制条件的项目。 -
CONSTRAINT_NAME
:您为自定义限制条件定义的名称。例如,custom.enableBuildTrigger
。
如需强制执行包含限制条件的组织政策,请运行以下命令:
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 自定义约束条件字段。
请注意,系统不会对由触发器触发的 build 强制执行对 build 的限制。
- Cloud Build 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 Connection 配置
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
常见用例的自定义组织政策示例
下表提供了一些可能对您有用的自定义组织政策的语法:
说明 | 限制条件语法 |
---|---|
仅允许创建监听包含“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". |
ORGANIZATION_ID
:您的组织 ID,例如123456789
。