使用自定义限制条件管理 Compute Engine 资源


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

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

优势

  • 费用管理:使用自定义组织政策限制可在您的组织中使用的虚拟机实例以及磁盘大小和类型。 您还可以限制用于虚拟机实例的机器系列
  • 安全、合规性和治理:您可以使用自定义组织政策强制执行政策,具体如下所示:
    • 如需强制执行安全要求,您可以要求遵守虚拟机上的特定防火墙端口规则。
    • 为支持硬件隔离或许可合规性,您可以要求特定项目或文件夹中的所有虚拟机都在单租户节点上运行。
    • 如需管理自动化脚本,您可以使用自定义组织政策来验证标签是否与指定表达式匹配。

政策继承

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

价格

组织政策服务(包括预定义组织政策和自定义组织政策)可免费使用。

限制

  • 对于所有 Compute Engine 资源,系统会对 CREATE 方法强制执行自定义限制条件。
  • 对于某些 Compute Engine 资源(例如 Compute Engine SSL 政策资源),系统还会对 UPDATE 方法强制执行自定义限制条件。

准备工作

  • 设置身份验证(如果尚未设置)。身份验证是通过其进行身份验证以访问 Google Cloud 服务和 API 的过程。如需从本地开发环境运行代码或示例,您可以按如下方式向 Compute Engine 进行身份验证。

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    gcloud

    1. 安装 Google Cloud CLI,然后通过运行以下命令初始化 Google Cloud CLI:

      gcloud init
    2. Set a default region and zone.

所需的角色

如需获得管理 Compute Engine 资源的组织政策所需的权限,请让您的管理员为您授予以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理访问权限

这些预定义角色可提供管理 Compute Engine 资源的组织政策所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

需要具备以下权限才能管理适用于 Compute Engine 资源的组织政策:

  • orgpolicy.constraints.list
  • orgpolicy.policies.create
  • orgpolicy.policies.delete
  • orgpolicy.policies.list
  • orgpolicy.policies.update
  • orgpolicy.policy.get
  • orgpolicy.policy.set
  • 如需测试限制条件,则需要具备以下权限:
    • 针对项目的 compute.instances.create 权限
    • 使用自定义映像创建虚拟机:针对映像的 compute.images.useReadOnly 权限
    • 使用快照创建虚拟机:针对快照的 compute.snapshots.useReadOnly 权限
    • 使用实例模板创建虚拟机:针对实例模板的 compute.instanceTemplates.useReadOnly 权限
    • 为虚拟机分配旧版网络:针对项目的 compute.networks.use 权限
    • 为虚拟机指定静态 IP 地址:针对项目的 compute.addresses.use 权限
    • 使用旧版网络时为虚拟机分配外部 IP 地址:针对项目的 compute.networks.useExternalIp 权限
    • 为虚拟机指定子网:针对项目或所选子网的 compute.subnetworks.use 权限
    • 在使用 VPC 网络时为虚拟机分配外部 IP 地址:针对项目或所选子网的 compute.subnetworks.useExternalIp 权限
    • 为虚拟机设置虚拟机实例元数据:针对项目的 compute.instances.setMetadata 权限
    • 为虚拟机设置标记:针对虚拟机的 compute.instances.setTags 权限
    • 为虚拟机设置标签:针对虚拟机的 compute.instances.setLabels 权限
    • 为虚拟机设置要使用的服务账号:针对虚拟机的 compute.instances.setServiceAccount 权限
    • 为虚拟机创建新磁盘:针对项目的 compute.disks.create 权限
    • 以只读或读写模式挂接现有磁盘:针对磁盘的 compute.disks.use 权限
    • 以只读模式挂接现有磁盘:针对磁盘的 compute.disks.useReadOnly 权限

您也可以使用自定义角色或其他预定义角色来获取这些权限。

Compute Engine 支持的资源

