事前準備
安裝「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
需要從 API 取得專案祖先,才能為驗證資源相關的任何專案,準確建構完整的 CAI 資產名稱。resourcemanager.folders.get
:如果經過驗證的資源包含任何與資料夾相關的資源,gcloud beta terraform vet
就需要從 API 取得資料夾祖先,才能準確建構完整的 CAI 資產名稱。
設定政策資料庫
如要使用這項工具,請建立政策庫。
驗證政策
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
開發人員也可以在本機使用 gcloud beta terraform vet
測試 Terraform 變更,再執行 CI/CD 管道。