您可以撰寫建構設定,指示 Cloud Build 驗證建構作業中的基礎架構即程式碼 (IaC)。驗證 IaC 可讓您判斷 Terraform 資源定義是否違反套用至 Google Cloud 資源的現有機構政策和安全狀態分析偵測器。
如要進一步瞭解 IaC 驗證,請參閱「根據貴機構的政策驗證 IaC Google Cloud 」。
事前準備
完成這些工作,即可開始使用 Cloud Build 進行 IaC 驗證。
啟用 Security Command Center Premium 級別或 Enterprise 級別
確認已在機構層級啟用 Security Command Center Premium 方案或 Enterprise 方案。
啟用 Security Command Center 會啟用 securityposture.googleapis.com
和 securitycentermanagement.googleapis.com
API。
設定權限
-
Make sure that you have the following role or roles on the organization:
- Security Posture Shift-Left Validator
- Log Writer
- Storage Writer
- Storage Reader
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the organization.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
前往「IAM」頁面 - 選取機構。
- 按一下「授予存取權」 。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。
- 在「Select a role」(選取角色) 清單中,選取角色。
- 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
- 按一下 [Save]。
如要進一步瞭解 IaC 驗證權限,請參閱機構層級啟用適用的 IAM。
啟用 Cloud Build API
-
Enable the Cloud Build API.
定義政策
定義機構政策和安全狀態分析偵測器。如要使用安全防護機制定義這些政策,請完成「建立及部署防護機制」中的工作。
建立 Terraform 程式碼
如需操作說明,請參閱「建立 Terraform 程式碼」。
在 Cloud Build 中驗證 IAC
在
cloudbuild.yaml
檔案中新增下列工作:初始化 Terraform:
- name: hashicorp/terraform args: - '-c' - | terraform init \ -backend-config="bucket=STATE_BUCKET" \ -backend-config="prefix=REPOSITORY_NAME" \ dir: FOLDER id: Terraform Init entrypoint: sh
更改下列內容:
STATE_BUCKET
,並將其替換為要儲存 Terraform 狀態的 Cloud Storage bucket 名稱REPOSITORY_NAME
,其中包含您的 Terraform 程式碼。- 將
FOLDER
替換為要儲存 Terraform 構件的資料夾名稱。
建立計畫檔案:
- name: hashicorp/terraform args: - '-c' - | terraform plan -out tf.plan dir: FOLDER id: Terraform Plan entrypoint: sh
將方案檔案轉換為 JSON 格式:
- name: hashicorp/terraform args: - '-c' - | terraform show -json tf.plan > plan.json dir: FOLDER id: Terraform Show entrypoint: sh
建立 IaC 驗證報告:
- name: gcr.io/cloud-builders/gcloud args: - '-c' - | gcloud scc iac-validation-reports create \ organizations/ORGANIZATION_ID/locations/global --tf-plan-file=plan.json \ --format="json(response.iacValidationReport)" > IaCScanReport_$BUILD_ID.json dir: FOLDER id: Run IaC scan entrypoint: /bin/bash
將
ORGANIZATION_ID
替換為貴機構的 ID。如果您使用 Cloud Storage,請將 JSON 結果檔案上傳至 Cloud Storage:
- name: gcr.io/cloud-builders/gsutil args: - cp - IaCScanReport_$BUILD_ID.json - SCAN_RESULT_FILE_BUCKET dir: FOLDER id: Upload report file
將
SCAN_RESULT_FILE_BUCKET
換成要上傳結果檔案的 Cloud Storage 值區。如要以 SARIF 格式查看結果,請完成下列步驟:
轉換檔案:
- name: golang args: - '-c' - | go run github.com/google/gcp-scc-iac-validation-utils/SARIFConverter@latest \ --inputFilePath=IaCScanReport_$BUILD_ID.json --outputFilePath=IaCScanReport_$BUILD_ID.sarif.json dir: FOLDER id: Convert to SARIF format entrypoint: /bin/bash
選用:將檔案上傳至 Cloud Storage:
- name: gcr.io/cloud-builders/gsutil args: - cp - IaCScanReport_$BUILD_ID.sarif.json - SCAN_RESULT_FILE_BUCKET dir: FOLDER id: Upload report file
驗證結果。針對尚未轉換為 SARIF 格式的結果 JSON 檔案,完成下列步驟:
- name: golang args: - '-c' - | go run github.com/google/gcp-scc-iac-validation-utils/ReportValidator@latest \ --inputFilePath=IaCScanReport_$BUILD_ID.json --failure_expression=FAILURE_CRITERIA dir: FOLDER id: Validate results entrypoint: /bin/bash
將
FAILURE_CRITERIA
替換為失敗門檻條件,決定建構何時會失敗。系統會根據 IaC 驗證掃描遇到的嚴重、高、中和低嚴重性問題數量,設定閾值條件。FAILURE_CRITERIA
指定允許的各嚴重程度問題數量,以及問題的匯總方式 (AND
或OR
)。舉例來說,如果希望建構作業在遇到一個嚴重問題或一個高嚴重程度問題時失敗,請將FAILURE_CRITERIA
設為Critical:1,High:1,Operator:OR
。預設值為Critical:1,High:1,Medium:1,Low:1,Operator:OR
,也就是說,如果 IaC 驗證掃描發現任何嚴重程度的違規事項,建構作業就必須失敗。如果建構失敗,請解決 Terraform 程式碼中的違規事項。
後續步驟