对于 Compute Engine,您可以对以下资源和字段设置自定义限制条件。

  • 永久性磁盘compute.googleapis.com/Disk
    • 永久性磁盘类型:resource.type
    • 永久性磁盘大小:resource.sizeGb
  • 图片compute.googleapis.com/Image
    • 原始磁盘来源:resource.rawDisk.source
  • 虚拟机实例compute.googleapis.com/Instance
    • 高级机器功能:
      • resource.advancedMachineFeatures.enableNestedVirtualization
      • resource.advancedMachineFeatures.threadsPerCore
    • IP 转发:resource.canIpForward
    • 删除防护:resource.deletionProtection
    • 标签数:resource.labels
    • 机器类型:resource.machineType
    • 满足最低 CPU 要求的平台:resource.minCpuPlatform
    • 网络接口:
      • resource.networkInterfaces.network
      • resource.networkInterfaces.subnetwork
    • 节点亲和性:
      • resource.scheduling.nodeAffinities.key
      • resource.scheduling.nodeAffinities.operator
      • resource.scheduling.nodeAffinities.values
    • 专用 Google 访问通道 (IPv6):resource.privateIpv6GoogleAccess
    • SSL 政策:
      • resource.profile
      • resource.minTlsVersion
      • resource.customFeatures

设置自定义限制条件

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

您可以使用 Google Cloud 控制台或 gcloud CLI 创建自定义限制条件并将其设置为在组织政策中使用。

控制台

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

    转到“组织政策”

  2. 选择页面顶部的项目选择器

  3. 项目选择器中,选择要为其设置组织政策的资源。

  4. 点击 自定义限制条件

  5. 显示名称框中,为限制条件输入一个易记的名称。此字段的最大长度为 200 个字符。 请勿在限制条件名称中使用 PII 或敏感数据,因为这些可能会在错误消息中公开。

  6. 限制条件 ID 框中,为新的自定义限制条件输入所需的名称。自定义限制条件必须以 custom. 开头,只能包含大写字母、小写字母或数字,例如 custom.createOnlyN2DVMs。该字段的长度上限为 70 个字符,不计算前缀(例如 organizations/123456789/customConstraints/custom.)。

  7. 说明框中,输入直观易懂的限制条件说明,在违反政策时此说明内容会以错误消息的形式显示。此字段的最大长度为 2000 个字符。

  8. 资源类型框中,选择包含要限制的对象和字段的 Google Cloud REST 资源的名称。例如 compute.googleapis.com/Instance

  9. 强制执行方法下,选择是否对 REST CREATE 方法强制执行限制条件。

  10. 如需定义条件,请点击 修改条件

    1. 添加条件面板中,创建一个引用受支持的服务资源的 CEL 条件,例如 resource.machineType.contains('/machineTypes/n2d')。此字段的最大长度为 1,000 个字符。

    2. 点击保存

  11. 操作下,选择在满足上述条件时是允许还是拒绝评估的方法。

  12. 点击创建限制条件

在每个字段中输入值后,右侧将显示此自定义限制条件的等效 YAML 配置。

gcloud

如需使用 gcloud CLI 创建自定义限制条件,请为自定义限制条件创建 YAML 文件:

name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resource_types: compute.googleapis.com/RESOURCE_NAME
method_types: CREATE
condition: CONDITION
action_type: ACTION
display_name: DISPLAY_NAME
description: DESCRIPTION

替换以下内容:

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

  • CONSTRAINT_NAME:新的自定义限制条件的名称。 自定义限制条件必须以 custom. 开头,并且只能包含大写字母、小写字母或数字。例如 custom.createOnlyN2DVMs。该字段的长度上限为 70 个字符,不计算前缀(例如 organizations/123456789/customConstraints/custom.)。

  • RESOURCE_NAME:包含要限制的对象和字段的 Compute Engine API REST 资源的名称(而非 URI)。例如 Instance

  • CONDITION:针对受支持的服务资源的表示法编写的 CEL 条件。此字段的最大长度为 1,000 个字符。如需详细了解可用于针对其编写条件的资源,请参阅支持的资源。 例如 "resource.machineType.contains('/machineTypes/n2d')"

  • ACTION:满足 condition 时要执行的操作。可以是 ALLOWDENY

  • DISPLAY_NAME:限制条件的直观易记名称。 此字段的最大长度为 200 个字符。

  • DESCRIPTION:直观易懂的限制条件说明,在违反政策时显示为错误消息。 此字段的最大长度为 2000 个字符。

如需详细了解如何创建自定义限制条件,请参阅创建和管理自定义组织政策

