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

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

  • firestore.googleapis.com/Database

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

组织政策和限制条件简介

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

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

政策继承

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

优势

  • 安全、合规性和治理:您可以使用自定义组织政策,具体如下所示:

    • 为了强制执行灾难恢复要求,您可以要求数据库采用特定的灾难恢复设置,例如删除保护和时间点恢复。

    • 您可以将数据库创建限制为仅在某些位置进行。

    • 您可以要求数据库使用 CMEK(客户管理的加密密钥)。

  • 审核:自定义组织政策限制条件会记录在审核日志中。任何操作(包括限制修改和限制检查)都会生成相应的 Cloud Audit Logs。

准备工作

  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. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  6. To initialize the gcloud CLI, run the following command:

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

    Go to project selector

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

  9. Install the Google Cloud CLI.

  10. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  11. To initialize the gcloud CLI, run the following command:

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

    如需获得管理自定义组织政策所需的权限,请让您的管理员为您授予组织资源的 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.deleteProtectionRequired。该字段的最大长度为 70 个字符。

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

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

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

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

        gcloud org-policies set-policy POLICY_PATH
        

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

    测试自定义组织政策

    在开始之前,您必须了解以下信息:

    • 您的组织 ID
    1. 创建 deleteProtectionRequired.yaml 文件,如下所示:

       name: organizations/ORGANIZATION_ID/customConstraints/custom.deleteProtectionRequired
       resourceTypes:
       - firestore.googleapis.com/Database
       methodTypes:
       - CREATE
       - UPDATE
       condition: "resource.deleteProtectionState == \"DELETE_PROTECTION_ENABLED\""
       actionType: ALLOW
       displayName: Firestore Delete Protection Required
       description: To ensure the data security, Delete Protection is required to be enabled for Firestore databases.
      

      这可确保对 Firestore 数据库执行的所有 CREATEUPDATE 方法都满足 deleteProtectionStateDELETE_PROTECTION_ENABLED 这一限制条件。因此,任何未明确启用删除保护的数据库创建/更新/恢复/克隆操作都会被拒绝。

    2. 在组织级层设置自定义限制条件:

      gcloud org-policies set-custom-constraint deleteProtectionRequired.yaml
      

    测试政策

    尝试在组织中的项目中创建数据库,但不设置 --delete-protection 标志:

    gcloud firestore database create \
       --project=PROJECT_ID \
       --database=DATABASE_ID \
    

    输出如下所示:

    Operation denied by custom org policies: ["customConstraints/custom.deleteProtectionRequired": "To ensure the data security, Delete Protection is required to be enabled for Firestore databases"]
    

    测试和分析组织政策变更

    我们建议您测试并试运行对组织政策的所有更改,以便更好地了解环境的状态以及更改对环境的影响。

    组织 Policy Simulator 可帮助您了解限制条件和组织政策对当前环境的影响。使用此工具,您可以在资源配置强制应用于生产环境之前,查看所有资源配置,了解哪些配置存在违规情况。如需查看详细说明,请参阅使用 Policy Simulator 测试组织政策更改

    了解当前效果后,您可以在试运行模式下创建组织政策,以了解政策在未来 30 天内的影响和潜在违规情况。处于试运行模式下的组织政策是一种组织政策,违反该政策的行为会被记录在审核日志中,但不会被拒绝。您可以使用 Google Cloud 控制台或 Google Cloud CLI,根据自定义限制条件以试运行模式创建组织政策。如需查看详细说明,请参阅以试运行模式创建组织政策

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

    下表列出了一些常见用例的自定义限制条件的语法:

    说明 限制条件语法
    数据库名称必须遵循特定模式。请注意,自定义组织政策中的数据库名称格式为 projects/project-id/databases/database-id,而在数据库管理操作中仅指定 database-id
    name: organizations/ORGANIZATION_ID/customConstraints/custom.nameSuffixMobile
    resourceTypes:
    - firestore.googleapis.com/Database
    methodTypes:
    - CREATE
    condition: "resource.name.endsWith('-mobile')"
    actionType: ALLOW
    displayName: Firestore database names end with "-mobile"
    description: Only allow the creation of database names ending with suffix "-mobile"
    数据库只能在指定的位置创建。
    name: organizations/ORGANIZATION_ID/customConstraints/custom.locationUsCentral1
    resourceTypes:
    - firestore.googleapis.com/Database
    methodTypes:
    - CREATE
    condition: "resource.locationId == 'us-central1'"
    actionType: ALLOW
    displayName: Firestore database location id us-central1
    description: Only allow the creation of databases in region us-central1
    数据库必须属于指定的类型
    name: organizations/ORGANIZATION_ID/customConstraints/custom.typeFirestore
    resourceTypes:
    - firestore.googleapis.com/Database
    methodTypes:
    - CREATE
    - UPDATE
    condition: "resource.type=="FIRESTORE_NATIVE""
    actionType: ALLOW
    displayName: Firestore database type Firestore_Native
    description: Only allow creation and updating of databases if the type is Firestore in native mode.
    数据库必须使用指定的并发模式
    name: organizations/ORGANIZATION_ID/customConstraints/custom.concurrencyNotPessimistic
    resourceTypes:
    - firestore.googleapis.com/Database
    methodTypes:
    - CREATE
    - UPDATE
    condition: "resource.concurrencyMode == 'PESSIMISTIC'"
    actionType: DENY
    displayName: Firestore database concurrencyMode not pessimistic
    description: Disallow the creation and updating of databases with pessimistic concurrency mode.
    数据库必须启用point-in-time-recovery
    name: organizations/ORGANIZATION_ID/customConstraints/custom.pitrEnforce
    resourceTypes:
    - firestore.googleapis.com/Database
    methodTypes:
    - CREATE
    - UPDATE
    condition: "resource.pointInTimeRecoveryEnablement == "POINT_IN_TIME_RECOVERY_ENABLED""
    actionType: ALLOW
    displayName: Firestore database enables PiTR
    description: Only allow the creation and updating of a databases if PiTR is enabled.
    数据库必须使用指定的 App Engine 集成模式
    name: organizations/ORGANIZATION_ID/customConstraints/custom.enableAppEngineIntegrationMode
    resourceTypes:
    - firestore.googleapis.com/Database
    methodTypes:
    - CREATE
    - UPDATE
    condition: "resource.appEngineIntegrationMode == 'ENABLED'"
    actionType: ALLOW
    displayName: Firestore Database App Engine integration mode enabled
    description: Only allow the creation and updating of databases with App Engine Integration Mode enabled.
    不允许创建数据库,除非已启用删除保护。
    name: organizations/ORGANIZATION_ID/customConstraints/custom.deleteProtectionRequired
    resourceTypes:
    - firestore.googleapis.com/Database
    methodTypes:
    - CREATE
    - UPDATE
    condition: "resource.deleteProtectionState == "DELETE_PROTECTION_ENABLED""
    actionType: ALLOW
    displayName: Firestore Delete Protection Required
    description: To ensure the data security, Delete Protection is required to be enabled for Firestore databases.
        
    数据库必须使用指定的 CMEK(客户管理的加密密钥)配置
    name: organizations/ORGANIZATION_ID/customConstraints/custom.cmekKeyNotDev
    resourceTypes:
    - firestore.googleapis.com/Database
    methodTypes:
    - CREATE
    - UPDATE
    condition: "resource.cmekConfig.kmsKeyName.matches('dev$')"
    actionType: DENY
    displayName: Firestore database CMEK key not dev
    description: Disallow the creation and updating of databases with CMEK KMS keys ending with "dev".

    Firestore 支持的资源

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

    资源 字段
    firestore.googleapis.com/Database resource.appEngineIntegrationMode
    resource.cmekConfig.kmsKeyName
    resource.concurrencyMode
    resource.deleteProtectionState
    resource.locationId
    resource.name
    resource.pointInTimeRecoveryEnablement
    resource.type

    后续步骤