IaC 検証を Cloud Build と統合する

ビルドの一部である Infrastructure as Code(IaC)を検証するように Cloud Build に指示する ビルド構成を作成できます。IaC を検証すると、Terraform リソース定義が Google Cloud リソースに適用されている既存の組織のポリシーと Security Health Analytics の検出機能に違反しているかどうかを判断できます。

IaC の検証の詳細については、Google Cloud 組織のポリシーに対して IaC を検証するをご覧ください。

始める前に

Cloud Build を使用して IaC の検証を開始するには、次のタスクを実行します。

Security Command Center のプレミアム ティアまたはエンタープライズ ティアを有効にする

Security Command Center のプレミアム ティアまたはエンタープライズ ティアが組織レベルで有効になっていることを確認します。

Security Command Center を有効にすると、securityposture.googleapis.com API と securitycentermanagement.googleapis.com API が有効になります。

権限を設定する

  1. 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

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the organization.
    3. 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.

    4. For all rows that specify or include you, check the Role colunn to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      [IAM] に移動
    2. 組織を選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。

    IaC 検証の権限の詳細については、組織レベルでの有効化のための IAM をご覧ください。

    Cloud Build API を有効にする

    1. Enable the Cloud Build API.

      Enable the API

    ポリシーを定義する

    組織のポリシーSecurity Health Analytics の検出機能を定義します。セキュリティ ポスチャーを使用してこれらのポリシーを定義するには、ポスチャーを作成してデプロイするのタスクを実行します。

    Terraform のコードを作成する

    手順については、Terraform のコードを作成するをご覧ください。

    Cloud Build で IaC を検証する

    次のタスクを cloudbuild.yaml ファイルに追加します。

    1. 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 は、Terraform の状態を保存する Cloud Storage バケットの名前で置き換えます。
      • REPOSITORY_NAME は、Terraform コードをホストするリポジトリに置き換えます。
      • FOLDER は、Terraform アーティファクトを保存するフォルダの名前に置き換えます。
    2. プランファイルを作成します。

      - name: hashicorp/terraform
        args:
          - '-c'
          - |
            terraform plan -out tf.plan
        dir: FOLDER
        id: Terraform Plan
        entrypoint: sh
      
    3. Terraform のプランファイルを JSON 形式に変換します。

      - name: hashicorp/terraform
        args:
          - '-c'
          - |
            terraform show -json tf.plan > plan.json
        dir: FOLDER
        id: Terraform Show
        entrypoint: sh
      
    4. 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 に置き換えます。

    5. 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 バケットに置き換えます。

    6. 結果を SARIF 形式で表示するには、次の手順を行います。

      1. ファイルを変換します。

        - 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
        
      2. 省略可: 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
        
    7. 結果を確認します。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)を指定します。たとえば、重大度が重大の 1 つの問題、または重大度が高の 1 つの問題が発生した場合にビルドを失敗させるには、FAILURE_CRITERIACritical:1,High:1,Operator:OR に設定します。デフォルトは Critical:1,High:1,Medium:1,Low:1,Operator:OR です。つまり、IaC 検証スキャンで重大度を問わず、違反が発生した場合、ビルドは失敗する必要があります。

    8. ビルドが失敗した場合は、Terraform コード内の違反を解決します。

    次のステップ