使用 Google Cloud CLI 创建新的自定义限制条件后,您必须对其进行设置,以使其可用于组织中的组织政策。如需设置自定义限制条件,请使用 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. (可选)如需使组织政策成为基于某个标记的条件性政策,请点击添加条件。请注意,如果您向组织政策添加条件规则,则必须至少添加一个无条件规则,否则无法保存政策。如需了解详情,请参阅使用标记设置组织政策
  10. 如果是自定义限制条件,您可以点击测试更改来模拟此组织政策的效果。如需了解详情,请参阅使用 Policy Simulator 测试组织政策更改
  11. 若要完成并应用组织政策,请点击设置政策。该政策将在 15 分钟内生效。

gcloud

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

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

请替换以下内容:

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

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

    gcloud org-policies set-policy POLICY_PATH
    

POLICY_PATH 替换为组织政策 YAML 文件的完整路径。该政策将在 15 分钟内生效。

示例:创建一个限制条件以限制虚拟机使用 N2D 机器类型

gcloud

  1. 使用以下信息创建 onlyN2DVMs.yaml 限制条件文件:

    name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlyN2DVMs
    resource_types: compute.googleapis.com/Instance
    condition: "resource.machineType.contains('/machineTypes/n2d')"
    action_type: ALLOW
    method_types: CREATE
    display_name: Only N2D VMs allowed
    description: Restrict all VMs created to only use N2D machine types.
  2. 设置自定义限制条件。

    gcloud org-policies set-custom-constraint onlyN2DVMs.yaml
    
  3. 创建一份 onlyN2DVMs-policy.yaml 具有以下信息政策文件。在此示例中,我们在项目级层强制执行此限制条件,但您也可以在组织或文件夹级层设置此限制条件。 请将 PROJECT_ID 替换为您的项目 ID。

    name: projects/PROJECT_ID/policies/custom.createOnlyN2DVMs
    spec:
      rules:
      – enforce: true
    
  4. 强制执行该政策:

    gcloud org-policies set-policy onlyN2DVMs-policy.yaml
    
  5. 尝试创建使用非 N2D 机器的机器类型的虚拟机,以测试限制条件。

    gcloud compute instances create my-test-instance \
        --project=PROJECT_ID \
        --zone=us-central1-c \
        --machine-type=e2-medium
    

    输出类似于以下内容:

    ERROR: (gcloud.compute.instances.create) Could not fetch resource:
    – Operation denied by custom org policies: [customConstraints/custom.createOnlyN2DVMs]: Restrict all VMs created to only use N2D machine types.
    

示例:使用自定义限制条件限制 TLS 功能

如需使用自定义限制条件对支持的负载均衡器限制 TLS 功能,请在组织中定义使用预定义 constraints/compute.requireSslPolicy 限制条件的政策。定义政策后,请按照以下步骤设置自定义限制条件并使用它们。

  1. 为自定义限制条件创建 YAML 文件。

    name: organizations/ORGANIZATION_ID/customConstraints/custom.CONSTRAINT_NAME
    resource_types: compute.googleapis.com/sslPolicy
    methodTypes:
    - CREATE
    - UPDATE
    condition: resource.FIELD_NAME == VALUE
    action_type: ACTION
    display_name: DISPLAY_NAME
    description: DESCRIPTION
    

    以下示例将最低 TLS 版本限制为 1.2:

    name: organizations/012345678901/customConstraints/custom.restrictLbTlsVersion
    resource_types: compute.googleapis.com/sslPolicy
    methodTypes:
    - CREATE
    - UPDATE
    condition: resource.minTlsVersion == "TLS_1_2"
    action_type: ALLOW
    display_name: Restrict Load Balancing TLS version to 1.2
    description: Only allow SSL policies to be created or updated if the minimum TLS version is 1.2 where this custom constraint is enforced.
    
  2. 将自定义限制条件添加到您的组织。

    gcloud org-policies set-custom-constraint PATH_TO_FILE
    
  3. 验证您的组织中是否存在自定义限制条件。

    gcloud org-policies list-custom-constraints \
        --organization=ORGANIZATION_ID
    
  4. 为限制条件创建政策文件。

    name: projects/PROJECT_ID/policies/custom.CONSTRAINT_NAME
    spec:
      rules:
      – enforce: true
    

    替换以下内容:

    • PROJECT_ID:您的 Google Cloud 项目 ID
    • CONSTRAINT_NAME:限制条件名称
  5. 强制执行该政策:

    gcloud org-policies set-policy PATH_TO_POLICY_FILE
    

    PATH_TO_POLICY_FILE 替换为政策文件的完全限定路径。

  6. 假设您已创建了 YAML 文件以将最低 TLS 版本限制为 1.2,请通过创建将 minTlsVersion 设置为 TLS_1_0 的 SSL 政策来测试限制条件:

    gcloud compute ssl-policies create SSL_POLICY_NAME \
        --min-tls-version=1.0 \
        --project=PROJECT_ID
    

    输出类似于以下内容:

    ERROR: (gcloud.compute.ssl-policies.update) HTTPError 412: Operation
    denied by custom org policy: [customConstraints/custom.
    restrictLbTlsVersion] : Only allow SSL policy resources to be created or
    updated if the minimum TLS version is 1.2 where this custom constraint
    is enforced.
    

