カスタム制約を使用して 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. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して 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. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  11. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  12. 組織 ID を確認します。
  13. 必要なロール

    組織のポリシーを管理するために必要な権限を取得するには、次の IAM ロールを付与するように管理者に依頼してください。

    ロールの付与については、プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。

    これらの事前定義ロールには、組織のポリシーの管理に必要な権限が含まれています。必要とされる正確な権限については、「必要な権限」セクションを開いてご確認ください。

    必要な権限

    組織のポリシーを管理するには、次の権限が必要です。

    • 組織に対する orgpolicy.*
    • このページの組織のポリシーの例をテストします。プロジェクトに対する storage.buckets.create

    カスタムロールや他の事前定義ロールを使用して、これらの権限を取得することもできます。

    カスタム制約を作成する

    カスタム制約は、組織のポリシーを適用しているサービスでサポートされるリソース、メソッド、条件、アクションを使用して 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.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. [ポリシーの編集] ページで、[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.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がポリシーの適用を開始するまで 2 分ほど待ちます。

    ポリシーのテスト

    • 文字以外の文字を含む名前でバケットを作成してみます。

      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.
    US または EU マルチリージョンにあるバケットの保持期間は 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 エラーが返されます。このエラーにより、CREATE メソッドと UPDATE メソッドがバケットで実行されなくなります。このエラーを回避するには、必ず "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 フィールドは、バケットロックの使用を禁止するためにのみ使用できます。バケットロックを適用することはできません。

    次のステップ