カスタム組織ポリシーを使用する

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

  • monitoring.googleapis.com/AlertPolicy
  • monitoring.googleapis.com/NotificationChannel
  • monitoring.googleapis.com/Snooze

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

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

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.

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

    カスタムの組織のポリシーを管理するために必要な権限を取得するには、組織のリソースに対する組織のポリシー管理者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. で始まる必要があり、大文字、小文字、数字のみを含めることができます。例: alertDocHasProjectこのフィールドの最大長は 70 文字です。

  • RESOURCE_NAME: 制限するオブジェクトとフィールドを含むGoogle Cloud リソースの完全修飾名。例: monitoring.googleapis.com/AlertPolicy

  • CONDITION: サポート対象のサービス リソースの表現に対して書き込まれる CEL 条件。このフィールドの最大長は 1,000 文字です。条件の書き込み先として使用できるリソースの詳細については、サポート対象のリソースをご覧ください。例: resource.documentation.content.contains('${resource.project}')

  • 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: 適用する制約の名前。例: alertDocHasProject

制約を含む組織のポリシーを適用するには、次のコマンドを実行します。

    gcloud org-policies set-policy POLICY_PATH     

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

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

次の例では、アラート ポリシーのドキュメントにプロジェクト名が記載されているかどうかを確認するカスタムの制約とポリシーを作成します。

始める前に、次のように置き換えます。

  • ORGANIZATION_ID: 組織の一意の数値識別子。この ID を取得する方法については、組織 ID の取得をご覧ください。
  • PROJECT_ID: プロジェクトの ID。

制約を作成する

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

        actionType: DENY     condition: resource.documentation.content.contains('${resource.project}')     description: Requires documentation to include the project name.     displayName: Alerting policy documentation must include project name.     methodTypes:       - CREATE       - UPDATE     name: organizations/ORGANIZATION_ID/customConstraints/custom.alertDocHasProject     resourceTypes:       - monitoring.googleapis.com/AlertPolicy 

    ORGANIZATION_ID は、実際の組織 ID に置き換えます。

    この制約により、アラート ポリシーのドキュメントにリソース プロジェクトの名前がない場合、アラート ポリシーを作成または更新できなくなります。

  2. 制約を適用します。

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

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

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

    CUSTOM_CONSTRAINT: custom.alertDocHasProject ACTION_TYPE: DENY METHOD_TYPES: CREATE,UPDATE RESOURCE_TYPES: monitoring.googleapis.com/AlertPolicy DISPLAY_NAME: Alerting policy documentation must include the project name. 

ポリシーを作成する

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

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

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

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

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

    gcloud org-policies list --project=PROJECT_ID 

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

    CONSTRAINT: custom.alertDocHasProject LIST_POLICY: - BOOLEAN_POLICY: SET ETAG: CKSx5LwGEPjBuKwD- 

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

ポリシーのテスト

ドキュメントにリソース プロジェクト名がないアラート ポリシーを作成してみます。

  gcloud alpha monitoring policies create \     --display-name="CPU Utilization - 5% Threshold" \     --project=PROJECT_ID \     --condition-display-name="Condition 1" \     --condition-filter='resource.type="gce_instance" AND metric.type="compute.googleapis.com/instance/cpu/utilization"' \     --duration='60s' \     --if='>5' \     --combiner='AND' \     --documentation='The ${metric.display_name} of the ${resource.type} ${resource.label.instance_id} has exceeded 5% for over 60 seconds.' \ 

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

次のような出力が表示されます。

  Operation denied by custom org policies: ["customConstraints/alertDocHasProject": "Alerting policy documentation must include project name."] 

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

次の表では一般的なカスタム制約の構文例をまとめています。

説明 制約の構文
アラート ポリシーに含めることができる条件は 1 つだけです
    actionType: DENY     condition: "resource.conditions.size() > 1"     description: Requires alerting policies to have only one condition.     displayName: Alerting policies must have only one condition     methodTypes:       - CREATE       - UPDATE     name: organizations/ORGANIZATION_ID/customConstraints/custom.oneCondition     resourceTypes:       - monitoring.googleapis.com/AlertPolicy     
アラート ポリシーは少なくとも 2 つの通知チャンネルを使用する必要があります
    actionType: DENY     condition: "resource.notificationChannels.size() < 2"     description: Alerting policies must use at least two notification channels.     displayName: Policies must have a backup notification channel     methodTypes:       - CREATE       - UPDATE     name: organizations/ORGANIZATION_ID/customConstraints/custom.alertOnTwoChannels     resourceTypes:       - monitoring.googleapis.com/AlertPolicy     
