カスタム制約を使用して Service Management リソースを管理する

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

  • servicemanagement.googleapis.com/Service

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

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

Google Cloud 組織のポリシー サービスを使用すると、組織のリソースをプログラマティックに一元管理できます。組織のポリシー管理者は組織のポリシーを定義できます。組織のポリシーは、Google Cloud リソース階層内のGoogle Cloud リソースやそれらのリソースの子孫に適用される、制約と呼ばれる一連の制限です。組織のポリシーは、組織レベル、フォルダレベル、またはプロジェクト レベルで適用できます。

組織のポリシーを利用することで、あらかじめ用意されたマネージド制約をさまざまな Google Cloud サービスに適用できます。ただし、組織のポリシーで制限されている特定の項目をより細かくカスタマイズして制御したい場合は、カスタム制約を作成して、それを組織のポリシーで使うこともできます。

ポリシーの継承

デフォルトでは、組織のポリシーは、そのポリシーを適用したリソースの子孫に継承されます。たとえば、フォルダにポリシーを適用した場合、 Google Cloud はそのフォルダ内のすべてのプロジェクトにそのポリシーを適用します。この動作の詳細と変更方法については、階層評価ルールをご覧ください。

始める前に

  1. Google Cloud アカウントにログインします。 Google Cloudを初めて使用する場合は、 アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
  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 role (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. 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 role (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. Google Cloud CLI をインストールします。

  10. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

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

    gcloud init
  12. 組織 ID を確認します。

必要なロール

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

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

Service Management でサポートされているリソース

次の表に、カスタム制約で参照できる Service Management リソースを示します。

リソース フィールド
servicemanagement.googleapis.com/Service resource.authentication.providers.audiences
resource.authentication.providers.authorizationUrl
resource.authentication.providers.issuer
resource.authentication.providers.jwksUri
resource.authentication.providers.jwtLocations.cookie
resource.authentication.providers.jwtLocations.header
resource.authentication.providers.jwtLocations.query
resource.authentication.providers.jwtLocations.valuePrefix
resource.authentication.rules.allowWithoutCredential
resource.authentication.rules.oauth.canonicalScopes
resource.authentication.rules.requirements.audiences
resource.authentication.rules.requirements.providerId
resource.authentication.rules.selector
resource.backend.rules.address
resource.backend.rules.deadline
resource.backend.rules.disableAuth
resource.backend.rules.jwtAudience
resource.backend.rules.operationDeadline
resource.backend.rules.pathTranslation
resource.backend.rules.protocol
resource.backend.rules.selector
resource.billing.consumerDestinations.metrics
resource.billing.consumerDestinations.monitoredResource
resource.context.rules.allowedRequestExtensions
resource.context.rules.allowedResponseExtensions
resource.context.rules.provided
resource.context.rules.requested
resource.context.rules.selector
resource.control.environment
resource.control.methodPolicies.requestPolicies.resourcePermission
resource.control.methodPolicies.requestPolicies.resourceType
resource.control.methodPolicies.requestPolicies.selector
resource.control.methodPolicies.selector
resource.customError.rules.isErrorType
resource.customError.rules.selector
resource.customError.types
resource.endpoints.aliases
resource.endpoints.allowCors
resource.endpoints.name
resource.endpoints.target
resource.logging.consumerDestinations.logs
resource.logging.consumerDestinations.monitoredResource
resource.logging.producerDestinations.logs
resource.logging.producerDestinations.monitoredResource
resource.logs.description
resource.logs.displayName
resource.logs.name
resource.metrics.description
resource.metrics.displayName
resource.metrics.launchStage
resource.metrics.metadata.ingestDelay
resource.metrics.metadata.samplePeriod
resource.metrics.metadata.timeSeriesResourceHierarchyLevel
resource.metrics.metricKind
resource.metrics.monitoredResourceTypes
resource.metrics.name
resource.metrics.type
resource.metrics.unit
resource.metrics.valueType
resource.monitoredResources.description
resource.monitoredResources.displayName
resource.monitoredResources.launchStage
resource.monitoredResources.name
resource.monitoredResources.type
resource.monitoring.consumerDestinations.metrics
resource.monitoring.consumerDestinations.monitoredResource
resource.monitoring.producerDestinations.metrics
resource.monitoring.producerDestinations.monitoredResource
resource.name
resource.producerProjectId
resource.publishing.apiShortName
resource.publishing.codeownerGithubTeams
resource.publishing.docTagPrefix
resource.publishing.documentationUri
resource.publishing.githubLabel
resource.publishing.librarySettings.cppSettings.common.destinations
resource.publishing.librarySettings.cppSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.dotnetSettings.common.destinations
resource.publishing.librarySettings.dotnetSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.dotnetSettings.forcedNamespaceAliases
resource.publishing.librarySettings.dotnetSettings.handwrittenSignatures
resource.publishing.librarySettings.dotnetSettings.ignoredResources
resource.publishing.librarySettings.dotnetSettings.renamedResources
resource.publishing.librarySettings.dotnetSettings.renamedServices
resource.publishing.librarySettings.goSettings.common.destinations
resource.publishing.librarySettings.goSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.javaSettings.common.destinations
resource.publishing.librarySettings.javaSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.javaSettings.libraryPackage
resource.publishing.librarySettings.javaSettings.serviceClassNames
resource.publishing.librarySettings.launchStage
resource.publishing.librarySettings.nodeSettings.common.destinations
resource.publishing.librarySettings.nodeSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.phpSettings.common.destinations
resource.publishing.librarySettings.phpSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.pythonSettings.common.destinations
resource.publishing.librarySettings.pythonSettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.pythonSettings.experimentalFeatures.restAsyncIoEnabled
resource.publishing.librarySettings.restNumericEnums
resource.publishing.librarySettings.rubySettings.common.destinations
resource.publishing.librarySettings.rubySettings.common.selectiveGapicGeneration.methods
resource.publishing.librarySettings.version
resource.publishing.methodSettings.autoPopulatedFields
resource.publishing.methodSettings.longRunning.initialPollDelay
resource.publishing.methodSettings.longRunning.maxPollDelay
resource.publishing.methodSettings.longRunning.pollDelayMultiplier
resource.publishing.methodSettings.longRunning.totalPollTimeout
resource.publishing.methodSettings.selector
resource.publishing.newIssueUri
resource.publishing.organization
resource.publishing.protoReferenceDocumentationUri
resource.publishing.restReferenceDocumentationUri
resource.quota.limits.defaultLimit
resource.quota.limits.description
resource.quota.limits.displayName
resource.quota.limits.duration
resource.quota.limits.freeTier
resource.quota.limits.maxLimit
resource.quota.limits.metric
resource.quota.limits.name
resource.quota.limits.unit
resource.quota.limits.values
resource.quota.metricRules.metricCosts
resource.quota.metricRules.selector
resource.systemParameters.rules.parameters.httpHeader
resource.systemParameters.rules.parameters.name
resource.systemParameters.rules.parameters.urlQueryParameter
resource.systemParameters.rules.selector
resource.title
resource.usage.producerNotificationChannel
resource.usage.requirements
resource.usage.rules.allowUnregisteredCalls
resource.usage.rules.selector
resource.usage.rules.skipServiceControl

カスタム制約を設定する

カスタム制約は、組織のポリシーを適用しているサービスでサポートされるリソース、メソッド、条件、アクションを使用して YAML ファイルで定義されます。カスタム制約の条件は、Common Expression Language(CEL)を使用して定義されます。CEL を使用してカスタム制約で条件を作成する方法については、カスタム制約の作成と管理の CEL セクションをご覧ください。

コンソール

カスタム制約を作成する手順は次のとおりです。

  1. Google Cloud コンソールで [組織のポリシー] ページに移動します。

    [組織のポリシー] に移動

  2. プロジェクト選択ツールで、組織のポリシーを設定するプロジェクトを選択します。
  3. [ カスタム制約] をクリックします。
  4. [表示名] ボックスに、人が読める形式で制約の名前を入力します。この名前はエラー メッセージで使用されるほか、識別やデバッグにも使用できます。エラー メッセージで公開される可能性があるため、表示名に個人を特定できる情報(PII)やセンシティブ データを使用しないでください。このフィールドには、最大 200 文字まで入力できます。
  5. [制約 ID] ボックスに、新しいカスタム制約の ID を入力します。カスタム制約の名前に使用できるのは、英字(大文字と小文字)と数字のみです(例: custom.authorizationCheck)。このフィールドには、接頭辞(custom.)を除き、最大 70 文字まで入力できます(例: organizations/123456789/customConstraints/custom)。エラー メッセージで公開される可能性があるため、制約 ID に個人情報(PII)や機密データを含めないでください。
  6. [説明] ボックスに、人が読める形式で制約の説明を入力します。この説明は、ポリシー違反の際にエラー メッセージとして使用されます。ポリシー違反が発生した理由と、ポリシー違反を解決する方法に関する詳細を含めてください。エラー メッセージで公開される可能性があるため、説明に個人情報(PII)や機密データを含めないでください。 このフィールドには、最大 2,000 文字まで入力できます。
  7. [リソースの種類] ボックスで、制限するオブジェクトとフィールドを含む Google Cloud REST リソースの名前を選択します(例: container.googleapis.com/NodePool)。ほとんどの種類のリソースは、最大 20 個のカスタム制約をサポートしています。これより多くのカスタム制約を作成しようとすると、オペレーションは失敗します。
  8. [適用方法] で、REST CREATE メソッドに制約を適用するか、CREATE メソッドと UPDATE メソッドの両方に制約を適用するかを選択します。制約に違反するリソースに対する UPDATE メソッドに制約を適用した場合、そのリソースへの変更は、違反を解決するものでない限り、組織のポリシーによってブロックされます。
  9. 各サービスでサポートされているメソッドを確認するには、 カスタム制約をサポートするサービスをご覧ください。

  10. 条件を定義するには、[ 条件を編集] をクリックします。
    1. [条件を追加] パネルで、サポートされているサービス リソースを参照する CEL 条件を作成します(例: resource.management.autoUpgrade == false)。このフィールドには、最大 1,000 文字まで入力できます。CEL の使用方法の詳細については、Common Expression Language をご覧ください。 カスタム制約で使用できるサービス リソースの詳細については、カスタム制約のサポート サービスをご覧ください。
    2. [保存] をクリックします。
  11. [アクション] で、条件が満たされた場合に評価対象のメソッドを許可するか拒否するかを選択します。
  12. 拒否アクションは、条件が true と評価された場合に、リソースを作成または更新するオペレーションがブロックされることを意味します。

    許可アクションは、条件が true と評価された場合にのみ、リソースを作成または更新するオペレーションが許可されることを意味します。条件に明記されているケースを除き、他のケースはすべてブロックされます。

  13. [制約を作成] をクリックします。
  14. 各フィールドに値を入力すると、このカスタム制約に対応する YAML 構成が右側に表示されます。

gcloud

  1. カスタム制約を作成するには、次の形式で YAML ファイルを作成します。
  2. 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.authorizationCheck)。このフィールドには、接頭辞(custom.)を除き、最大 70 文字まで入力できます(例: organizations/123456789/customConstraints/custom)。エラー メッセージで公開される可能性があるため、制約 ID に個人情報(PII)やセンシティブ データを含めないでください。
    • RESOURCE_NAME: 制限するオブジェクトとフィールドを含む Google Cloudリソースの完全修飾名。たとえば、servicemanagement.googleapis.com/Service のようにします。ほとんどの種類のリソースは、最大 20 個のカスタム制約をサポートしています。これより多くのカスタム制約を作成しようとすると、オペレーションは失敗します。
    • methodTypes: 制約が適用される REST メソッド。CREATE、または CREATEUPDATE の両方。制約に違反するリソースに対する UPDATE メソッドに制約を適用した場合、そのリソースへの変更は、違反を解決するものでない限り、組織のポリシーによってブロックされます。
    • 各サービスでサポートされているメソッドを確認するには、 カスタム制約をサポートするサービスをご覧ください。

    • CONDITION: サポート対象のサービス リソースの表現に対して記述する CEL 条件。このフィールドには、最大 1,000 文字まで入力できます。例: resource.name == \"test-service-name\"
    • 条件を記述できるリソースの詳細については、サポートされているリソースをご覧ください。

    • ACTION: condition が満たされた場合に実行するアクション。 有効な値は ALLOWDENY です。
    • 許可アクションは、条件が true と評価された場合に、リソースを作成または更新するオペレーションが許可されることを意味します。つまり、条件に明記されているケース以外はすべてブロックされます。

      拒否アクションは、条件が true と評価された場合に、リソースを作成または更新するオペレーションがブロックされることを意味します。

    • DISPLAY_NAME: 制約の名前。わかりやすい名前を入力してください。この名前はエラー メッセージで使用されるほか、識別やデバッグにも使用できます。エラー メッセージで公開される可能性があるため、表示名に個人情報(PII)やセンシティブ データを使用しないでください。このフィールドには、最大 200 文字まで入力できます。
    • DESCRIPTION: ポリシー違反の際にエラー メッセージとして表示される制約の説明。わかりやすい説明を入力してください。このフィールドには、最大 2,000 文字まで入力できます。
  3. 新しいカスタム制約の YAML ファイルを作成したら、組織内で組織のポリシーとして使用できるように設定する必要があります。カスタム制約を設定するには、gcloud org-policies set-custom-constraint コマンドを使用します。
  4. gcloud org-policies set-custom-constraint CONSTRAINT_PATH

    CONSTRAINT_PATH は、カスタム制約ファイルのフルパスに置き換えます。例: /home/user/customconstraint.yaml

    この操作が完了すると、カスタム制約が Google Cloud の組織ポリシーのリストに表示され、組織のポリシーとして使用できるようになります。

  5. カスタム制約が存在することを確認するには、gcloud org-policies list-custom-constraints コマンドを使用します。
  6. 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. ドライラン モードで組織のポリシーを適用するには、[ドライラン ポリシーを設定] をクリックします。詳細については、 組織のポリシーをテストするをご覧ください。
  11. ドライラン モードで組織のポリシーが想定どおりに動作することを確認したら、[ポリシーを設定] をクリックして有効なポリシーを設定します。

gcloud

  1. ブール値のルールを含む組織のポリシーを作成するには、制約を参照するポリシーの YAML ファイルを作成します。
  2. name: projects/PROJECT_ID/policies/CONSTRAINT_NAME
    spec:
      rules:
      - enforce: true
    
    dryRunSpec:
      rules:
      - enforce: true

    次のように置き換えます。

    • PROJECT_ID: 制約を適用するプロジェクト。
    • CONSTRAINT_NAME: カスタム制約に定義した名前。例: custom.authorizationCheck
  3. ドライラン モードで組織のポリシーを適用するには、dryRunSpec フラグを指定して次のコマンドを実行します。
  4. gcloud org-policies set-policy POLICY_PATH --update-mask=dryRunSpec

    POLICY_PATH は、組織のポリシーの YAML ファイルのフルパスに置き換えます。ポリシーが有効になるまでに最大 15 分かかります。

  5. ドライラン モードで組織のポリシーが想定どおりに動作することを確認したら、org-policies set-policy コマンドと spec フラグを使用して有効なポリシーを設定します。
  6. gcloud org-policies set-policy POLICY_PATH --update-mask=spec

    POLICY_PATH は、組織のポリシーの YAML ファイルのフルパスに置き換えます。ポリシーが有効になるまでに最大 15 分かかります。

カスタム組織のポリシーをテストする

次の例では、Service リソースの属性に基づいてカスタム制約を作成します。これらのルールを使用して、特定の CreateServiceConfig リクエストを許可または拒否できます。

始める前に、次のことを理解しておく必要があります。

  • 組織の ID
  • プロジェクト ID

制約を作成する

  1. 次のファイルに constraint-file-name.yaml という名前を付けて保存します。

        name: organizations/ORGANIZATION_ID/customConstraints/custom.authorizationCheck
        resourceTypes:
        -  servicemanagement.googleapis.com/Service
        methodTypes:
        -   CREATE
        -   UPDATE
        condition: resource.name == \"test-service-name\"
        actionType: ALLOW
        displayName: Check Configuration time policy enforcement
        description: Organization policies, defined with CEL and enforced via service manager APIs, allow admins to control service configurations through inheritable and granular rules.
    

    これにより、すべての Service Management のサービス名が test-service-name と等しくなければならないという制約が定義されます。

  2. 制約を適用します。

    gcloud org-policies set-custom-constraint ~/constraint-file-name.yaml
    
  3. 制約が存在することを確認します。

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

    出力は次のようになります。

    CUSTOM_CONSTRAINT ACTION_TYPE METHOD_TYPES RESOURCE_TYPES DISPLAY_NAME
    custom.configTimeEnforcement ALLOW CREATE,UPDATE servicemanagement.googleapis.com/Service Configuration time policy enforcement
    

ポリシーを作成する

  1. 次のファイルに policy-file-name.yaml という名前を付けて保存します。

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

    PROJECT_ID は、実際のプロジェクト ID に置き換えます。

  2. ポリシーを適用します。

    gcloud org-policies set-policy ~/policy-file-name.yaml
    
  3. ポリシーが存在することを確認します。

    gcloud org-policies list --project=PROJECT_ID
    

    出力は次のようになります。

    CONSTRAINT LIST_POLICY BOOLEAN_POLICY ETAG
    custom.configTimeEnforcement - SET COCsm5QGENiXi2E=
    

ポリシーを適用したら、 Google Cloud がポリシーの適用を開始するまで 2 分ほど待ちます。

ポリシーのテスト

次のコマンドを実行して、制約を確認するサービス構成を作成します。

  gcloud endpoints services deploy policy-file-name.yaml

出力は次のようになります。

  Operation denied by custom org policies: ["customConstraints/custom.authorizationCheck": "error Message"]

次のステップ

  • サービス管理オプションの詳細については、サービス管理の概要をご覧ください。
  • Service Management の使用時に発生する可能性のある一般的な問題を解決するには、トラブルシューティングをご覧ください。
  • Service Management の事前定義された組織のポリシーの制約を確認する。
  • 組織ポリシー サービスの詳細を学習する。
  • 組織のポリシーの作成と管理の方法について学習する。
  • マネージドの組織のポリシーの制約全一覧を参照する。