常见用例的自定义限制条件示例

以下部分介绍一些可能有用的自定义限制条件的语法:

磁盘

用例 语法
永久性磁盘类型必须为“极端永久性磁盘 (pd-extreme)”
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createDisksPDExtremeOnly
  resource_types: compute.googleapis.com/Disk
  condition: "resource.type.contains('pd-extreme')"
  action_type: ALLOW
  method_types: CREATE
  display_name: Create pd-extreme disks only
  description: Only the extreme persistent disk type is allowed to be created.
磁盘大小必须小于或等于 250 GB
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createDisksLessThan250GB
  resource_types: compute.googleapis.com/Disk
  condition: "resource.sizeGb <= 250"
  action_type: ALLOW
  method_types: CREATE
  display_name: Disks size maximum is 250 GB
  description: Restrict the boot disk size to 250 GB or less for all VMs.

图片

用例 语法
来源映像只能来自 Cloud Storage test_bucket
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createDisksfromStoragebucket
  resource_types: compute.googleapis.com/Image
  condition: "resource.rawDisk.source.contains('storage.googleapis.com/test_bucket/')"
  action_type: ALLOW
  method_types: CREATE
  display_name: Source image must be from Cloud Storage test_bucket only
  description: Source images used in this project must be imported from the
  Cloud Storage test_bucket.

虚拟机实例

用例 语法
虚拟机必须具有一个键设置为 cost center 的标签
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createVMWithLabel
  resource_types: compute.googleapis.com/Instance
  condition: "'cost_center' in resource.labels"
  action_type: ALLOW
  method_types: CREATE
  display_name: 'cost_center' label required
  description: Requires that all VMs created must have the a 'cost_center' label
  that can be used for tracking and billing purposes.
虚拟机必须具有一个键设置为 cost center的标签,而且值应设置为 eCommerce
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createECommerceVMOnly
  resource_types: compute.googleapis.com/Instance
  condition: "'cost_center' in resource.labels and resource.labels['cost_center'] == 'eCommerce'"
  action_type: ALLOW
  method_types: CREATE
  display_name:  Label (cost_center/eCommerce) required
  description: Label required and Key/value must be cost_center/eCommerce.
虚拟机必须使用机器类型 N2D
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlyN2DVMs
  resource_types: compute.googleapis.com/Instance
  condition: "resource.machineType.contains('/machineTypes/n2d')"
  action_type: ALLOW
  method_types: CREATE
  display_name: Only N2D VMs allowed
  description: Restrict all VMs created to only use N2D machine types.
虚拟机必须使用机器类型 e2-highmem-8
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlyE2highmem8
  resource_types: compute.googleapis.com/Instance
  condition: "resource.machineType.endsWith('-e2-highmem-8')"
  action_type: ALLOW
  method_types: CREATE
  display_name: Only "e2-highmem-8" VMs allowed
  description: Restrict all VMs created to only use the E2 high-memory
  machine types that have 8 vCPUs.
确保将虚拟机安排到节点组“foo”上
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlySTVM
  resource_types: compute.googleapis.com/Instance
  condition: "resource.scheduling.nodeAffinities.exists(n, n.key == 'foo')"
  action_type: ALLOW
  method_types: CREATE
  display_name: Only VMs scheduled on node group "foo" allowed
  description: Restrict all VMs created to use the node group "foo".

后续步骤