使用自定义限制条件管理服务管理资源

本页面介绍了如何使用组织政策服务自定义限制条件来限制对以下 Google Cloud 资源执行的特定操作:

  • servicemanagement.googleapis.com/Service

如需详细了解组织政策,请参阅自定义组织政策

组织政策和限制条件简介

借助 Google Cloud 组织政策服务,您可以对组织的资源进行程序化集中控制。作为组织政策管理员,您可以定义组织政策,这是一组称为限制条件的限制,会应用于 Google Cloud 资源层次结构中的Google Cloud 资源及其后代。您可以在组织、文件夹或项目级强制执行组织政策。

组织政策为各种 Google Cloud 服务提供预定义限制条件。但是,如果您想要更精细地控制和自定义组织政策中受限的特定字段,还可以创建自定义限制条件并在组织政策中使用这些自定义限制条件。

政策继承

如果您对资源强制执行政策,默认情况下,该资源的后代会继承组织政策。例如,如果您对某个文件夹强制执行一项政策, Google Cloud 会对该文件夹中的所有项目强制执行该政策。如需详细了解此行为及其更改方式,请参阅层次结构评估规则

准备工作

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Install the Google Cloud CLI.
  5. To initialize the gcloud CLI, run the following command:

    gcloud init
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Install the Google Cloud CLI.
  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. 请确保您知道您的组织 ID

所需的角色

如需获得管理自定义组织政策所需的权限,请让您的管理员为您授予组织资源的 Organization Policy Administrator (roles/orgpolicy.policyAdmin) IAM 角色。 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义角色或其他预定义角色来获取所需的权限。

创建自定义限制条件

自定义限制条件是在 YAML 文件中,由实施组织政策的服务所支持的资源、方法、条件和操作定义的。自定义限制条件的条件使用通用表达式语言 (CEL) 进行定义。如需详细了解如何使用 CEL 构建自定义限制条件中的条件,请参阅创建和管理自定义限制条件的 CEL 部分。

如需创建自定义限制条件,请使用以下格式创建 YAML 文件:

name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes:
- RESOURCE_NAME
methodTypes:
- CREATE
- UPDATE
condition: "CONDITION"
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION

替换以下内容:

  • ORGANIZATION_ID:您的组织 ID,例如 123456789

  • CONSTRAINT_NAME:新的自定义限制条件的名称。 自定义限制条件必须以 custom. 开头,只能包含大写字母、小写字母或数字。例如 custom.authorizationCheck。该字段的最大长度为 70 个字符。

  • RESOURCE_NAME:包含要限制的对象和字段的Google Cloud 资源的完全限定名称。例如 servicemanagement.googleapis.com/Service

  • CONDITION:针对受支持的服务资源的表示法编写的 CEL 条件。此字段的长度上限为 1,000 个字符。 如需详细了解可用于针对其编写条件的资源,请参阅支持的资源。 例如 resource.name == \"test-service-name\"

  • ACTION:满足 condition 时要执行的操作。 可能的值包括 ALLOWDENY

  • 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 资源。

控制台

  1. 在 Google Cloud 控制台中,转到组织政策页面。

    转到组织政策

  2. 在项目选择器中,选择要设置组织政策的项目。
  3. 组织政策页面上的列表中选择您的限制条件,以查看该限制条件的政策详情页面。
  4. 如需为该资源配置组织政策,请点击管理政策
  5. 修改政策页面,选择覆盖父级政策
  6. 点击添加规则
  7. 强制执行部分中,选择开启还是关闭此组织政策的强制执行。
  8. (可选)如需使组织政策成为基于某个标记的条件性政策,请点击添加条件。请注意,如果您向组织政策添加条件规则,则必须至少添加一个无条件规则,否则无法保存政策。如需了解详情,请参阅设置带有标记的组织政策
  9. 如果是自定义限制条件,您可以点击测试更改来模拟此组织政策的效果。如需了解详情,请参阅使用 Policy Simulator 测试组织政策更改
  10. 若要完成并应用组织政策,请点击设置政策。该政策最长需要 15 分钟才能生效。

gcloud

如需创建强制执行布尔值限制条件的组织政策,请创建引用该限制条件的 YAML 政策文件:

      name: projects/PROJECT_ID/policies/CONSTRAINT_NAME
      spec:
        rules:
        - enforce: true
    

