始める前に
Google Cloud CLI をインストールする
gcloud beta terraform vet を使用するには、まず Google Cloud CLI をインストールする必要があります。
Google Cloud CLI をインストールしますが、
gcloud initコマンドはスキップします。次のコマンドを実行して、terraform-tools コンポーネントをインストールします。
gcloud components update gcloud components install terraform-tools次のコマンドを実行して、gcloud CLI がインストールされていることを確認します。
gcloud beta terraform vet --help
必要な権限の取得
検証に使用する Google Cloud アカウントには、次の権限が必要です。
getIamPolicy:gcloud beta terraform vetは、完全な Identity and Access Management(IAM)ポリシーを取得し、メンバー、バインディングと結合して、検証する最終的な状態を取得する必要があります。resourcemanager.projects.get:gcloud beta terraform vetは、検証対象のリソースに関連するプロジェクトに完全な CAI アセット名を正確に作成するため、API からプロジェクトの祖先を取得する必要があります。resourcemanager.folders.get: 検証対象のリソースにフォルダ関連のリソースが含まれている場合、gcloud beta terraform vetは、完全な CAI アセット名を正確に作成するために、API からフォルダの祖先を取得する必要があります。
ポリシー ライブラリを設定する
このツールを使用するには、ポリシー ライブラリを作成する必要があります。
ポリシーを検証する
1. Terraform プランを生成する
gcloud beta terraform vet は Terraform 0.12 以降に対応しています。gcloud beta terraform vet は、入力として terraform plan JSON を使用します。Terraform ディレクトリで次のコマンドを実行すると、JSON ファイルを生成できます。
terraform plan -out=tfplan.tfplan terraform show -json ./tfplan.tfplan > ./tfplan.json
2. 実行 gcloud beta terraform vet
gcloud beta terraform vet を使用すると、組織の POLICY_LIBRARY_REPO に対して terraform plan JSON を検証できます。例:
git clone POLICY_LIBRARY_REPO POLICY_LIBRARY_DIR gcloud beta terraform vet tfplan.json --policy-library=POLICY_LIBRARY_DIR
このコマンドを実行すると、gcloud beta terraform vet は、プランの正確な検証に必要な Google Cloud API を使用して、プロジェクト データを取得します。
フラグ
--policy-library=POLICY_LIBRARY_DIR- ポリシー ライブラリを含むディレクトリ。--project=PROJECT_ID-gcloud beta terraform vetは、オプションの--projectフラグを受け入れます。このフラグには、明示的なプロジェクトが設定されていないリソースの祖先を( Google Cloud リソース階層から)構築するときに使用するデフォルト プロジェクトを指定します。--format=FORMAT- デフォルトは yaml です。サポートされている形式は、default、json、none、text、yamlです。詳しくは、$ gcloud topic formats を実行して確認してください。
終了コードと出力
- すべての制約が検証されると、コマンドは終了コード 0 を返し、違反を表示しません。
- 違反が見つかった場合、
gcloud beta terraform vetは終了コード 2 を返し、違反のリストを表示します。たとえば、JSON 出力は次のようになります。
[
{
"constraint": "GCPIAMAllowedPolicyMemberDomainsConstraintV2.service_accounts_only",
"constraint_config": {
"api_version": "constraints.gatekeeper.sh/v1alpha1",
"kind": "GCPIAMAllowedPolicyMemberDomainsConstraintV2",
"metadata": {
"annotations": {
"description": "Checks that members that have been granted IAM roles belong to allowlisted domains.",
"validation.gcp.forsetisecurity.org/originalName": "service_accounts_only",
"validation.gcp.forsetisecurity.org/yamlpath": "policies/constraints/iam_service_accounts_only.yaml"
},
"name": "service-accounts-only"
},
"spec": {
"match": {
"target": [
"organizations/**"
]
},
"parameters": {
"domains": [
"gserviceaccount.com"
]
},
"severity": "high"
}
},
"message": "IAM policy for //cloudresourcemanager.googleapis.com/projects/PROJECT_ID contains member from unexpected domain: user:me@example.com",
"metadata": {
"ancestry_path": "organizations/ORG_ID/projects/PROJECT_ID",
"constraint": {
"annotations": {
"description": "Checks that members that have been granted IAM roles belong to allowlisted domains.",
"validation.gcp.forsetisecurity.org/originalName": "service_accounts_only",
"validation.gcp.forsetisecurity.org/yamlpath": "policies/constraints/iam_service_accounts_only.yaml"
},
"labels": {},
"parameters": {
"domains": [
"gserviceaccount.com"
]
}
},
"details": {
"member": "user:me@example.com",
"resource": "//cloudresourcemanager.googleapis.com/projects/PROJECT_ID"
}
},
"resource": "//cloudresourcemanager.googleapis.com/projects/PROJECT_ID",
"severity": "high"
}
]
CI / CD の例
CI / CD パイプラインで gcloud beta terraform vet を使用する bash スクリプトは次のようになります。
terraform plan -out=tfplan.tfplan terraform show -json ./tfplan.tfplan > ./tfplan.json git clone POLICY_LIBRARY_REPO POLICY_LIBRARY_DIR VIOLATIONS=$(gcloud beta terraform vet tfplan.json --policy-library=POLICY_LIBRARY_DIR --format=json) retVal=$? if [ $retVal -eq 2 ]; then # Optional: parse the VIOLATIONS variable as json and check the severity level echo "$VIOLATIONS" echo "Violations found; not proceeding with terraform apply" exit 1 fi if [ $retVal -ne 0]; then echo "Error during gcloud beta terraform vet; not proceeding with terraform apply" exit 1 fi echo "No policy violations detected; proceeding with terraform apply" terraform apply
デベロッパーは、CI / CD パイプラインを実行する前に、gcloud beta terraform vet をローカルで使用して、Terraform の変更をテストすることもできます。