您可以编写构建配置,以指示 Cloud Build 验证作为构建一部分的基础架构即代码 (IaC)。通过验证 IaC,您可以确定您的 Terraform 资源定义是否违反了应用于您 Google Cloud 资源的现有组织政策和 Security Health Analytics 检测器。
如需详细了解 IaC 验证,请参阅根据 Google Cloud 组织的政策验证 IaC。
准备工作
完成以下任务,以便开始使用 Cloud Build 进行 IaC 验证。
激活 Security Command Center 高级方案或 Enterprise 方案
验证 Security Command Center 高级方案或 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 - 选择组织。
- 点击 授予访问权限。
-
在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
如需详细了解 IaC 验证权限,请参阅适用于组织级激活的 IAM。
启用 Cloud Build API
-
Enable the Cloud Build API.
定义政策
定义组织政策和 Security Health Analytics 检测器。如需使用安全状况定义这些政策,请完成创建和部署状况中的任务。
创建 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
替换为 Cloud Storage 存储桶的名称,以便在其中存储 Terraform 状态 - 将
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 代码中的所有违规问题。
后续步骤
- 查看 Cloud Storage 中的 IaC 验证报告。
- 查看 GitHub 中的 IaC 验证脚本。
- 查看
cloud.yaml
示例。