カスタム制約を使用する
Google Cloud の組織のポリシーを使用すると、組織のリソースをプログラムで一元管理できます。組織ポリシー管理者は組織ポリシーを定義できます。組織ポリシーは、Google Cloud のリソース階層内の Google Cloud リソースやそれらのリソースの子孫に適用される、制約と呼ばれる一連の制限です。組織のポリシーは、組織レベル、フォルダレベル、またはプロジェクト レベルで適用できます。
組織のポリシーは、さまざまな Google Cloud サービスに事前定義された制約を提供します。ただし、組織のポリシーで制限されている特定のフィールドをきめ細かくカスタマイズ可能な方法で制御する必要がある場合は、カスタム制約を作成して、組織のポリシーでそれらのカスタム制約を使用することもできます。
利点
カスタムの組織のポリシーを使用すると、Dataproc サーバーレス バッチの特定のオペレーションを許可または拒否できます。たとえば、バッチ ワークロードの作成リクエストが、組織のポリシーで設定されたカスタム制約検証を満たしていない場合、リクエストは失敗し、エラーが呼び出し元に返されます。
ポリシーの継承
デフォルトでは、組織のポリシーは、そのポリシーを適用したリソースの子孫に継承されます。たとえば、フォルダにポリシーを適用した場合、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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Dataproc Serverless API.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Dataproc Serverless API.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 組織 ID を確実に把握します。
必要なロール
組織のポリシーを管理するために必要な権限を取得するには、組織のリソースに対する組織ポリシー管理者(roles/orgpolicy.policyAdmin
)の IAM ロールを付与するよう管理者に依頼してください。ロールの付与については、プロジェクト、フォルダ、組織へのアクセスを管理するをご覧ください。
この事前定義ロールには、組織のポリシーを管理するために必要な権限が含まれています。必要とされる正確な権限については、「必要な権限」セクションを開いてご確認ください。
必要な権限
組織のポリシーを管理するには、次の権限が必要です。
-
orgpolicy.constraints.list
-
orgpolicy.policies.create
-
orgpolicy.policies.delete
-
orgpolicy.policies.list
-
orgpolicy.policies.update
-
orgpolicy.policy.get
-
orgpolicy.policy.set
カスタムロールや他の事前定義ロールを使用して、これらの権限を取得することもできます。
カスタム制約を作成する
カスタム制約は、適用されるリソース、メソッド、条件、アクションによって YAML ファイルで定義されます。Dataproc Serverless は、BATCH
リソースの CREATE
メソッドに適用されるカスタム制約をサポートしています(リソースとオペレーションに対する Dataproc Serverless の制約をご覧ください)。
Dataproc Serverless カスタム制約の YAML ファイルを作成するには:
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes:
- dataproc.googleapis.com/Batch
methodTypes:
- CREATE
condition: CONDITION
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION
以下を置き換えます。
ORGANIZATION_ID
: 組織 ID(123456789
など)。CONSTRAINT_NAME
: 新しいカスタム制約に付ける名前。カスタム制約はcustom.
で始まる必要があります。含められるのは大文字、小文字、数字のみです(例:custom.batchMustHaveSpecifiedCategoryLabel
)。このフィールドの最大長は 70 文字です。接頭辞(例:organizations/123456789/customConstraints/custom
)はカウントされません。CONDITION
: サポート対象のサービス リソースの表現に対して書き込まれる CEL 条件。このフィールドの最大長は 1000 文字です。条件の書き込み先として使用できるリソースの詳細については、サポート対象のリソースをご覧ください。条件の例:("category" in resource.labels) && (resource.labels['category'] in ['retail', 'ads', 'service'])
ACTION
: 条件が満たされている場合に実行するアクション。ALLOW
またはDENY
になります。DISPLAY_NAME
: 制約の名前。わかりやすい名前を入力してください。表示名の例: 「バッチの「category」ラベルの要件を適用する」。このフィールドの最大長は 200 文字です。DESCRIPTION
: ポリシー違反時にエラー メッセージとして表示される制約の説明。わかりやすい説明を入力してください。このフィールドの最大長は 2,000 文字です。説明の例: 「「retail」、「ads」、「service」の値を持つ「category」ラベルがある場合にのみ、Dataproc のバッチ作成を許可します」。
カスタム制約の作成方法については、カスタム制約の定義をご覧ください。
カスタム制約を設定する
Google Cloud CLI を使用して新しいカスタム制約を作成したら、組織内の組織のポリシーで使用できるように設定する必要があります。カスタム制約を設定するには、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 リソースに適用します。Console
ブール型制約を適用するには、次のようにします。
- Google Cloud コンソールで、[組織のポリシー] ページに移動します。
- ページの上部にあるプロジェクト選択ツールを選択します。
- プロジェクト選択ツールから、組織のポリシーを設定するプロジェクトを選択します。
- [組織のポリシー] ページのリストから制約を選択します。その制約の [ポリシーの詳細] ページが表示されます。
- このリソースの組織のポリシーを構成するには、[ポリシーを管理] をクリックします。
- [ポリシーの編集] ページで、[親のポリシーをオーバーライドする] を選択します。
- [ルールの追加] をクリックします。
- [適用] で、この組織のポリシーの適用を有効にするかどうかを選択します。
- 必要に応じて、タグで条件付きの組織のポリシーにするには、[条件を追加] をクリックします。組織のポリシーに条件付きルールを追加する場合は、少なくとも 1 つは無条件のルールを追加する必要があります。そうしないとポリシーを保存できないのでご注意ください。詳細については、タグを使用した組織のポリシーの設定をご覧ください。
- カスタム制約の場合は、[変更内容をテスト] をクリックして、組織のポリシーの効果をシミュレートできます。詳細については、Policy Simulator で組織のポリシーの変更をテストするをご覧ください。
- 組織のポリシーを完成させて適用するには、[ポリシーを設定] をクリックします。ポリシーが有効になるまでに最長 15 分かかります。
gcloud
ブール型制約を適用する組織のポリシーを作成するには、制約を参照するポリシー YAML ファイルを作成します。
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME spec: rules: - enforce: true
次のように置き換えます。
-
PROJECT_ID
: 制約を適用するプロジェクト。 -
CONSTRAINT_NAME
: カスタム制約に定義した名前。例:
。custom.batchMustHaveSpecifiedCategoryLabel
制約を含む組織のポリシーを適用するには、次のコマンドを実行します。
gcloud org-policies set-policy POLICY_PATH
POLICY_PATH
は、組織のポリシーの YAML ファイルのパスに置き換えます。ポリシーが有効になるまでに最大 15 分かかります。
カスタム制約をテストする
次のバッチ作成例では、カスタム制約が作成され、バッチ作成時に適用され、バッチに「retail」、「ads」、「service」の値が設定された「category」ラベルが付加されている必要があります: ("category" in resource.labels) && (resource.labels['category'] in ['retail', 'ads', 'service'])
。この例の「category」ラベルには、必要な値が設定されていません。
gcloud dataproc batches submit spark \
--region us-west1
--jars file:///usr/lib/spark/examples/jars/spark-examples.jar \
--class org.apache.spark.examples.SparkPi \
--network default \
--labels category=foo \
-- 100
出力例:
Operation denied by custom org policies: ["customConstraints/custom.batchMustHaveSpecifiedCategoryLabel
": ""Only allow Dataproc batch creation if it has a 'category' label with
a 'retail', 'ads', or 'service' value""]
リソースとオペレーションに対する Dataproc サーバーレスの制約
次の Dataproc Serverless カスタム制約は、バッチ ワークロードを作成(送信)する際に使用できます。
全般
resource.labels
RuntimeConfig
resource.runtimeConfig.version
resource.runtimeConfig.containerImage
resource.runtimeConfig.properties
ExecutionConfig
resource.environmentConfig.executionConfig.serviceAccount
resource.environmentConfig.executionConfig.networkTags
resource.environmentConfig.executionConfig.kmsKey
resource.environmentConfig.executionConfig.stagingBucket
PeripheralsConfig
resource.environmentConfig.peripheralsConfig.metastoreService
resource.environmentConfig.peripheralsConfig.sparkHistoryServerConfig.dataprocCluster
一般的なユースケースのカスタム制約の例
次の表に、Dataproc サーバーレス バッチのカスタム制約の例を示します。
説明 | 制約の構文 |
---|---|
Batch では、許可された値を含む「カテゴリ」ラベルを添付する必要があります。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.batchMustHaveSpecifiedCategoryLabel resourceTypes: - dataproc.googleapis.com/Batch methodTypes: - CREATE condition: ("category" in resource.labels) && (resource.labels['category'] in ['retail', 'ads', 'service']) actionType: ALLOW displayName: Enforce batch "category" label requirement. description: Only allow batch creation if it attaches a "category" label with an allowable value. |
Batch では、許可されるランタイム バージョンを設定する必要があります。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.batchMustUseAllowedVersion resourceTypes: - dataproc.googleapis.com/Batch methodTypes: - CREATE condition: (has(resource.runtimeConfig.version)) && (resource.runtimeConfig.version in ["2.0.45", "2.0.48"]) actionType: ALLOW displayName: Enforce batch runtime version. description: Only allow batch creation if it sets an allowable runtime version. |
Batch では、20 個を超える Spark 初期エグゼキュータを設定できません。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.batchInitialExecutorMax20 resourceTypes: - dataproc.googleapis.com/Batch methodTypes: - CREATE condition: (has(resource.runtimeConfig.properties)) && ('spark.executor.instances' in resource.runtimeConfig.properties) && (int(resource.runtimeConfig.properties['spark.executor.instances'])>20) actionType: DENY displayName: Enforce maximum number of batch Spark executor instances. description: Deny batch creation if it specifies more than 20 Spark executor instances. |
Batch では、20 個を超える Spark 動的割り当て初期エグゼキュータを設定できません。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.batchDynamicAllocationInitialExecutorMax20 resourceTypes: - dataproc.googleapis.com/Batch methodTypes: - CREATE condition: (has(resource.runtimeConfig.properties)) && ('spark.dynamicAllocation.initialExecutors' in resource.runtimeConfig.properties) && (int(resource.runtimeConfig.properties['spark.dynamicAllocation.initialExecutors'])>20) actionType: DENY displayName: Enforce maximum number of batch dynamic allocation initial executors. description: Deny batch creation if it specifies more than 20 Spark dynamic allocation initial executors. |
Batch では、20 個を超える動的割り当てエグゼキュータを許可してはなりません。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.batchDynamicAllocationMaxExecutorMax20 resourceTypes: - dataproc.googleapis.com/Batch methodTypes: - CREATE condition: (resource.runtimeConfig.properties['spark.dynamicAllocation.enabled']=='false') || (('spark.dynamicAllocation.maxExecutors' in resource.runtimeConfig.properties) && (int(resource.runtimeConfig.properties['spark.dynamicAllocation.maxExecutors'])<=20)) actionType: ALLOW displayName: Enforce batch maximum number of dynamic allocation executors. description: Only allow batch creation if dynamic allocation is disabled or the maximum number of dynamic allocation executors is set to less than or equal to 20. |
Batch では、KMS 鍵を許可されたパターンに設定する必要があります。 |
name: organizations/ORGANIZATION_ID/custom.batchKmsPattern resourceTypes: - dataproc.googleapis.com/Batch methodTypes: - CREATE condition: matches(resource.environmentConfig.executionConfig.kmsKey, '^keypattern[a-z]$') actionType: ALLOW displayName: Enforce batch KMS Key pattern. description: Only allow batch creation if it sets the KMS key to an allowable pattern. |
Batch では、ステージング バケットの接頭辞を許可された値に設定する必要があります。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.batchStagingBucketPrefix
resourceTypes:
- dataproc.googleapis.com/Batch
methodTypes:
- CREATE
condition: resource.environmentConfig.executionConfig.stagingBucket.startsWith( |
次のステップ
- 組織のポリシーの詳細について、組織のポリシー サービスの概要を確認する。
- 組織のポリシーの作成と管理の方法について学習する。
- 事前定義された組織のポリシーの制約の完全なリストを確認する。