カスタムの組織のポリシーを追加する

このページでは、Cloud SQL インスタンスでカスタム制約を作成、設定、管理する方法について説明します。カスタムの組織のポリシーの概要については、カスタムの組織のポリシーをご覧ください。

Cloud SQL でサポートされているリソース

このページでは、組織のポリシー サービスのカスタム制約を使用して、次の Google Cloud リソースに対する特定のオペレーションを制限する方法について説明します。

  • sqladmin.googleapis.com/Instance
  • sqladmin.googleapis.com/BackupRun

組織のポリシーの詳細については、カスタムの組織のポリシーをご覧ください。

組織のポリシーと制約について

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 gcloud 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 gcloud 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. 必要なロール

    カスタムの組織のポリシーを管理するために必要な権限を取得するには、組織のリソースに対する組織のポリシー管理者roles/orgpolicy.policyAdmin)の IAM ロールを付与するよう管理者に依頼してください。ロールの付与については、プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。

    必要な権限は、カスタムロールや他の事前定義ロールから取得することもできます。

    カスタム制約を作成する

    カスタム制約は、組織のポリシーを適用しているサービスでサポートされるリソース、メソッド、条件、アクションを使用して YAML ファイルで定義されます。カスタム制約の条件は、Common Expression Language(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 が満たされている場合に実行するアクション。有効な値は 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. [ポリシーの編集] ページで、[Override parent's policy] を選択します。
    6. [ルールを追加] をクリックします。
    7. [適用] セクションで、この組織のポリシーの適用を有効にするかどうかを選択します。
    8. 省略可: タグで組織のポリシーに条件を設定するには、[条件を追加] をクリックします。組織のポリシーに条件付きルールを追加する場合は、少なくとも 1 つは無条件のルールを追加する必要があります。そうしないとポリシーを保存できないのでご注意ください。詳細については、タグ付きの組織のポリシーの設定をご覧ください。
    9. [変更内容をテスト] をクリックして、組織のポリシーの効果をシミュレートします。以前のマネージド制約ではポリシー シミュレーションを使用できません。詳細については、Policy Simulator で組織のポリシーの変更をテストするをご覧ください。
    10. 組織のポリシーを完成させて適用するには、[ポリシーを設定] をクリックします。ポリシーが有効になるまでに最大 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 分かかります。

    一般的なユースケースのカスタム組織のポリシーの例

    次の表に、一般的なユースケースのカスタム制約の構文を示します。

    説明 制約の構文
    インスタンスが Enterprise 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.
        
    インスタンスにパスワード ポリシーがあることを確認する
        name: organizations/ORGANIZATION_ID/customConstraints/custom.enforcePassword
        resourceTypes:
        - sqladmin.googleapis.com/Instance
        methodTypes:
        - CREATE
        - UPDATE
        condition: "resource.settings.passwordValidationPolicy.enablePasswordPolicy == true"
        actionType: ALLOW
        displayName: The password policy must be enabled for Cloud SQL instances.
        description: Cloud SQL instances must have a password policy.
        
    インスタンスのパスワード ポリシーに最小長が含まれていることを確認する
        name: organizations/ORGANIZATION_ID/customConstraints/custom.passwordMinLength
        resourceTypes:
        - sqladmin.googleapis.com/Instance
        methodTypes:
        - CREATE
        - UPDATE
        condition: "resource.settings.passwordValidationPolicy.minLength >= 6"
        actionType: ALLOW
        displayName: The password policy must have a minimum length of six characters.
        description: Cloud SQL instances must have a password policy that has a minimum length of six characters.
        
    インスタンスでパスワード ポリシーの複雑さが有効になっていることを確認する
        name: organizations/ORGANIZATION_ID/customConstraints/custom.passwordComplexity
        resourceTypes:
        - sqladmin.googleapis.com/Instance
        methodTypes:
        - CREATE
        - UPDATE
        condition: "resource.settings.passwordValidationPolicy.complexity == 'COMPLEXITY_DEFAULT'"
        actionType: ALLOW
        displayName: The password policy complexity must be enabled.
        description: Cloud SQL instances must have a password policy with complex passwords.
        
    暗号化された接続でのみ 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.
        

    Cloud SQL for PostgreSQL でサポートされているリソース

    次の表に、カスタム制約で参照できる Cloud SQL for PostgreSQL リソースを記載します。

    リソース フィールド
    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.passwordValidationPolicy.complexity
    resource.settings.passwordValidationPolicy.disallowUsernameSubstring
    resource.settings.passwordValidationPolicy.enablePasswordPolicy
    resource.settings.passwordValidationPolicy.minLength
    resource.settings.passwordValidationPolicy.passwordChangeInterval
    resource.settings.passwordValidationPolicy.reuseInterval
    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

    次のステップ