本文档介绍了如何根据组织政策和您在 Google Cloud 组织中定义的 Security Health Analytics 检测器验证您的基础设施即代码 (IaC)。借助 IaC,您可以使用 Terraform 等语言创建和管理云资源,以便使用开发工作流部署资源。IaC 验证功能仅支持组织政策和 Security Health Analytics 检测器。
通过验证 IaC,您可以确定新资源定义或修改的资源定义是否违反了应用于Google Cloud 资源(例如集群、存储桶或实例)的现有政策。您可以使用安全状况定义这些政策,但 IaC 验证功能会根据您的Google Cloud 组织中的所有政策分析您的代码。IaC 验证可帮助开发者在资产或资源的 IaC 配置应用到Google Cloud 环境之前,识别和修复其中的任何安全问题。
IaC 验证功能支持 Terraform 方案文件。您可以使用 Google Cloud CLI 验证 Terraform 方案,也可以将验证流程集成到 Cloud Build、Jenkins 或 GitHub Actions 开发者工作流中。
准备工作
完成以下任务,开始使用 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
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。
设置 Google Cloud CLI
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
定义政策
定义组织政策和 Security Health Analytics 检测器。 如需使用安全状况定义这些政策,请完成创建和部署状况中的任务。
创建 Terraform 代码
使用开发者工作流和工具创建 Terraform 文件,其中包含您要创建或修改的 Google Cloud 资产。
请考虑以下事项:
- 在 Terraform 配置中填充每个资源或资产的父级属性(项目、文件夹或组织)。
- 分别修改资产和政策。该 API 不支持验证同时修改资产和政策的 Terraform 方案文件。
- 仅使用支持的资产类型和政策。如需查看支持的资产类型和政策的列表,请参阅适用于 IaC 验证的支持的资产类型和政策。
- 查看 IaC 验证的限制。
- 请勿在 Terraform 方案文件中包含密码或其他个人身份信息等敏感信息。如果验证功能在资源更改中遇到标记为敏感的字段,则会移除这些字段。
创建 Terraform 代码后,您可以运行 IaC 验证报告。您可以使用 gcloud CLI、Cloud Build、Jenkins 或 GitHub Actions。
使用 Google Cloud CLI 创建 IaC 验证报告
如需创建 IaC 验证报告,请完成以下操作:
在 gcloud CLI 中,运行
terraform init
。验证您运行的是 Terraform 提供程序 v5.5 版或更高版本。如果需要,请升级到最新的 Google 提供程序版本:
terraform init -upgrade
将 Terraform 方案文件转换为 JSON 格式:
terraform plan -out TF_PLAN_FILENAME terraform show -json TF_PLAN_FILENAME > TF_PLAN_JSON_FILENAME.json
替换以下内容:
TF_PLAN_FILENAME
:Terraform 方案文件的名称。TF_PLAN_JSON_FILENAME
:将包含 JSON 格式的 Terraform 方案的文件的名称。
创建 IaC 验证报告:
gcloud scc iac-validation-reports create PARENT \ --tf-plan-file=TF_PLAN_JSON_FILENAME.json
替换以下内容:
PARENT
:要创建 IaC 验证报告的 Google Cloud 组织。格式为organizations/ORGANIZATION_ID/locations/LOCATION
。LOCATION
为global
。TF_PLAN_JSON_FILENAME
:包含要验证的 IaC 方案的 JSON 文件的路径。
例如,如需在
organizations/3589215982/locations/global
组织下创建 IaC 验证报告,且planFile.json
中包含 IaC 方案,请运行以下命令:gcloud scc iac-validation-reports create organizations/3589215982/locations/global --tf-plan-file=planFile.json
此命令会返回创建 IAC 验证报告的操作详细信息。如需获取有关该操作的信息,请参阅查看有关安全状况部署操作的信息。
后续步骤
- 查看示例教程。
- 将 IaC 验证与您的 Cloud Build 工作流集成。
- 将 IaC 验证与 GitHub Actions 或 Jenkins 项目集成。
- 管理安全状况。
-