IaC 検証レポートのサンプルを作成する


このチュートリアルでは、Infrastructure as Code(IaC)が組織のポリシーや Security Health Analytics の検出機能に違反していないことを確認する方法について説明します。

目標

  • セキュリティ ポスチャーを作成する。
  • プロジェクトにポスチャーをデプロイする。
  • サンプルの Terraform ファイルに違反がないか確認する。
  • Terraform ファイルの違反を修正し、ファイルを再度確認して修正を検証する。

始める前に

権限を設定する

  1. Make sure that you have the following role or roles on the organization: Project Creator and Security Posture Admin

    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 column 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. [保存] をクリックします。

    Cloud Shell を設定する

    1. In the Google Cloud console, activate Cloud Shell.

      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.

    2. 自分の組織 ID を確認します。
      gcloud organizations list
    3. 環境を準備する

      1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
      2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

        Go to project selector

      3. Verify that billing is enabled for your Google Cloud project.

      4. Enable the Security posture service and Security Command Center management APIs.

        Enable the APIs

      5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

        Go to project selector

      6. Verify that billing is enabled for your Google Cloud project.

      7. Enable the Security posture service and Security Command Center management APIs.

        Enable the APIs

      8. プロジェクト番号をコピーします。ポスチャーのデプロイ中にターゲット リソースを設定するには、プロジェクト番号が必要です。
        gcloud projects describe PROJECT_ID
      9. Terraform を初期化します。
        terraform init
      10. ポスチャーを作成してデプロイする

        1. Cloud Shell で Cloud Shell エディタを起動します。エディタを起動するには、Cloud Shell ウィンドウのツールバーにある コードエディタ ボタン [エディタを開く] をクリックします。

        2. example-standard.yaml という名前の YAML ファイルを作成します。

        3. 次のコードをファイルに貼り付けます。

        name: organizations/ORGANIZATION_ID/locations/global/postures/example-standard
        state: ACTIVE
        policySets:
        - policies:
          - constraint:
              orgPolicyConstraintCustom:
                customConstraint:
                  actionType: ALLOW
                  condition: "resource.initialNodeCount == 3"
                  description: Set initial node count to be exactly 3.
                  displayName: fixedNodeCount
                  methodTypes:
                  - CREATE
                  name: organizations/ORGANIZATION_ID/customConstraints/custom.fixedNodeCount
                  resourceTypes:
                  - container.googleapis.com/NodePool
                policyRules:
                - enforce: true
            policyId: fixedNodeCount
          - constraint:
              securityHealthAnalyticsCustomModule:
                config:
                  customOutput: {}
                  description: Set MTU for a network to be exactly 1000.
                  predicate:
                    expression: "!(resource.mtu == 1000)"
                  recommendation: Only create networks whose MTU is 1000.
                  resourceSelector:
                    resourceTypes:
                    - compute.googleapis.com/Network
                  severity: HIGH
                displayName: fixedMTU
                moduleEnablementState: ENABLED
            policyId: fixedMTU
          - constraint:
              securityHealthAnalyticsModule:
                moduleEnablementState: ENABLED
                moduleName: BUCKET_POLICY_ONLY_DISABLED
            policyId: bucket_policy_only_disabled
          - constraint:
              securityHealthAnalyticsModule:
                moduleEnablementState: ENABLED
                moduleName: BUCKET_LOGGING_DISABLED
            policyId: bucket_logging_disabled
          policySetId: policySet1

        ORGANIZATION_ID は、実際の組織 ID に置き換えます。

        1. Cloud Shell で、ポスチャーを作成します。

          gcloud scc postures create organizations/ORGANIZATION_ID/locations/global/postures/example-standard --posture-from-file=example-standard.yaml
          
        2. コマンドで生成されるポスチャー リビジョン ID をコピーします。

        3. ポスチャーをプロジェクトにデプロイします。

          gcloud scc posture-deployments create organizations/ORGANIZATION_ID/locations/global/postureDeployments/example-standard \
          --posture-name=organizations/ORGANIZATION_ID/locations/global/postures/example-standard \
          --posture-revision-id="POSTURE_REVISION_ID" \
          --target-resource=projects/PROJECT_NUMBER
          

          次のように置き換えます。

          • ORGANIZATION_ID: 組織の ID。
          • POSTURE REVISION_ID: コピーしたポスチャーのリビジョン ID。
          • PROJECT_NUMBER: プロジェクトの番号。

        Terraform ファイルを作成して検証する

        1. Cloud Shell で Cloud Shell エディタを起動します。

        2. main.tf という名前の Terraform ファイルを作成します。

        3. 次のコードをファイルに貼り付けます。

          terraform {
            required_providers {
              google = {
                source  = "hashicorp/google"
              }
            }
          }
          
          provider "google" {
            region  = "us-central1"
            zone    = "us-central1-c"
          }
          
          resource "google_compute_network" "example_network"{
            name                            = "example-network-1"
            delete_default_routes_on_create = false
            auto_create_subnetworks         = false
            routing_mode                    = "REGIONAL"
            mtu                             = 100
            project                         = "PROJECT_ID"
          }
          
          resource "google_container_node_pool" "example_node_pool" {
            name               = "example-node-pool-1"
            cluster            = "example-cluster-1"
            project            = "PROJECT_ID"
            initial_node_count = 2
          
            node_config {
              preemptible  = true
              machine_type = "e2-medium"
            }
          }
          
          resource "google_storage_bucket" "example_bucket" {
            name          = "example-bucket-1"
            location      = "EU"
            force_destroy = true
          
            project = "PROJECT_ID"
          
            uniform_bucket_level_access = false
          }
          

          PROJECT_ID は、作成したプロジェクトのプロジェクト ID に置き換えます。

        4. Cloud Shell で、Terraform プランファイルを作成して JSON 形式に変換します。

          terraform plan -out main.plan
          terraform show -json main.plan > mainplan.json
          
        5. mainplan.json の IaC 検証レポートを作成します。

          gcloud scc iac-validation-reports create organizations/ORGANIZATION_ID/locations/global --tf-plan-file=mainplan.json
          

          このコマンドは、次の違反を記述する IaC 検証レポートを返します。

          • example_networkmtu が 1,000 ではない。
          • example_node_poolinitial_node_count が 3 ではない。
          • example_bucket で均一なバケットレベルのアクセスが有効になっていない。
          • example_bucket でロギングが有効になっていない。

        違反を解決する

        1. Cloud Shell で Cloud Shell エディタを起動します。

        2. main.tf ファイルを更新して、次の変更を加えます。

          terraform {
            required_providers {
              google = {
                source  = "hashicorp/google"
              }
            }
          }
          
          provider "google" {
            region  = "us-central1"
            zone    = "us-central1-c"
          }
          
          resource "google_compute_network" "example_network"{
            name                            = "example-network-1"
            delete_default_routes_on_create = false
            auto_create_subnetworks         = false
            routing_mode                    = "REGIONAL"
            mtu                             = 1000
            project                         = "PROJECT_ID"
          }
          
          resource "google_container_node_pool" "example_node_pool" {
            name               = "example-node-pool-1"
            cluster            = "example-cluster-1"
            project            = "PROJECT_ID"
            initial_node_count = 3
          
            node_config {
              preemptible  = true
              machine_type = "e2-medium"
            }
          }
          
          resource "google_storage_bucket" "example_bucket" {
            name          = "example-bucket-1"
            location      = "EU"
            force_destroy = true
          
            project = "PROJECT_ID"
            uniform_bucket_level_access = true
          
            logging {
              log_bucket   = "my-unique-logging-bucket" // Create a separate bucket for logs
              log_object_prefix = "tf-logs/"             // Optional prefix for better structure
            }
          }
          

          PROJECT_ID は、作成したプロジェクトのプロジェクト ID に置き換えます。

        3. Cloud Shell で、Terraform プランファイルを作成して JSON 形式に変換します。

          terraform plan -out main.plan
          terraform show -json main.plan > mainplan.json
          
        4. mainplan.json の IaC 検証レポートを再作成します。

          gcloud scc iac-validation-reports create organizations/ORGANIZATION_ID/locations/global --tf-plan-file=mainplan.json
          

      クリーンアップ

      このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。

      プロジェクトを削除する

      1. In the Google Cloud console, go to the Manage resources page.

        Go to Manage resources

      2. In the project list, select the project that you want to delete, and then click Delete.
      3. In the dialog, type the project ID, and then click Shut down to delete the project.

      次のステップ