使用自定义限制条件管理 Integration Connectors 资源
本页面介绍了如何使用组织政策服务自定义限制条件来限制对以下 Google Cloud 资源执行的特定操作:
connectors.googleapis.com/Connectionconnectors.googleapis.com/EndpointAttachmentconnectors.googleapis.com/ManagedZoneconnectors.googleapis.com/EventSubscriptionconnectors.googleapis.com/RegionalSettings
如需详细了解组织政策,请参阅自定义组织政策。
组织政策和限制条件简介
借助 Google Cloud 组织政策服务,您可以对组织的资源进行程序化集中控制。作为组织政策管理员,您可以定义组织政策,这是一组称为限制条件的限制,会应用于 Google Cloud 资源层次结构中的Google Cloud 资源及其后代。您可以在组织、文件夹或项目级强制执行组织政策。
组织政策为各种 Google Cloud 服务提供内置的托管式限制。但是,如果您想要更精细地控制和自定义组织政策中受限的特定字段,还可以创建自定义限制条件并在组织政策中使用这些自定义限制条件。
政策继承
如果您对资源强制执行政策,默认情况下,该资源的后代会继承组织政策。例如,如果您对某个文件夹强制执行一项政策, Google Cloud 会对该文件夹中的所有项目强制执行该政策。如需详细了解此行为及其更改方式,请参阅层次结构评估规则。
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
安装 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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init - 请确保您知道您的组织 ID。
所需的角色
如需获得管理组织政策所需的权限,请让您的管理员为您授予以下 IAM 角色:
- 组织资源的 Organization Policy Administrator (
roles/orgpolicy.policyAdmin) -
创建或更新 Integration Connectors 连接:项目资源的 Integration Connectors Admin (
roles/connectors.admin)
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
这些预定义角色包含管理组织政策所需的权限。如需查看所需的确切权限,请展开所需权限部分:
所需权限
管理组织政策需要以下权限:
-
组织资源的
orgpolicy.* -
如需创建或更新 Integration Connectors 连接,请执行以下操作:
-
项目资源的
connectors.connections.create -
项目资源的
connectors.connections.get -
项目资源的
connectors.connections.list -
项目资源的
connectors.connections.update
-
项目资源的
Integration Connectors 支持的资源
下表列出了您可以在自定义限制条件中引用的 Integration Connectors 资源。| 资源 | 字段 |
|---|---|
| connectors.googleapis.com/Connection |
resource.asyncOperationsEnabled
|
resource.authConfig.additionalVariables.boolValue
| |
resource.authConfig.additionalVariables.encryptionKeyValue.kmsKeyName
| |
resource.authConfig.additionalVariables.encryptionKeyValue.type
| |
resource.authConfig.additionalVariables.intValue
| |
resource.authConfig.additionalVariables.key
| |
resource.authConfig.additionalVariables.secretValue.secretVersion
| |
resource.authConfig.additionalVariables.stringValue
| |
resource.authConfig.authKey
| |
resource.authConfig.authType
| |
resource.authConfig.oauth2AuthCodeFlow.authCode
| |
resource.authConfig.oauth2AuthCodeFlow.authUri
| |
resource.authConfig.oauth2AuthCodeFlow.clientId
| |
resource.authConfig.oauth2AuthCodeFlow.clientSecret.secretVersion
| |
resource.authConfig.oauth2AuthCodeFlow.enablePkce
| |
resource.authConfig.oauth2AuthCodeFlow.pkceVerifier
| |
resource.authConfig.oauth2AuthCodeFlow.redirectUri
| |
resource.authConfig.oauth2AuthCodeFlow.scopes
| |
resource.authConfig.oauth2AuthCodeFlowGoogleManaged.authCode
| |
resource.authConfig.oauth2AuthCodeFlowGoogleManaged.redirectUri
| |
resource.authConfig.oauth2AuthCodeFlowGoogleManaged.scopes
| |
resource.authConfig.oauth2ClientCredentials.clientId
| |
resource.authConfig.oauth2ClientCredentials.clientSecret.secretVersion
| |
resource.authConfig.oauth2JwtBearer.clientKey.secretVersion
| |
resource.authConfig.oauth2JwtBearer.jwtClaims.audience
| |
resource.authConfig.oauth2JwtBearer.jwtClaims.issuer
| |
resource.authConfig.oauth2JwtBearer.jwtClaims.subject
| |
resource.authConfig.sshPublicKey.certType
| |
resource.authConfig.sshPublicKey.sshClientCert.secretVersion
| |
resource.authConfig.sshPublicKey.sshClientCertPass.secretVersion
| |
resource.authConfig.sshPublicKey.username
| |
resource.authConfig.userPassword.password.secretVersion
| |
resource.authConfig.userPassword.username
| |
resource.authOverrideEnabled
| |
resource.configVariables.boolValue
| |
resource.configVariables.encryptionKeyValue.kmsKeyName
| |
resource.configVariables.encryptionKeyValue.type
| |
resource.configVariables.intValue
| |
resource.configVariables.key
| |
resource.configVariables.secretValue.secretVersion
| |
resource.configVariables.stringValue
| |
resource.connectorVersion
| |
resource.description
| |
resource.destinationConfigs.destinations.host
| |
resource.destinationConfigs.destinations.port
| |
resource.destinationConfigs.key
| |
resource.euaOauthAuthConfig.additionalVariables.boolValue
| |
resource.euaOauthAuthConfig.additionalVariables.encryptionKeyValue.kmsKeyName
| |
resource.euaOauthAuthConfig.additionalVariables.encryptionKeyValue.type
| |
resource.euaOauthAuthConfig.additionalVariables.intValue
| |
resource.euaOauthAuthConfig.additionalVariables.key
| |
resource.euaOauthAuthConfig.additionalVariables.secretValue.secretVersion
| |
resource.euaOauthAuthConfig.additionalVariables.stringValue
| |
resource.euaOauthAuthConfig.authKey
| |
resource.euaOauthAuthConfig.authType
| |
resource.euaOauthAuthConfig.oauth2AuthCodeFlow.authCode
| |
resource.euaOauthAuthConfig.oauth2AuthCodeFlow.authUri
| |
resource.euaOauthAuthConfig.oauth2AuthCodeFlow.clientId
| |
resource.euaOauthAuthConfig.oauth2AuthCodeFlow.clientSecret.secretVersion
| |
resource.euaOauthAuthConfig.oauth2AuthCodeFlow.enablePkce
| |
resource.euaOauthAuthConfig.oauth2AuthCodeFlow.pkceVerifier
| |
resource.euaOauthAuthConfig.oauth2AuthCodeFlow.redirectUri
| |
resource.euaOauthAuthConfig.oauth2AuthCodeFlow.scopes
| |
resource.euaOauthAuthConfig.oauth2AuthCodeFlowGoogleManaged.authCode
| |
resource.euaOauthAuthConfig.oauth2AuthCodeFlowGoogleManaged.redirectUri
| |
resource.euaOauthAuthConfig.oauth2AuthCodeFlowGoogleManaged.scopes
| |
resource.euaOauthAuthConfig.oauth2ClientCredentials.clientId
| |
resource.euaOauthAuthConfig.oauth2ClientCredentials.clientSecret.secretVersion
| |
resource.euaOauthAuthConfig.oauth2JwtBearer.clientKey.secretVersion
| |
resource.euaOauthAuthConfig.oauth2JwtBearer.jwtClaims.audience
| |
resource.euaOauthAuthConfig.oauth2JwtBearer.jwtClaims.issuer
| |
resource.euaOauthAuthConfig.oauth2JwtBearer.jwtClaims.subject
| |
resource.euaOauthAuthConfig.sshPublicKey.certType
| |
resource.euaOauthAuthConfig.sshPublicKey.sshClientCert.secretVersion
| |
resource.euaOauthAuthConfig.sshPublicKey.sshClientCertPass.secretVersion
| |
resource.euaOauthAuthConfig.sshPublicKey.username
| |
resource.euaOauthAuthConfig.userPassword.password.secretVersion
| |
resource.euaOauthAuthConfig.userPassword.username
| |
resource.eventingConfig.additionalVariables.boolValue
| |
resource.eventingConfig.additionalVariables.encryptionKeyValue.kmsKeyName
| |
resource.eventingConfig.additionalVariables.encryptionKeyValue.type
| |
resource.eventingConfig.additionalVariables.intValue
| |
resource.eventingConfig.additionalVariables.key
| |
resource.eventingConfig.additionalVariables.secretValue.secretVersion
| |
resource.eventingConfig.additionalVariables.stringValue
| |
resource.eventingConfig.allowedEventTypes
| |
resource.eventingConfig.authConfig.additionalVariables.boolValue
| |
resource.eventingConfig.authConfig.additionalVariables.encryptionKeyValue.kmsKeyName
| |
resource.eventingConfig.authConfig.additionalVariables.encryptionKeyValue.type
| |
resource.eventingConfig.authConfig.additionalVariables.intValue
| |
resource.eventingConfig.authConfig.additionalVariables.key
| |
resource.eventingConfig.authConfig.additionalVariables.secretValue.secretVersion
| |
resource.eventingConfig.authConfig.additionalVariables.stringValue
| |
resource.eventingConfig.authConfig.authKey
| |
resource.eventingConfig.authConfig.authType
| |
resource.eventingConfig.authConfig.oauth2AuthCodeFlow.authCode
| |
resource.eventingConfig.authConfig.oauth2AuthCodeFlow.authUri
| |
resource.eventingConfig.authConfig.oauth2AuthCodeFlow.clientId
| |
resource.eventingConfig.authConfig.oauth2AuthCodeFlow.clientSecret.secretVersion
| |
resource.eventingConfig.authConfig.oauth2AuthCodeFlow.enablePkce
| |
resource.eventingConfig.authConfig.oauth2AuthCodeFlow.pkceVerifier
| |
resource.eventingConfig.authConfig.oauth2AuthCodeFlow.redirectUri
| |
resource.eventingConfig.authConfig.oauth2AuthCodeFlow.scopes
| |
resource.eventingConfig.authConfig.oauth2AuthCodeFlowGoogleManaged.authCode
| |
resource.eventingConfig.authConfig.oauth2AuthCodeFlowGoogleManaged.redirectUri
| |
resource.eventingConfig.authConfig.oauth2AuthCodeFlowGoogleManaged.scopes
| |
resource.eventingConfig.authConfig.oauth2ClientCredentials.clientId
| |
resource.eventingConfig.authConfig.oauth2ClientCredentials.clientSecret.secretVersion
| |
resource.eventingConfig.authConfig.oauth2JwtBearer.clientKey.secretVersion
| |
resource.eventingConfig.authConfig.oauth2JwtBearer.jwtClaims.audience
| |
resource.eventingConfig.authConfig.oauth2JwtBearer.jwtClaims.issuer
| |
resource.eventingConfig.authConfig.oauth2JwtBearer.jwtClaims.subject
| |
resource.eventingConfig.authConfig.sshPublicKey.certType
| |
resource.eventingConfig.authConfig.sshPublicKey.sshClientCert.secretVersion
| |
resource.eventingConfig.authConfig.sshPublicKey.sshClientCertPass.secretVersion
| |
resource.eventingConfig.authConfig.sshPublicKey.username
| |
resource.eventingConfig.authConfig.userPassword.password.secretVersion
| |
resource.eventingConfig.authConfig.userPassword.username
| |
resource.eventingConfig.deadLetterConfig.projectId
| |
resource.eventingConfig.deadLetterConfig.topic
| |
resource.eventingConfig.enrichmentConfig.appendAcl
| |
resource.eventingConfig.enrichmentEnabled
| |
resource.eventingConfig.listenerAuthConfig.additionalVariables.boolValue
| |
resource.eventingConfig.listenerAuthConfig.additionalVariables.encryptionKeyValue.kmsKeyName
| |
resource.eventingConfig.listenerAuthConfig.additionalVariables.encryptionKeyValue.type
| |
resource.eventingConfig.listenerAuthConfig.additionalVariables.intValue
| |
resource.eventingConfig.listenerAuthConfig.additionalVariables.key
| |
resource.eventingConfig.listenerAuthConfig.additionalVariables.secretValue.secretVersion
| |
resource.eventingConfig.listenerAuthConfig.additionalVariables.stringValue
| |
resource.eventingConfig.listenerAuthConfig.authKey
| |
resource.eventingConfig.listenerAuthConfig.authType
| |
resource.eventingConfig.listenerAuthConfig.oauth2AuthCodeFlow.authCode
| |
resource.eventingConfig.listenerAuthConfig.oauth2AuthCodeFlow.authUri
| |
resource.eventingConfig.listenerAuthConfig.oauth2AuthCodeFlow.clientId
| |
resource.eventingConfig.listenerAuthConfig.oauth2AuthCodeFlow.clientSecret.secretVersion
| |
resource.eventingConfig.listenerAuthConfig.oauth2AuthCodeFlow.enablePkce
| |
resource.eventingConfig.listenerAuthConfig.oauth2AuthCodeFlow.pkceVerifier
| |
resource.eventingConfig.listenerAuthConfig.oauth2AuthCodeFlow.redirectUri
| |
resource.eventingConfig.listenerAuthConfig.oauth2AuthCodeFlow.scopes
| |
resource.eventingConfig.listenerAuthConfig.oauth2AuthCodeFlowGoogleManaged.authCode
| |
resource.eventingConfig.listenerAuthConfig.oauth2AuthCodeFlowGoogleManaged.redirectUri
| |
resource.eventingConfig.listenerAuthConfig.oauth2AuthCodeFlowGoogleManaged.scopes
| |
resource.eventingConfig.listenerAuthConfig.oauth2ClientCredentials.clientId
| |
resource.eventingConfig.listenerAuthConfig.oauth2ClientCredentials.clientSecret.secretVersion
| |
resource.eventingConfig.listenerAuthConfig.oauth2JwtBearer.clientKey.secretVersion
| |
resource.eventingConfig.listenerAuthConfig.oauth2JwtBearer.jwtClaims.audience
| |
resource.eventingConfig.listenerAuthConfig.oauth2JwtBearer.jwtClaims.issuer
| |
resource.eventingConfig.listenerAuthConfig.oauth2JwtBearer.jwtClaims.subject
| |
resource.eventingConfig.listenerAuthConfig.sshPublicKey.certType
| |
resource.eventingConfig.listenerAuthConfig.sshPublicKey.sshClientCert.secretVersion
| |
resource.eventingConfig.listenerAuthConfig.sshPublicKey.sshClientCertPass.secretVersion
| |
resource.eventingConfig.listenerAuthConfig.sshPublicKey.username
| |
resource.eventingConfig.listenerAuthConfig.userPassword.password.secretVersion
| |
resource.eventingConfig.listenerAuthConfig.userPassword.username
| |
resource.eventingConfig.privateConnectivityAllowlistedProjects
| |
resource.eventingConfig.privateConnectivityEnabled
| |
resource.eventingConfig.proxyDestinationConfig.destinations.host
| |
resource.eventingConfig.proxyDestinationConfig.destinations.port
| |
resource.eventingConfig.proxyDestinationConfig.key
| |
resource.eventingConfig.registrationDestinationConfig.destinations.host
| |
resource.eventingConfig.registrationDestinationConfig.destinations.port
| |
resource.eventingConfig.registrationDestinationConfig.key
| |
resource.eventingConfig.sslConfig.additionalVariables.boolValue
| |
resource.eventingConfig.sslConfig.additionalVariables.encryptionKeyValue.kmsKeyName
| |
resource.eventingConfig.sslConfig.additionalVariables.encryptionKeyValue.type
| |
resource.eventingConfig.sslConfig.additionalVariables.intValue
| |
resource.eventingConfig.sslConfig.additionalVariables.key
| |
resource.eventingConfig.sslConfig.additionalVariables.secretValue.secretVersion
| |
resource.eventingConfig.sslConfig.additionalVariables.stringValue
| |
resource.eventingConfig.sslConfig.clientCertificate.secretVersion
| |
resource.eventingConfig.sslConfig.clientCertType
| |
resource.eventingConfig.sslConfig.clientPrivateKey.secretVersion
| |
resource.eventingConfig.sslConfig.clientPrivateKeyPass.secretVersion
| |
resource.eventingConfig.sslConfig.privateServerCertificate.secretVersion
| |
resource.eventingConfig.sslConfig.serverCertType
| |
resource.eventingConfig.sslConfig.trustModel
| |
resource.eventingConfig.sslConfig.type
| |
resource.eventingConfig.sslConfig.useSsl
| |
resource.eventingEnablementType
| |
resource.fallbackOnAdminCredentials
| |
resource.lockConfig.locked
| |
resource.lockConfig.reason
| |
resource.logConfig.enabled
| |
resource.logConfig.level
| |
resource.nodeConfig.maxNodeCount
| |
resource.nodeConfig.minNodeCount
| |
resource.serviceAccount
| |
resource.sslConfig.additionalVariables.boolValue
| |
resource.sslConfig.additionalVariables.encryptionKeyValue.kmsKeyName
| |
resource.sslConfig.additionalVariables.encryptionKeyValue.type
| |
resource.sslConfig.additionalVariables.intValue
| |
resource.sslConfig.additionalVariables.key
| |
resource.sslConfig.additionalVariables.secretValue.secretVersion
| |
resource.sslConfig.additionalVariables.stringValue
| |
resource.sslConfig.clientCertificate.secretVersion
| |
resource.sslConfig.clientCertType
| |
resource.sslConfig.clientPrivateKey.secretVersion
| |
resource.sslConfig.clientPrivateKeyPass.secretVersion
| |
resource.sslConfig.privateServerCertificate.secretVersion
| |
resource.sslConfig.serverCertType
| |
resource.sslConfig.trustModel
| |
resource.sslConfig.type
| |
resource.sslConfig.useSsl
| |
resource.suspended
| |
resource.trafficShapingConfigs.duration
| |
resource.trafficShapingConfigs.quotaLimit
| |
| connectors.googleapis.com/EndpointAttachment |
resource.description
|
resource.endpointGlobalAccess
| |
resource.serviceAttachment
| |
| connectors.googleapis.com/EventSubscription |
resource.destinations.endpoint.endpointUri
|
resource.destinations.endpoint.headers.key
| |
resource.destinations.endpoint.headers.value
| |
resource.destinations.pubsub.configVariables.boolValue
| |
resource.destinations.pubsub.configVariables.encryptionKeyValue.kmsKeyName
| |
resource.destinations.pubsub.configVariables.intValue
| |
resource.destinations.pubsub.configVariables.key
| |
resource.destinations.pubsub.configVariables.secretValue.secretVersion
| |
resource.destinations.pubsub.configVariables.stringValue
| |
resource.destinations.pubsub.projectId
| |
resource.destinations.pubsub.topicId
| |
resource.destinations.serviceAccount
| |
resource.destinations.type
| |
resource.eventTypeId
| |
resource.jms.name
| |
resource.jms.type
| |
resource.name
| |
resource.subscriber
| |
resource.subscriberLink
| |
resource.triggerConfigVariables.boolValue
| |
resource.triggerConfigVariables.encryptionKeyValue.kmsKeyName
| |
resource.triggerConfigVariables.intValue
| |
resource.triggerConfigVariables.key
| |
resource.triggerConfigVariables.secretValue.secretVersion
| |
resource.triggerConfigVariables.stringValue
| |
| connectors.googleapis.com/ManagedZone |
resource.description
|
resource.dns
| |
resource.targetProject
| |
resource.targetVpc
| |
| connectors.googleapis.com/RegionalSettings |
resource.encryptionConfig.encryptionType
|
resource.encryptionConfig.kmsKeyName
| |
resource.networkConfig.egressMode
|
设置自定义限制条件
自定义限制条件是在 YAML 文件中,由实施组织政策的服务所支持的资源、方法、条件和操作定义的。自定义限制条件的条件使用通用表达式语言 (CEL) 进行定义。如需详细了解如何使用 CEL 构建自定义限制条件中的条件,请参阅创建和管理自定义限制条件的 CEL 部分。
控制台
如需创建自定义限制条件,请执行以下操作:
- 在 Google Cloud 控制台中,转到组织政策页面。
- 在项目选择器中,选择要为其设置组织政策的项目。
- 点击 自定义限制条件。
- 在显示名称框中,为限制条件输入一个人类可读名称。此名称会在错误消息中使用,并可用于识别和调试用途。请勿在显示名称中使用个人身份信息 (PII) 或敏感数据,因为此名称可能会在错误消息中公开。此字段最多可包含 200 个字符。
-
在限制条件 ID 框中,为新的自定义限制条件输入所需的 ID。自定义限制条件只能包含字母(包括大写和小写)或数字,例如
custom.allowedConnectionLocations。此字段最多可包含 70 个字符,不计算前缀 (custom.),例如organizations/123456789/customConstraints/custom。请勿在限制条件 ID 中包含个人身份信息或敏感数据,因为该 ID 可能会在错误消息中公开。 - 在说明框中,输入人类可读的限制条件说明。当违反政策时,此说明将用作错误消息。请包含有关发生违规的原因以及如何解决违规问题的详细信息。请勿在说明中包含个人身份信息或敏感数据,因为该说明可能会在错误消息中公开。 此字段最多可包含 2000 个字符。
-
在资源类型框中,选择包含要限制的对象和字段的 Google Cloud REST 资源的名称,例如
container.googleapis.com/NodePool。大多数资源类型最多支持 20 个自定义限制条件。如果您尝试创建更多自定义限制条件,操作将会失败。 -
在强制执行方法下,选择是对 REST
CREATE方法强制执行限制条件,还是同时对CREATE和UPDATE方法强制执行限制条件。如果您对违反限制条件的资源使用UPDATE方法强制执行限制条件,除非更改解决了违规问题,否则组织政策会阻止对该资源的更改。 - 如需定义条件,请点击 修改条件。
-
在添加条件面板中,创建一个引用受支持服务资源的 CEL 条件,例如
resource.management.autoUpgrade == false。此字段最多可包含 1,000 个字符。如需详细了解 CEL 用法,请参阅通用表达式语言。 如需详细了解可在自定义限制条件中使用的服务资源,请参阅自定义限制条件支持的服务。 - 点击保存。
- 在操作下,选择在满足条件时是允许还是拒绝评估的方法。
- 点击创建限制条件。
如需查看每种服务支持的方法,请在 支持自定义限制条件的服务中找到相应服务。
拒绝操作意味着,如果条件计算结果为 true,则创建或更新资源的操作会被阻止。
允许操作意味着,仅当条件计算结果为 true 时,才允许执行创建或更新资源的操作。除了条件中明确列出的情况之外,其他所有情况都会被阻止。
在每个字段中输入值后,右侧将显示此自定义限制条件的等效 YAML 配置。
gcloud
- 如需创建自定义限制条件,请使用以下格式创建 YAML 文件:
ORGANIZATION_ID:您的组织 ID,例如123456789。-
CONSTRAINT_NAME:新的自定义限制条件的名称。自定义限制条件只能包含字母(包括大写和小写)或数字,例如custom.allowedConnectionLocations。此字段最多可包含 70 个字符,不计算前缀 (custom.),例如organizations/123456789/customConstraints/custom。请勿在限制条件 ID 中包含个人身份信息或敏感数据,因为该 ID 可能会在错误消息中公开。 -
RESOURCE_NAME:包含要限制的对象和字段的 Google Cloud资源的完全限定名称。例如connectors.googleapis.com/Connection。 大多数资源类型最多支持 20 个自定义限制条件。如果您尝试创建更多自定义限制条件,操作将会失败。 -
methodTypes:强制执行限制条件的 REST 方法。 可以是CREATE,也可以是CREATE和UPDATE。如果您对违反限制条件的资源使用UPDATE方法强制执行限制条件,除非更改解决了违规问题,否则组织政策会阻止对该资源的更改。 -
CONDITION:针对受支持服务资源的表示形式编写的 CEL 条件。此字段最多可包含 1,000 个字符。例如"!(resource.location == 'us-central1' || resource.location == 'europe-west1')"。 -
ACTION:满足condition时要执行的操作。 可能的值包括ALLOW和DENY。 -
DISPLAY_NAME:限制条件的直观易记名称。此名称会在错误消息中使用,并可用于识别和调试用途。请勿在显示名称中使用个人身份信息或敏感数据,因为此名称可能会在错误消息中公开。 此字段最多可包含 200 个字符。 -
DESCRIPTION:直观易懂的限制条件说明,当违反政策时会作为错误消息显示。此字段最多可包含 2000 个字符。 -
为新的自定义限制条件创建 YAML 文件后,您必须对其进行设置,以使其可用于组织中的组织政策。如需设置自定义限制条件,请使用
gcloud org-policies set-custom-constraint命令: - 如需验证自定义限制条件是否存在,请使用
gcloud org-policies list-custom-constraints命令:
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME resourceTypes: RESOURCE_NAME methodTypes: - CREATE
- UPDATE condition: "CONDITION" actionType: ACTION displayName: DISPLAY_NAME description: DESCRIPTION
请替换以下内容:
如需查看每种服务支持的方法,请在 支持自定义限制条件的服务中找到相应服务。
如需详细了解可针对其编写条件的资源,请参阅支持的资源。
允许操作意味着,如果条件计算结果为 true,则允许执行创建或更新资源的操作。这也意味着,除了条件中明确列出的情况之外,其他所有情况都会被阻止。
拒绝操作意味着,如果条件计算结果为 true,则创建或更新资源的操作会被阻止。
gcloud org-policies set-custom-constraint CONSTRAINT_PATH
将 CONSTRAINT_PATH 替换为自定义限制条件文件的完整路径。例如 /home/user/customconstraint.yaml。
此操作完成后,您的自定义限制条件将作为组织政策显示在您的 Google Cloud 组织政策列表中。
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
将 ORGANIZATION_ID 替换为您的组织资源的 ID。
如需了解详情,请参阅查看组织政策。
强制执行自定义组织政策
如需强制执行限制条件,您可以创建引用该限制条件的组织政策,并将该组织政策应用于 Google Cloud 资源。控制台
- 在 Google Cloud 控制台中,前往组织政策页面。
- 在项目选择器中,选择要为其设置组织政策的项目。
- 从组织政策页面上的列表中选择您的限制条件,以查看该限制条件的政策详情页面。
- 如需为该资源配置组织政策,请点击管理政策。
- 在修改政策页面,选择覆盖父级政策。
- 点击添加规则。
- 在强制执行部分中,选择是否强制执行此组织政策。
- (可选)如需使组织政策成为基于某个标记的条件性政策,请点击添加条件。请注意,如果您向组织政策添加条件规则,则必须至少添加一个无条件规则,否则无法保存政策。如需了解详情,请参阅 使用标记限定组织政策的范围。
- 点击测试更改以模拟组织政策的效果。如需了解详情,请参阅使用 Policy Simulator 测试组织政策更改。
- 如需在试运行模式下强制执行组织政策,请点击设置试运行政策。如需了解详情,请参阅 测试组织政策。
- 验证试运行模式下的组织政策按预期运行后,点击设置政策来设置现行政策。
gcloud
- 如需创建包含布尔值规则的组织政策,请创建引用该限制条件的 YAML 政策文件:
-
PROJECT_ID:要对其强制执行您的限制条件的项目。 -
CONSTRAINT_NAME:您为自定义限制条件定义的名称。例如,custom.allowedConnectionLocations。 -
如需在试运行模式下强制执行组织政策,请运行以下带有
dryRunSpec标志的命令: -
验证试运行模式下的组织政策按预期运行后,使用
org-policies set-policy命令和spec标志设置现行政策:
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME spec: rules: - enforce: true dryRunSpec: rules: - enforce: true
请替换以下内容:
gcloud org-policies set-policy POLICY_PATH --update-mask=dryRunSpec
将 POLICY_PATH 替换为组织政策 YAML 文件的完整路径。该政策最长需要 15 分钟才能生效。
gcloud org-policies set-policy POLICY_PATH --update-mask=spec
将 POLICY_PATH 替换为组织政策 YAML 文件的完整路径。该政策最长需要 15 分钟才能生效。
测试自定义组织政策
以下示例创建自定义限制条件和政策,以限制特定项目中的新 Integration Connectors 连接只能在 us-central1 或 europe-west1 中创建。
在开始之前,您必须拥有以下各项:
- 您的组织 ID
- 项目 ID
创建限制条件
如需创建自定义限制条件,请按照以下步骤操作:
创建以下 YAML 文件并将其另存为
constraint-allowed-locations.yaml:name: organizations/ORGANIZATION_ID/customConstraints/custom.allowedConnectionLocations resourceTypes: - connectors.googleapis.com/Connection methodTypes: - CREATE condition: "!(resource.location == 'us-central1' || resource.location == 'europe-west1')" actionType: DENY displayName: Restrict connection locations description: Connections can only be created in us-central1 or europe-west1.将
ORGANIZATION_ID替换为您的组织 ID。此文件定义了一个限制条件,对于每个新连接,如果位置不是
us-central1或europe-west1,则操作将被拒绝。应用限制条件:
gcloud org-policies set-custom-constraint ~/constraint-allowed-locations.yaml验证限制条件存在:
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID输出类似于以下内容:
CUSTOM_CONSTRAINT ACTION_TYPE METHOD_TYPES RESOURCE_TYPES DISPLAY_NAME custom.allowedConnectionLocations DENY CREATE connectors.googleapis.com/Connection Restrict connection locations ...
创建政策
现在,创建一个政策并将其应用于您创建的自定义限制条件。
将以下文件保存为
policy-allowed-locations.yaml:name: projects/PROJECT_ID/policies/custom.allowedConnectionLocations spec: rules: - enforce: true将
PROJECT_ID替换为您的项目 ID。应用政策:
gcloud org-policies set-policy ~/policy-allowed-locations.yaml验证政策存在:
gcloud org-policies list --project=PROJECT_ID输出类似于以下内容:
CONSTRAINT LIST_POLICY BOOLEAN_POLICY ETAG custom.allowedConnectionLocations - SET COCsm5QGENiXi2E=
应用政策后,请等待大约两分钟,以便 Google Cloud 开始强制执行政策。
测试政策
尝试在不允许的位置(例如 us-east1)创建 Integration Connectors 连接。您可以使用 Google Cloud 控制台、Google Cloud CLI 或 REST API 来执行此操作。例如,如果使用 gcloud CLI,请在 us-east1 中创建连接:
gcloud connectors connections create test-connection --location=us-east1 --connector-version=<var>CONNECTOR_VERSION</var> ...
将 CONNECTOR_VERSION 替换为项目中的有效连接器版本,并添加创建连接所需的其他标志。
输出如下所示:
Operation denied by custom org policies: ["customConstraints/custom.allowedConnectionLocations": "Connections can only be created in us-central1 or europe-west1."]
常见用例的自定义组织政策示例
下表提供了一些常见自定义限制条件的语法示例。
| 说明 | 限制条件语法 |
|---|---|
将连接位置限制为 us-central1 或 europe-west1 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.allowedConnectionLocations resourceTypes: - connectors.googleapis.com/Connection methodTypes: - CREATE condition: "!(resource.location == 'us-central1' || resource.location == 'europe-west1')" actionType: DENY displayName: Restrict connection locations description: Connections can only be created in us-central1 or europe-west1. |