请替换以下内容:

  • PROJECT_ID:要对其实施限制条件的项目。
  • CONSTRAINT_NAME:您为自定义限制条件定义的名称。例如,custom.authorizationCheck

如需强制执行包含限制条件的组织政策,请运行以下命令:

    gcloud org-policies set-policy POLICY_PATH
    

POLICY_PATH 替换为组织政策 YAML 文件的完整路径。该政策最长需要 15 分钟才能生效。

测试自定义组织政策

以下示例会根据 Service 资源的属性创建自定义约束条件。这些规则可用于允许或拒绝特定的 CreateServiceConfig 请求

在开始之前,您应该了解以下信息:

  • 您的组织 ID
  • 项目 ID

创建限制条件

  1. 将以下文件保存为 constraint-file-name.yaml

        name: organizations/ORGANIZATION_ID/customConstraints/custom.authorizationCheck
        resourceTypes:
        -  servicemanagement.googleapis.com/Service
        methodTypes:
        -   CREATE
        -   UPDATE
        condition: resource.name == \"test-service-name\"
        actionType: ALLOW
        displayName: Check Configuration time policy enforcement
        description: Organization policies, defined with CEL and enforced via service manager APIs, allow admins to control service configurations through inheritable and granular rules.
    

    这定义了一个约束条件,即每个 Service Management 的服务名称都应等于 test-service-name

  2. 应用限制条件:

    gcloud org-policies set-custom-constraint ~/constraint-file-name.yaml
    
  3. 验证限制条件存在:

    gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
    

    输出类似于以下内容:

    CUSTOM_CONSTRAINT ACTION_TYPE METHOD_TYPES RESOURCE_TYPES DISPLAY_NAME
    custom.configTimeEnforcement ALLOW CREATE,UPDATE servicemanagement.googleapis.com/Service Configuration time policy enforcement
    

创建政策

  1. 将以下文件保存为 policy-file-name.yaml

    name: projects/PROJECT_ID/policies/custom.configTimeEnforcement
    spec:
      rules:
    - enforce: true
    

    PROJECT_ID 替换为您的项目 ID。

  2. 应用政策:

    gcloud org-policies set-policy ~/policy-file-name.yaml
    
  3. 验证政策存在:

    gcloud org-policies list --project=PROJECT_ID
    

    输出类似于以下内容:

    CONSTRAINT LIST_POLICY BOOLEAN_POLICY ETAG
    custom.configTimeEnforcement - SET COCsm5QGENiXi2E=
    

应用政策后,请等待大约两分钟,以便 Google Cloud 开始强制执行政策。

测试政策

运行以下命令以创建服务配置,以检查约束条件。

  gcloud endpoints services deploy policy-file-name.yaml

输出类似于以下内容:

  Operation denied by custom org policies: ["customConstraints/custom.authorizationCheck": "error Message"]

服务管理支持的资源

下表列出了您可以在自定义限制条件中引用的 Service Management 资源。

