将 IaC 验证与 GitHub Actions 集成

您可以使用 Analyze Code Security 操作来验证作为 GitHub Actions 工作流一部分的基础设施即代码 (IaC)。通过验证 IaC,您可以确定 Terraform 资源定义是否违反了应用于 Google Cloud 资源的现有组织政策和 Security Health Analytics 检测器。

如需详细了解 IaC 验证,请参阅根据 Google Cloud 组织的政策验证 IaC

准备工作

完成以下任务,以开始使用 GitHub Actions 进行 IaC 验证。

激活 Security Command Center 高级方案或 Enterprise 方案

验证 Security Command Center 高级方案或 Enterprise 方案是否已在组织级激活。

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

创建服务账号

创建一个可用于 Analyze Code Security 操作的服务账号。

  1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator). Learn how to grant roles.
  2. In the Google Cloud console, go to the Create service account page.

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

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

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

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

设置身份验证

  1. 配置使用 GitHub 身份提供方的工作负载身份联合。如需查看相关说明,请参阅工作负载身份联合

  2. 获取工作负载身份联合 ID 令牌的网址。例如 https://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/providers/PROVIDER_ID

    请考虑以下事项:

    • PROJECT_NUMBER 是您在其中设置工作负载身份联合的Google Cloud 项目的编号。
    • POOL_ID 是池名称。
    • PROVIDER_ID 是您的身份提供方的名称。
  3. 向 Google Cloud 进行身份验证操作添加到工作流,以对 IaC 验证操作进行身份验证。

定义政策

定义组织政策Security Health Analytics 检测器。如需使用安全状况定义这些政策,请完成创建和部署状况中的任务。

创建 Terraform plan JSON 文件

  1. 创建 Terraform 代码。如需查看相关说明,请参阅创建 Terraform 代码

  2. 在 GitHub Actions 中,初始化 Terraform。例如,如果您使用的是 HashiCorp - 设置 Terraform 操作,请运行以下命令:

    - name: Terraform Init
      id: init
      run: terraform init
    
  3. 创建 Terraform plan 文件:

    - name: Create Terraform Plan
      id: plan
      run: terraform plan -out=TF_PLAN_FILE
    

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

  4. 将 plan 文件转换为 JSON 格式:

    - name: Convert Terraform Plan to JSON
      id: convert
      run: terraform show -no-color -json TF_PLAN_FILE > TF_PLAN_JSON_FILE
    

    TF_PLAN_JSON_FILE 替换为采用 JSON 格式的 Terraform plan 文件的名称。例如 mytfplan.json

将操作添加到 GitHub Actions 工作流

  1. 在 GitHub 代码库中,浏览到您的工作流。
  2. 打开工作流编辑器。
  3. 在 GitHub Marketplace 边栏中,搜索 Analyze Code Security
  4. 安装部分,复制相应语法。
  5. 将该语法作为新步骤粘贴到工作流中。
  6. 替换以下值:

    • workload_identity_provider 替换为指向工作负载身份联合 ID 令牌的网址的链接。
    • service_account 替换为您为操作创建的服务账号的邮箱。
    • organization_id 替换为您的 Google Cloud 组织 ID。
    • scan_file_ref 替换为采用 JSON 格式的 Terraform plan 文件的路径。
    • failure_criteria 替换为用于确定操作何时失败的失败阈值条件。阈值条件基于 IaC 验证扫描遇到的严重、高、中和低严重性级别问题的数量。failure_criteria 指定允许每种严重级别的问题数量以及问题的汇总方式(ANDOR)。例如,如果您希望操作在遇到一个重大问题或一个高严重级别问题时失败,请将 failure_criteria 设置为 Critical:1,High:1,Operator:OR。默认值为 Critical:1,High:1,Medium:1,Low:1,Operator:OR,这意味着,如果 IaC 验证扫描遇到任何问题,相应操作一定会失败。

现在,您可以运行工作流来验证 Terraform plan 文件。如需手动运行工作流,请参阅手动运行工作流

查看 IaC 违规报告

  1. 在 GitHub 代码库中,点击操作,然后选择您的工作流。

  2. 点击工作流的最近一次运行作业。

    制品部分中,违规报告 (ias-scan-sarif.json) 以 zip 文件的形式提供。该报告包含以下字段:

    • 一个 rules 字段,用于说明 Terraform plan 违反了哪些政策。每条规则都包含一个 ruleID,您可以将其与报告中包含的结果进行匹配。
    • 一个 results 字段,用于说明违反特定规则的提议资产修改。
  3. 在应用 Terraform 代码之前,请先解决其中的所有违规行为。

后续步骤