使用自定义限制条件管理 Cloud Storage 资源

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

  • storage.googleapis.com/Bucket

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

组织政策和限制条件简介

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

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

政策继承

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

限制

  • 不建议在自定义限制条件中使用存储分区标签。请改用标记,标记只能由具有所需 Identity and Access Management (IAM) 角色的个人设置,所受的控制比标签更严格。

  • 新强制执行的自定义限制条件不会自动应用于现有资源。必须更新现有资源才能应用该约束条件。

    如需查找需要更新的现有资源,您可以强制执行试运行组织政策

  • 自定义限制条件不能用于限制对象或存储分区的 ACL 或 IAM 政策。

准备工作

  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.

    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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Install the Google Cloud CLI.

  5. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  6. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  7. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  8. Verify that billing is enabled for your Google Cloud project.

  9. Install the Google Cloud CLI.

  10. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  11. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  12. 请确保您知道您的组织 ID
  13. 所需的角色

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

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

这些预定义角色包含管理组织政策所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

管理组织政策需要以下权限:

  • 针对组织的 orgpolicy.* 权限
  • 通过创建以下存储桶来测试本页面上的组织政策示例: 针对项目的 storage.buckets.create

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

创建自定义限制条件

自定义限制条件是在 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.bucketNamingRequirement。该字段的最大长度为 70 个字符。

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

  • CONDITION:针对受支持的服务资源的表示法编写的 CEL 条件。此字段的长度上限为 1,000 个字符。 如需详细了解可用于针对其编写条件的资源,请参阅支持的资源。 例如 "resource.name.matches('^[a-zA-Z]+$')"

  • 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.bucketNamingRequirement

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

    gcloud org-policies set-policy POLICY_PATH     

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

测试自定义组织政策

以下示例创建自定义限制条件和政策,要求所有新创建的存储分区的名称只能包含字母。您可以尝试执行该政策应禁止的操作来测试该政策。

创建限制条件

  1. 将以下文件保存为 constraint-bucket-names.yaml

    name: organizations/ORGANIZATION_ID/customConstraints/custom.bucketNamingRequirement resource_types: storage.googleapis.com/Bucket method_types: - CREATE condition: "resource.name.matches('^[a-zA-Z]+$')" action_type: ALLOW display_name: Bucket names must match the specified regular expression description: Newly created buckets must have a name that matches the specified regular expression. Only letters are allowed in the bucket name. 

    ORGANIZATION_ID 替换为您的组织 ID。

  2. 应用限制条件:

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

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

    输出类似于以下内容:

    CUSTOM_CONSTRAINT                 ACTION_TYPE    METHOD_TYPES    RESOURCE_TYPES                  DISPLAY_NAME custom.bucketNamingRequirement    ALLOW          CREATE          storage.googleapis.com/Bucket   Bucket names must match the specified regular expression ... 

创建政策

  1. 将以下文件保存为 policy-bucket-names.yaml

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

    PROJECT_ID 替换为您的项目 ID。

    在此示例中,您在项目级层强制执行此限制条件,但您也可以在组织或文件夹级层设置此限制条件。

  2. 应用政策:

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

    gcloud org-policies list --project=PROJECT_ID 

    输出类似于以下内容:

    CONSTRAINT                        LIST_POLICY    BOOLEAN_POLICY    ETAG custom.bucketNamingRequirement    -              SET               CIqktscGELiZn6cC- 

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

测试政策

  • 尝试创建名称中包含非字母字符的存储桶:

    gcloud storage buckets create gs://example-bucket --location=BUCKET_LOCATION 

    BUCKET_LOCATION 替换为相应存储桶的位置。例如 US

    请求失败,并显示类似于以下内容的错误:

    ERROR: (gcloud.storage.buckets.create) HTTPError 412: orgpolicy:projects/_/buckets/example-bucket violates customConstraints/custom.bucketNamingRequirement. Details: Newly created buckets must have a name that matches the specified regular expression. Only letters are allowed in the bucket name. 

