您可以使用 Google Analyze Code Security 插件(适用于 Jenkins)来验证作为 Jenkins 项目一部分的基础设施即代码 (IaC)。通过验证 IaC,您可以确定您的 Terraform 资源定义是否违反了应用于您 Google Cloud 资源的现有组织政策和 Security Health Analytics 检测器。
如需详细了解 IaC 验证,请参阅根据 Google Cloud 组织的政策验证 IaC。
IaC 验证仅适用于 Jenkins freestyle 项目。
准备工作
完成以下任务,开始使用 Jenkins 进行 IaC 验证。
激活 Security Command Center 高级方案或 Enterprise 方案
验证 Security Command Center 高级方案或 Enterprise 方案是否已在组织级激活。
激活 Security Command Center 会启用 securityposture.googleapis.com
和 securitycentermanagement.googleapis.com
API。
创建服务账号
创建一个服务账号,以便用于适用于 Jenkins 的 Google Analyze Code Security 插件。
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the Security Posture Shift-Left Validator role to the service account.
To grant the role, find the Select a role list, then select Security Posture Shift-Left Validator.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
如需详细了解 IaC 验证权限,请参阅适用于组织级激活的 IAM。
定义政策
定义组织政策和 Security Health Analytics 检测器。如需使用安全状况定义这些政策,请完成创建和部署状况中的任务。
安装和配置插件
- 在 Jenkins 控制台中,依次点击管理 Jenkins > 管理插件。
- 在 Available 标签页中,搜索 google-analyze-code-security。
- 完成安装步骤。
- 依次点击管理 Jenkins > 配置系统。
- 在 Google Analyze 代码安全性部分,点击添加凭据。
- 在组织 ID 中,输入 Google Cloud组织的组织 ID,该组织包含您要创建或修改的 Terraform 资源。
- 在 Security Command Center 凭据中,添加服务账号密钥。
- 测试连接以验证服务账号凭据。
- 点击保存。
创建 Terraform 方案 JSON 文件
创建 Terraform 代码。如需了解相关说明,请参阅创建 Terraform 代码。
在 Jenkins 控制台中,前往 Jenkins Freestyle 项目的配置页面。
点击源代码管理。
在代码库网址中,输入您创建的 Terraform 代码的网址。
点击构建步骤。
添加以下步骤:
初始化 Terraform:
terraform init
创建 Terraform 方案文件。
terraform plan -out=TF_PLAN_FILE
将
TF_PLAN_FILE
替换为 Terraform 方案文件的名称。例如myplan.tfplan
。将方案文件转换为 JSON 格式:
terraform show -no-color -json TF_PLAN_FILE > TF_PLAN_JSON_FILE
将
TF_PLAN_JSON_FILE
替换为 Terraform 方案文件的名称(采用 JSON 格式)。例如mytfplan.json
。
将插件添加到 Jenkins 项目中
- 在 Jenkins 控制台中,前往 Jenkins Freestyle 项目的配置页面。
- 在构建步骤中,依次点击添加构建步骤 > 在构建期间执行代码扫描。
- 输入您的组织 ID。
- 以 JSON 格式提供 Terraform 方案文件的路径。
可选:设置 build 失败条件。失败条件基于 IaC 验证扫描遇到的严重、高、中和低严重性级别问题的数量。您可以指定每种严重性允许的问题数量,以及问题的汇总方式(AND 或 OR)。
点击 Fail on Asset Violation。
如果您希望仅当所有严重程度级别的问题数量都达到指定值时,构建才失败,请选择 AND。如果您希望在达到任何严重性级别的问题数量时构建失败,请选择 OR。例如,如果您希望构建在遇到一个严重问题或一个高严重性问题时失败,请将汇总值设置为 OR。
指明您希望在构建失败之前允许的各种严重程度的问题数量。
如果您不想指定失败条件,请选择 Ignore Asset Violation。
点击保存。
现在,您可以运行 build 来验证 Terraform 规划文件。
查看 IaC 违规报告
在 Jenkins 控制台中,点击您构建的最新工作流。
点击状态。以下 HTML 文件可作为 build 制品使用:
- 如果插件已运行,则违规报告 (
GoogleAnalyzeCodeSecurity_ViolationSummary.html
)
此报告按违规严重程度对违规行为进行分组。“违规”部分描述了未满足的规则以及违反该规则的 Terraform 计划中的资产 ID。
- 如果 build 失败,则会生成错误摘要报告
- 如果插件已运行,则违规报告 (
在应用 Terraform 代码之前,请解决其中的所有违规问题。