本页面介绍了如何对 Cloud SQL 实例创建、设置和管理自定义限制条件。如需简要了解自定义组织政策,请参阅自定义组织政策。
Cloud SQL 支持的资源
本页面介绍了如何使用组织政策服务自定义限制条件来限制对以下 Google Cloud 资源执行的特定操作:
sqladmin.googleapis.com/Instancesqladmin.googleapis.com/BackupRun
如需详细了解组织政策,请参阅自定义组织政策。
组织政策和限制条件简介
借助 Google Cloud 组织政策服务,您可以对组织的资源进行程序化集中控制。作为组织政策管理员,您可以定义组织政策,这是一组称为限制条件的限制,会应用于 Google Cloud 资源层次结构中的Google Cloud 资源及其后代。您可以在组织、文件夹或项目级强制执行组织政策。
组织政策为各种 Google Cloud 服务提供内置的托管式限制。但是,如果您想要更精细地控制和自定义组织政策中受限的特定字段,还可以创建自定义限制条件并在组织政策中使用这些自定义限制条件。
政策继承
如果您对资源强制执行政策,默认情况下,该资源的后代会继承组织政策。例如,如果您对某个文件夹强制执行一项政策, Google Cloud 会对该文件夹中的所有项目强制执行该政策。如需详细了解此行为及其更改方式,请参阅层次结构评估规则。
准备工作
- 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.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Install the gcloud 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 (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Install the gcloud CLI.
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init - 请确保您知道您的组织 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.region。该字段的最大长度为 70 个字符。RESOURCE_NAME:包含要限制的对象和字段的Google Cloud 资源的完全限定名称。例如sqladmin.googleapis.com/Instance。CONDITION:针对受支持的服务资源的表示法编写的 CEL 条件。此字段的长度上限为 1,000 个字符。 如需详细了解可用于针对其编写条件的资源,请参阅支持的资源。 例如"resource.region == "us-central1""。ACTION:满足condition时要执行的操作。 可能的值包括ALLOW和DENY。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 资源。控制台
- 在 Google Cloud 控制台中,前往组织政策页面。
- 在项目选择器中,选择要设置组织政策的项目。
- 从组织政策页面上的列表中选择您的限制条件,以查看该限制条件的政策详情页面。
- 如需为该资源配置组织政策,请点击管理政策。
- 在修改政策页面,选择覆盖父级政策。
- 点击添加规则。
- 在强制执行部分中,选择开启还是关闭此组织政策的强制执行。
- (可选)如需使组织政策成为基于某个标记的条件性政策,请点击添加条件。请注意,如果您向组织政策添加条件规则,则必须至少添加一个无条件规则,否则无法保存政策。如需了解详情,请参阅设置带有标记的组织政策。
- 点击测试更改以模拟组织政策的效果。政策模拟不适用于旧版托管式限制。如需了解详情,请参阅使用 Policy Simulator 测试组织政策更改。
- 若要完成并应用组织政策,请点击设置政策。该政策最长需要 15 分钟才能生效。
gcloud
如需创建包含布尔值规则的组织政策,请创建引用该限制条件的 YAML 政策文件:
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME spec: rules: - enforce: true
请替换以下内容:
-
PROJECT_ID:要对其实施限制条件的项目。 -
CONSTRAINT_NAME:您为自定义限制条件定义的名称。例如,custom.region。
如需强制执行包含限制条件的组织政策,请运行以下命令:
gcloud org-policies set-policy POLICY_PATH
将 POLICY_PATH 替换为组织政策 YAML 文件的完整路径。该政策最长需要 15 分钟才能生效。
常见用例的自定义组织政策示例
下表提供了一些常见自定义限制条件的语法示例。
| 说明 | 限制条件语法 |
|---|---|
| 确保实例为企业 Plus 版 | name: organizations/ORGANIZATION_ID/customConstraints/custom.edition resourceTypes: - sqladmin.googleapis.com/Instance methodTypes: - CREATE - UPDATE condition: "resource.settings.edition == 'ENTERPRISE_PLUS'" actionType: ALLOW displayName: Instances must be Enterprise Plus. description: Cloud SQL instances must be Enterprise Plus. |
| 确保实例具备高可用性 | name: organizations/ORGANIZATION_ID/customConstraints/custom.availability resourceTypes: - sqladmin.googleapis.com/Instance methodTypes: - CREATE - UPDATE condition: "resource.settings.availabilityType == 'REGIONAL'" actionType: ALLOW displayName: Instances must be regional. description: Cloud SQL instances must be highly available. To do this, make them regional. |
| 要求仅在使用加密连接时启用 SSL 模式。 | name: organizations/ORGANIZATION_ID/customConstraints/custom.sslMode resourceTypes: - sqladmin.googleapis.com/Instance methodTypes: - CREATE - UPDATE condition: "resource.settings.ipConfiguration.sslMode == 'ENCRYPTED_ONLY'" actionType: ALLOW displayName: Only allow connections that are encrypted with SSL/TLS. description: Cloud SQL instances must only allow connections that are encrypted with SSL/TLS. |
| 阻止从互联网上的任何位置访问数据库。 | name: organizations/ORGANIZATION_ID/customConstraints/custom.authorizedNetworks resourceTypes: - sqladmin.googleapis.com/Instance methodTypes: - CREATE - UPDATE condition: "resource.settings.ipConfiguration.authorizedNetworks.exists(network, network.value == '0.0.0.0/0')" actionType: DENY displayName: The list of authorized networks can't contain 0.0.0.0/0 description: Authorized networks for Cloud SQL instances can't contain 0.0.0.0/0. This allows users to access the database from any IP address. |
| 确保创建实例时未使用外部 IP 地址 | name: organizations/ORGANIZATION_ID/customConstraints/custom.ipv4Enabled resourceTypes: - sqladmin.googleapis.com/Instance methodTypes: - CREATE - UPDATE condition: "resource.settings.ipConfiguration.ipv4Enabled == false" actionType: ALLOW displayName: ipv4Enabled must be set to false. description: Cloud SQL instances can't have an external IP address. |
| 确保使用 KMS 密钥对实例进行加密。 | name: organizations/ORGANIZATION_ID/customConstraints/custom.encrypted resourceTypes: - sqladmin.googleapis.com/Instance methodTypes: - CREATE - UPDATE condition: "resource.diskEncryptionConfiguration.kmsKeyName.size() > 0" actionType: ALLOW displayName: Instances must be encrypted with a KMS key. description: Cloud SQL instances must be disk-encrypted with a KMS key. |
| 确保实例位于 us-central1 | name: organizations/ORGANIZATION_ID/customConstraints/custom.region resourceTypes: - sqladmin.googleapis.com/Instance methodTypes: - CREATE - UPDATE condition: "resource.region == 'us-central1'" actionType: ALLOW displayName: Instances must be in the us-central1 region. description: Cloud SQL instances must be in the us-central1 region. |
| 确保实例已启用数据缓存。 | name: organizations/ORGANIZATION_ID/customConstraints/custom.dataCache resourceTypes: - sqladmin.googleapis.com/Instance methodTypes: - CREATE - UPDATE condition: "resource.settings.dataCacheConfig.dataCacheEnabled == true" actionType: ALLOW displayName: Instances must have data cache enabled. description: Cloud SQL instances must have data cache enabled. |
| 强制执行自动备份。 | name: organizations/ORGANIZATION_ID/customConstraints/custom.enableBackups resourceTypes: - sqladmin.googleapis.com/Instance methodTypes: - CREATE - UPDATE condition: "resource.settings.backupConfiguration.enabled == true" actionType: ALLOW displayName: Automated backups must be enabled. description: Cloud SQL instances must have automated backups enabled. |
| 限制自动备份的存储位置。 | name: organizations/ORGANIZATION_ID/customConstraints/custom.automatedBackupLocation resourceTypes: - sqladmin.googleapis.com/Instance methodTypes: - CREATE - UPDATE condition: "resource.settings.backupConfiguration.location == 'us-central1'" actionType: ALLOW displayName: The location of automated backups must be in the us-central1 region. description: The location of automated backups for Cloud SQL instances must be in the us-central1 region. |
| 将按需备份的位置限制为 us-central1 | name: organizations/ORGANIZATION_ID/customConstraints/custom.onDemandBackupLocation resourceTypes: - sqladmin.googleapis.com/BackupRun methodTypes: - CREATE - UPDATE condition: "condition: resource.location == 'us-central1'" actionType: ALLOW displayName: The location of on-demand backups must be in the us-central1 region. description: On-demand backups are restricted to the us-central1. |
| 在删除实例时强制执行最终备份 | name: organizations/ORGANIZATION_ID/customConstraints/custom.retainBackupsOnDelete resourceTypes: - sqladmin.googleapis.com/Instance methodTypes: - CREATE - UPDATE condition: "condition: resource.settings.finalBackupConfig.enabled == true" actionType: ALLOW displayName: Final backup is enabled and must be taken at instance deletion. description: Final backups are enabled for instance. |
| 在实例删除保留期限内强制执行最终备份 | name: organizations/ORGANIZATION_ID/customConstraints/custom.retainBackupsOnDelete resourceTypes: - sqladmin.googleapis.com/Instance methodTypes: - CREATE - UPDATE condition: "condition: resource.settings.finalBackupConfig.enabled == true && resource.settings.finalBackupConfig.retentionDays == 20" actionType: ALLOW displayName: Final backup is enabled and must be retained for 20 days after instance deletion. description: Final backups is retained for 20 days after instance deletion. |
Cloud SQL for SQL Server 支持的资源
下表列出了您可以在自定义限制条件中引用的 Cloud SQL for SQL Server 资源。| 资源 | 字段 |
|---|---|
| sqladmin.googleapis.com/BackupRun | resource.description |
resource.location | |
| sqladmin.googleapis.com/Instance | resource.databaseVersion |
resource.diskEncryptionConfiguration.kmsKeyName | |
resource.diskEncryptionStatus.kmsKeyVersionName | |
resource.failoverReplica.name | |
resource.masterInstanceName | |
resource.name | |
resource.nodeCount | |
resource.project | |
resource.region | |
resource.replicaConfiguration.cascadableReplica | |
resource.replicaConfiguration.failoverTarget | |
resource.settings.activationPolicy | |
resource.settings.activeDirectoryConfig.domain | |
resource.settings.advancedMachineFeatures.threadsPerCore | |
resource.settings.availabilityType | |
resource.settings.backupConfiguration.backupRetentionSettings.retainedBackups | |
resource.settings.backupConfiguration.backupRetentionSettings.retentionUnit | |
resource.settings.backupConfiguration.binaryLogEnabled | |
resource.settings.backupConfiguration.enabled | |
resource.settings.backupConfiguration.location | |
resource.settings.backupConfiguration.pointInTimeRecoveryEnabled | |
resource.settings.backupConfiguration.startTime | |
resource.settings.backupConfiguration.transactionLogRetentionDays | |
resource.settings.collation | |
resource.settings.connectionPoolConfig.connectionPoolingEnabled | |
resource.settings.connectionPoolConfig.flags.name | |
resource.settings.connectionPoolConfig.flags.value | |
resource.settings.connectorEnforcement | |
resource.settings.databaseFlags.name | |
resource.settings.databaseFlags.value | |
resource.settings.dataCacheConfig.dataCacheEnabled | |
resource.settings.dataDiskProvisionedIops | |
resource.settings.dataDiskProvisionedThroughput | |
resource.settings.dataDiskSizeGb | |
resource.settings.dataDiskType | |
resource.settings.deletionProtectionEnabled | |
resource.settings.denyMaintenancePeriods.endDate | |
resource.settings.denyMaintenancePeriods.startDate | |
resource.settings.denyMaintenancePeriods.time | |
resource.settings.edition | |
resource.settings.enableDataplexIntegration | |
resource.settings.enableGoogleMlIntegration | |
resource.settings.insightsConfig.queryInsightsEnabled | |
resource.settings.insightsConfig.queryPlansPerMinute | |
resource.settings.insightsConfig.queryStringLength | |
resource.settings.insightsConfig.recordApplicationTags | |
resource.settings.insightsConfig.recordClientAddress | |
resource.settings.ipConfiguration.authorizedNetworks.name | |
resource.settings.ipConfiguration.authorizedNetworks.value | |
resource.settings.ipConfiguration.customSubjectAlternativeNames | |
resource.settings.ipConfiguration.enablePrivatePathForGoogleCloudServices | |
resource.settings.ipConfiguration.ipv4Enabled | |
resource.settings.ipConfiguration.privateNetwork | |
resource.settings.ipConfiguration.pscConfig.allowedConsumerProjects | |
resource.settings.ipConfiguration.pscConfig.pscAutoConnections.consumerNetwork | |
resource.settings.ipConfiguration.pscConfig.pscAutoConnections.consumerProject | |
resource.settings.ipConfiguration.pscConfig.pscEnabled | |
resource.settings.ipConfiguration.serverCaMode | |
resource.settings.ipConfiguration.serverCaPool | |
resource.settings.ipConfiguration.sslMode | |
resource.settings.locationPreference.secondaryZone | |
resource.settings.locationPreference.zone | |
resource.settings.maintenanceWindow.day | |
resource.settings.maintenanceWindow.hour | |
resource.settings.maintenanceWindow.updateTrack | |
resource.settings.replicationLagMaxSeconds | |
resource.settings.retainBackupsOnDelete | |
resource.settings.sqlServerAuditConfig.bucket | |
resource.settings.sqlServerAuditConfig.retentionInterval | |
resource.settings.sqlServerAuditConfig.uploadInterval | |
resource.settings.storageAutoResize | |
resource.settings.tier | |
resource.settings.timeZone |