メール通知チャンネルのみを許可します
    actionType: DENY     condition: "resource.type != 'email'"     description: Requires notification channels to use only the email channel     type.     displayName: Always use email for notification channels     methodTypes:       - CREATE       - UPDATE     name: organizations/ORGANIZATION_ID/customConstraints/custom.emailChannelsOnly     resourceTypes:       - monitoring.googleapis.com/NotificationChannel     
スヌーズは 1 時間未満にする必要があります
    actionType: DENY     condition: "int(resource.interval.endTime) > int(resource.interval.startTime) + 3600"     description: Requires snoozes to be under 1 hour.     displayName: Snooze must be less than 1 hour     methodTypes:     - CREATE     - UPDATE     name: organizations/ORGANIZATION_ID/customConstraints/custom.setMaxSnooze     resourceTypes:     - monitoring.googleapis.com/Snooze     

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

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

リソース フィールド
monitoring.googleapis.com/AlertPolicy resource.alertStrategy.autoClose
resource.alertStrategy.notificationChannelStrategy.notificationChannelNames
resource.alertStrategy.notificationChannelStrategy.renotifyInterval
resource.alertStrategy.notificationRateLimit.period
resource.combiner
resource.conditions.conditionAbsent.aggregations.alignmentPeriod
resource.conditions.conditionAbsent.aggregations.crossSeriesReducer
resource.conditions.conditionAbsent.aggregations.groupByFields
resource.conditions.conditionAbsent.aggregations.perSeriesAligner
resource.conditions.conditionAbsent.duration
resource.conditions.conditionAbsent.filter
resource.conditions.conditionAbsent.trigger.count
resource.conditions.conditionAbsent.trigger.percent
resource.conditions.conditionMatchedLog.filter
resource.conditions.conditionMatchedLog.labelExtractors
resource.conditions.conditionMonitoringQueryLanguage.duration
resource.conditions.conditionMonitoringQueryLanguage.evaluationMissingData
resource.conditions.conditionMonitoringQueryLanguage.query
resource.conditions.conditionMonitoringQueryLanguage.trigger.count
resource.conditions.conditionMonitoringQueryLanguage.trigger.percent
resource.conditions.conditionPrometheusQueryLanguage.alertRule
resource.conditions.conditionPrometheusQueryLanguage.duration
resource.conditions.conditionPrometheusQueryLanguage.evaluationInterval
resource.conditions.conditionPrometheusQueryLanguage.labels
resource.conditions.conditionPrometheusQueryLanguage.query
resource.conditions.conditionPrometheusQueryLanguage.ruleGroup
resource.conditions.conditionSql.booleanTest.column
resource.conditions.conditionSql.daily.periodicity
resource.conditions.conditionSql.hourly.minuteOffset
resource.conditions.conditionSql.hourly.periodicity
resource.conditions.conditionSql.minutes.periodicity
resource.conditions.conditionSql.query
resource.conditions.conditionSql.rowCountTest.comparison
resource.conditions.conditionSql.rowCountTest.threshold
resource.conditions.conditionThreshold.aggregations.alignmentPeriod
resource.conditions.conditionThreshold.aggregations.crossSeriesReducer
resource.conditions.conditionThreshold.aggregations.groupByFields
resource.conditions.conditionThreshold.aggregations.perSeriesAligner
resource.conditions.conditionThreshold.comparison
resource.conditions.conditionThreshold.denominatorAggregations.alignmentPeriod
resource.conditions.conditionThreshold.denominatorAggregations.crossSeriesReducer
resource.conditions.conditionThreshold.denominatorAggregations.groupByFields
resource.conditions.conditionThreshold.denominatorAggregations.perSeriesAligner
resource.conditions.conditionThreshold.denominatorFilter
resource.conditions.conditionThreshold.duration
resource.conditions.conditionThreshold.evaluationMissingData
resource.conditions.conditionThreshold.filter
resource.conditions.conditionThreshold.forecastOptions.forecastHorizon
resource.conditions.conditionThreshold.thresholdValue
resource.conditions.conditionThreshold.trigger.count
resource.conditions.conditionThreshold.trigger.percent
resource.conditions.displayName
resource.conditions.name
resource.displayName
resource.documentation.content
resource.documentation.links.displayName
resource.documentation.links.url
resource.documentation.mimeType
resource.documentation.subject
resource.enabled
resource.name
resource.notificationChannels
resource.severity
monitoring.googleapis.com/NotificationChannel resource.description
resource.displayName
resource.enabled
resource.type
monitoring.googleapis.com/Snooze resource.criteria.policies
resource.displayName
resource.interval.endTime
resource.interval.startTime

次のステップ