常见用例的自定义组织政策示例

下表提供了一些常见自定义限制条件的语法示例。 如需查看可用于 Cloud Storage 的预定义限制条件列表,请参阅 Cloud Storage 的组织政策限制条件

说明 限制条件语法
存储桶必须启用对象版本控制
  name: organizations/ORGANIZATION_ID/customConstraints/custom.enforceBucketVersioning   method_types:   - CREATE   - UPDATE   resource_types: storage.googleapis.com/Bucket   condition: "resource.versioning.enabled == true"   action_type: ALLOW   display_name: Buckets must have Object Versioning enabled   description: Newly created buckets and newly updated buckets must have Object Versioning enabled.
存储桶必须使用特定的正则表达式命名
  name: organizations/ORGANIZATION_ID/customConstraints/custom.bucketNamingRequirement   method_types:   - CREATE   resource_types: storage.googleapis.com/Bucket   condition: "resource.name.matches('^[a-zA-Z]+$')"   action_type: ALLOW   display_name: Bucket names must match the specified regular expression   description: Newly created buckets must have a name that matches the specified regular expression. Only letters are allowed in the bucket name.
存储桶无法启用存储桶锁定
  name: organizations/ORGANIZATION_ID/customConstraints/custom.prohibitBucketLock   method_types:   - CREATE   - UPDATE   resource_types: storage.googleapis.com/Bucket   condition: "resource.retentionPolicy.isLocked == true"   action_type: DENY   display_name: Prohibit the use of Bucket Lock   description: Newly created buckets and newly updated buckets cannot have Bucket Lock enabled.
存储桶无法启用对象保留锁定
  name: organizations/ORGANIZATION_ID/customConstraints/custom.prohibitObjectRetentionLock   method_types:   - CREATE   - UPDATE   resource_types: storage.googleapis.com/Bucket   condition: "resource.objectRetention.mode == 'Enabled'"   action_type: DENY   display_name: Objects cannot have retention configurations   description: Newly created buckets and newly updated buckets cannot have Object Retention Lock enabled.
位于 USEU 多区域的存储分区的保留期限必须为 86,400 秒
  name: organizations/ORGANIZATION_ID/customConstraints/custom.locationRetentionPolicy   method_types:   - CREATE   - UPDATE   resource_types: storage.googleapis.com/Bucket   condition: "(resource.location.startsWith('US') || resource.location.startsWith('EU')) && resource.retentionPolicy.retentionPeriod != 86400"   action_type: DENY   display_name: All buckets in US and EU must have a retention policy of 86,400 seconds   description: Newly created buckets and newly updated buckets located in US and EU regions must have a retention policy of 86,400 seconds.
存储桶必须具有标签1
  name: organizations/ORGANIZATION_ID/customConstraints/custom.labels   method_types:   - CREATE   - UPDATE   resource_types: storage.googleapis.com/Bucket   condition: "'my_annotations.data.source' in resource.labels && resource.labels['my_annotations.data.source'] in ['SOURCE_IMAGES','SOURCE_TEXT','SOURCE_VIDEOS']"   action_type: ALLOW   display_name: Buckets must have a label classifying the contents of the bucket   description: Newly created buckets and newly updated buckets must have the label my_annotations.data.source with the SOURCE_IMAGES, SOURCE_TEXT, or SOURCE_VIDEOS key.
存储桶必须位于双区域
  name: organizations/ORGANIZATION_ID/customConstraints/custom.dualRegionUS   method_types:   - CREATE   - UPDATE   resource_types: storage.googleapis.com/Bucket   condition: "'US-EAST1' in resource.customPlacementConfig.dataLocations && 'US-EAST4' in resource.customPlacementConfig.dataLocations"   action_type: ALLOW   display_name: Buckets must be located in a dual-region   description: Newly created buckets and newly updated buckets must be located in a dual-region composed of the us-east1 and us-east4 regions.