资源 字段
servicemanagement.googleapis.com/Service resource.authentication.providers.audiences
resource.authentication.providers.authorizationUrl
resource.authentication.providers.issuer
resource.authentication.providers.jwksUri
resource.authentication.providers.jwtLocations.cookie
resource.authentication.providers.jwtLocations.header
resource.authentication.providers.jwtLocations.query
resource.authentication.providers.jwtLocations.valuePrefix
resource.authentication.rules.allowWithoutCredential
resource.authentication.rules.oauth.canonicalScopes
resource.authentication.rules.requirements.audiences
resource.authentication.rules.requirements.providerId
resource.authentication.rules.selector
resource.backend.rules.address
resource.backend.rules.deadline
resource.backend.rules.disableAuth
resource.backend.rules.jwtAudience
resource.backend.rules.operationDeadline
resource.backend.rules.pathTranslation
resource.backend.rules.protocol
resource.backend.rules.selector
resource.billing.consumerDestinations.metrics
resource.billing.consumerDestinations.monitoredResource
resource.context.rules.allowedRequestExtensions
resource.context.rules.allowedResponseExtensions
resource.context.rules.provided
resource.context.rules.requested
resource.context.rules.selector
resource.control.environment
resource.control.methodPolicies.requestPolicies.resourcePermission
resource.control.methodPolicies.requestPolicies.resourceType
resource.control.methodPolicies.requestPolicies.selector
resource.control.methodPolicies.selector
resource.customError.rules.isErrorType
resource.customError.rules.selector
resource.customError.types
resource.endpoints.aliases
resource.endpoints.allowCors
resource.endpoints.name
resource.endpoints.target
resource.logging.consumerDestinations.logs
resource.logging.consumerDestinations.monitoredResource
resource.logging.producerDestinations.logs
resource.logging.producerDestinations.monitoredResource
resource.logs.description
resource.logs.displayName
resource.logs.name
resource.metrics.description
resource.metrics.displayName
resource.metrics.launchStage
resource.metrics.metadata.ingestDelay
resource.metrics.metadata.samplePeriod
resource.metrics.metadata.timeSeriesResourceHierarchyLevel
resource.metrics.metricKind
resource.metrics.monitoredResourceTypes
resource.metrics.name
resource.metrics.type
resource.metrics.unit
resource.metrics.valueType
resource.monitoredResources.description
resource.monitoredResources.displayName
resource.monitoredResources.launchStage
resource.monitoredResources.name
resource.monitoredResources.type
resource.monitoring.consumerDestinations.metrics
resource.monitoring.consumerDestinations.monitoredResource
resource.monitoring.producerDestinations.metrics
resource.monitoring.producerDestinations.monitoredResource
resource.name
resource.producerProjectId
resource.publishing.apiShortName
resource.publishing.codeownerGithubTeams
resource.publishing.docTagPrefix
resource.publishing.documentationUri
resource.publishing.githubLabel
resource.publishing.librarySettings.cppSettings.common.destinations
resource.publishing.librarySettings.cppSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.dotnetSettings.common.destinations
resource.publishing.librarySettings.dotnetSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.dotnetSettings.forcedNamespaceAliases
resource.publishing.librarySettings.dotnetSettings.handwrittenSignatures
resource.publishing.librarySettings.dotnetSettings.ignoredResources
resource.publishing.librarySettings.dotnetSettings.renamedResources
resource.publishing.librarySettings.dotnetSettings.renamedServices
resource.publishing.librarySettings.goSettings.common.destinations
resource.publishing.librarySettings.goSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.javaSettings.common.destinations
resource.publishing.librarySettings.javaSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.javaSettings.libraryPackage
resource.publishing.librarySettings.javaSettings.serviceClassNames
resource.publishing.librarySettings.launchStage
resource.publishing.librarySettings.nodeSettings.common.destinations
resource.publishing.librarySettings.nodeSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.phpSettings.common.destinations
resource.publishing.librarySettings.phpSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.pythonSettings.common.destinations
resource.publishing.librarySettings.pythonSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.pythonSettings.experimentalFeatures.restAsyncIoEnabled
resource.publishing.librarySettings.restNumericEnums
resource.publishing.librarySettings.rubySettings.common.destinations
resource.publishing.librarySettings.rubySettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.version
resource.publishing.methodSettings.autoPopulatedFields
resource.publishing.methodSettings.longRunning.initialPollDelay
resource.publishing.methodSettings.longRunning.maxPollDelay
resource.publishing.methodSettings.longRunning.pollDelayMultiplier
resource.publishing.methodSettings.longRunning.totalPollTimeout
resource.publishing.methodSettings.selector
resource.publishing.newIssueUri
resource.publishing.organization
resource.publishing.protoReferenceDocumentationUri
resource.publishing.restReferenceDocumentationUri
resource.quota.limits.defaultLimit
resource.quota.limits.description
resource.quota.limits.displayName
resource.quota.limits.duration
resource.quota.limits.freeTier
resource.quota.limits.maxLimit
resource.quota.limits.metric
resource.quota.limits.name
resource.quota.limits.unit
resource.quota.limits.values
resource.quota.metricRules.metricCosts
resource.quota.metricRules.selector
resource.systemParameters.rules.parameters.httpHeader
resource.systemParameters.rules.parameters.name
resource.systemParameters.rules.parameters.urlQueryParameter
resource.systemParameters.rules.selector
resource.title
resource.usage.producerNotificationChannel
resource.usage.requirements
resource.usage.rules.allowUnregisteredCalls
resource.usage.rules.selector
resource.usage.rules.skipServiceControl

后续步骤

  • 如需详细了解服务管理选项,请参阅服务管理概览。
  • 如需帮助解决使用服务管理时可能会遇到的常见问题,请参阅问题排查。
  • 查看适用于服务管理的预定义组织政策限制条件。
  • 详细了解组织政策服务
  • 详细了解如何创建和管理组织政策
  • 查看预定义组织政策限制条件的完整列表。