将 IaC 验证与 Jenkins 集成

您可以使用 Google Analyze Code Security Plugin for Jenkins 以验证 作为 Jenkins 项目一部分的基础架构即代码 (IaC)。 通过验证 IaC,您可以确定 Terraform 资源定义 违反现有的组织政策 应用于您的 Google Cloud 资源的 Security Health Analytics 检测器。

有关 IaC 验证的详细信息,请参阅根据 Google Cloud 组织的 政策

IaC 验证仅适用于 Jenkins 自由样式 项目

准备工作

完成以下任务后,便可开始使用 Jenkins 进行 IaC 验证。

激活 Security Command Center 高级层级或企业层级

验证 Security Command Center 专业版层级还是企业层级 已在 组织级别。

激活 Security Command Center 即会启用 securityposture.googleapis.comsecuritycentermanagement.googleapis.com API。

创建服务账号

创建可用于 Google Analyze Code Security 的服务账号 适用于 Jenkins 的插件。

  1. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. 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.

    6. Click Continue.
    7. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  2. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.

如需详细了解 IaC 验证权限,请参阅 用于组织级别的 IAM 激活

定义政策

定义组织政策Security Health Analytics 检测器。定义 这些政策,请完成创建和 部署 安全状况

安装并配置插件

  1. 在 Jenkins 控制台中,点击 Manage Jenkins(管理 Jenkins)>管理插件
  2. Available 标签页中,搜索 google-analyze-code-security
  3. 完成安装步骤。
  4. 点击 Manage Jenkins(管理 Jenkins)>配置系统
  5. Google Analyze Code Security(Google Analyze 代码安全性)部分,点击 Add credential(添加凭据)。
  6. 组织 ID 中,输入 Google Cloud 的组织 ID 您要创建的 Terraform 资源所在的组织, 修改。
  7. Security Command Center 凭据中,添加服务账号密钥。
  8. 测试连接以验证服务账号凭据。
  9. 点击保存

创建 Terraform 方案 JSON 文件

  1. 创建 Terraform 代码。有关说明,请参阅创建 Terraform 代码

  2. 安装适用于 Jenkins 的 Terraform 插件

  3. 在 Jenkins 控制台中,在 Jenkins 自由式项目中,转到 配置页面。

  4. 点击源代码管理

  5. Repository 网址 中,输入您创建的 Terraform 代码的网址。

  6. 点击构建步骤

  7. 添加以下步骤:

    1. 初始化 Terraform:

      terraform init
      
    2. 创建一个 Terraform 计划文件。

      terraform plan -out=TF_PLAN_FILE
      

      TF_PLAN_FILE 替换为 Terraform 计划的名称 文件。例如 myplan.tfplan

    3. 将计划文件转换为 JSON 格式:

      terraform show -no-color -json TF_PLAN_FILE > TF_PLAN_JSON_FILE
      

      TF_PLAN_JSON_FILE 替换为 Terraform 的名称 并采用 JSON 格式例如 mytfplan.json

将插件添加到您的 Jenkins 项目中

  1. 在 Jenkins 控制台中,在 Jenkins 自由式项目中,转到 配置页面。
  2. 构建步骤中,点击添加构建步骤 >执行代码扫描期间: 构建
  3. 输入您的组织 ID。
  4. 以 JSON 格式提供 Terraform 计划文件的路径。
  5. 可选:设置构建失败标准。失败标准取决于 严重程度为“严重”“高”“中”和“低”的问题的数量 遇到验证扫描的情况。您可以指定每种严重级别的问题数量 允许的内容和问题的汇总方式(OR)。

    1. 点击因资源违规而失败

    2. 如果您希望只在所有问题数量都达到 ,请选择。如果您希望构建失败 如果达到了任意严重级别的问题数量,请选择。 例如,如果您希望构建在遇到关键错误时失败 某个问题严重级别为“高”的问题,请将汇总值设置为 OR

    3. 指明所需的各种严重级别的问题数量 在构建失败之前允许的所有项目

    如果您不想指定失败标准,请选择忽略资产 违规

  6. 点击保存

现在,您可以运行构建来验证 Terraform 计划文件。

查看 IaC 违规报告

  1. 在 Jenkins 控制台中,点击您的构建的最新工作流。

  2. 点击状态。以下 HTML 文件可用作构建制品:

    • 如果插件正在运行,违规报告(GoogleAnalyzeCodeSecurity_ViolationSummary.html

    该报告会按严重程度对违规问题进行分组。“违规行为”部分介绍了 不符合哪条规则,以及违反的 Terraform 方案中的资产 ID 规则。

    • 如果构建失败,系统会显示错误摘要报告
  3. 请先解决 Terraform 代码中的所有违规问题,然后再应用代码。

后续步骤