存储桶不能使用旧存储类别
  name: organizations/ORGANIZATION_ID/customConstraints/custom.disableLegacyStorageClass   method_types:   - CREATE   - UPDATE   resource_types: storage.googleapis.com/Bucket   condition: "resource.storageClass in ['STANDARD', 'NEARLINE', 'COLDLINE', 'ARCHIVE']"   action_type: ALLOW   display_name: Buckets cannot use legacy storage classes   description: Newly created buckets and newly updated buckets must use Standard storage, Nearline storage, Coldline storage, or Archive storage.
存储桶 IP 过滤必须限制来自所有公共互联网的请求
  name: organizations/ORGANIZATION_ID/customConstraints/custom.IpFilter   method_types:   - CREATE   resource_types: storage.googleapis.com/Bucket   condition: "!has(resource.ipFilter) || (resource.ipFilter.mode == 'Disabled' || resource.ipFilter.publicNetworkSource.allowedIpCidrRanges.size() > 0)"   action_type: DENY   display_name: Bucket IP filter rules must restrict all the public network   description: Newly created buckets must have IP filtering and IP filtering rules must restrict all public network resources. 

1 如果指定不存在的存储桶标签键,则系统会返回 BAD_CONDITION 错误。此错误会阻止 CREATEUPDATE 方法在存储桶上运行。为避免此错误,请始终先使用 "my_annotations.data.source" in resource.labels 检查该标签键是否存在。

有条件的组织政策

您可以使用标记使自定义组织政策成为有条件的政策。如需了解详情,请参阅设置带有标记的组织政策

Cloud Storage 支持的资源

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

资源 字段
storage.googleapis.com/Bucket resource.billing.requesterPays
resource.cors.maxAgeSeconds
resource.cors.method
resource.cors.origin
resource.cors.responseHeader
resource.customPlacementConfig.dataLocations
resource.defaultEventBasedHold
resource.encryption.defaultKmsKeyName
resource.iamConfiguration.publicAccessPrevention
resource.iamConfiguration.uniformBucketLevelAccess.enabled
resource.ipFilter.mode
resource.ipFilter.publicNetworkSource.allowedIpCidrRanges
resource.ipFilter.vpcNetworkSources.allowedIpCidrRanges
resource.ipFilter.vpcNetworkSources.network
resource.labels
resource.lifecycle.rule.action.storageClass
resource.lifecycle.rule.action.type
resource.lifecycle.rule.condition.age
resource.lifecycle.rule.condition.createdBefore
resource.lifecycle.rule.condition.customTimeBefore
resource.lifecycle.rule.condition.daysSinceCustomTime
resource.lifecycle.rule.condition.daysSinceNoncurrentTime
resource.lifecycle.rule.condition.isLive
resource.lifecycle.rule.condition.matchesPrefix
resource.lifecycle.rule.condition.matchesStorageClass
resource.lifecycle.rule.condition.matchesSuffix
resource.lifecycle.rule.condition.noncurrentTimeBefore
resource.lifecycle.rule.condition.numNewerVersions
resource.location
resource.locationType
resource.logging.logBucket
resource.logging.logObjectPrefix
resource.name
resource.objectRetention.mode
resource.retentionPolicy.isLocked
resource.retentionPolicy.retentionPeriod
resource.rpo
resource.softDeletePolicy.retentionDurationSeconds
resource.storageClass
resource.versioning.enabled
resource.website.mainPageSuffix
resource.website.notFoundPage

请注意以下几点:

  • 您必须使用大写字母来指定以下字段的值:
    • resource.customPlacementConfig.dataLocations
    • resource.lifecycle.rule.action.storageClass
    • resource.location
    • resource.storageClass
  • resource.retentionPolicy.isLocked 字段只能用于禁止使用存储分区锁定,而不能用于强制执行存储分区锁定。

后续步骤