샘플 IaC 검증 보고서 만들기


이 튜토리얼에서는 코드형 인프라(IaC)가 조직 정책 또는 Security Health Analytics 감지기를 위반하지 않는지 확인하는 방법을 설명합니다.

목표

  • 보안 상황을 만듭니다.
  • 프로젝트에 상황을 배포합니다.
  • 예시 Terraform 파일에서 위반을 확인합니다.
  • Terraform 파일에서 위반을 수정하고 파일을 다시 확인하여 수정 사항을 확인합니다.

시작하기 전에

권한 설정

  1. 조직에 다음 역할이 있는지 확인합니다. Project Creator and Security Posture Admin

    역할 확인

    1. Google Cloud 콘솔에서 IAM 페이지로 이동합니다.

      IAM으로 이동
    2. 조직을 선택합니다.
    3. 주 구성원 열에서 이메일 주소가 있는 행을 찾습니다.

      이메일 주소가 열에 없으면 역할이 없는 것입니다.

    4. 이메일 주소가 있는 행에 대해 역할 열에서 역할 목록에 필요한 역할이 있는지 확인합니다.

    역할 부여

    1. Google Cloud 콘솔에서 IAM 페이지로 이동합니다.

      IAM으로 이동
    2. 조직을 선택합니다.
    3. 액세스 권한 부여를 클릭합니다.
    4. 새 주 구성원 필드에 이메일 주소를 입력합니다.
    5. 역할 선택 목록에서 역할을 선택합니다.
    6. 역할을 추가로 부여하려면 다른 역할 추가를 클릭하고 각 역할을 추가합니다.
    7. 저장을 클릭합니다.

Cloud Shell 설정

  1. Google Cloud 콘솔에서 Cloud Shell을 활성화합니다.

    Cloud Shell 활성화

    Google Cloud 콘솔 하단에서 Cloud Shell 세션이 시작되고 명령줄 프롬프트가 표시됩니다. Cloud Shell은 Google Cloud CLI가 사전 설치된 셸 환경으로, 현재 프로젝트의 값이 이미 설정되어 있습니다. 세션이 초기화되는 데 몇 초 정도 걸릴 수 있습니다.

  2. 조직 ID를 찾습니다.
    gcloud organizations list

환경 준비

  1. Google Cloud 계정에 로그인합니다. Google Cloud를 처음 사용하는 경우 계정을 만들고 Google 제품의 실제 성능을 평가해 보세요. 신규 고객에게는 워크로드를 실행, 테스트, 배포하는 데 사용할 수 있는 $300의 무료 크레딧이 제공됩니다.
  2. Install the Google Cloud CLI.
  3. To initialize the gcloud CLI, run the following command:

    gcloud init
  4. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  5. Google Cloud 프로젝트에 결제가 사용 설정되어 있는지 확인합니다.

  6. Security posture service and Security Command Center management API를 사용 설정합니다.

    gcloud services enable securityposture.googleapis.com  securitycentermanagement.googleapis.com
  7. Install the Google Cloud CLI.
  8. To initialize the gcloud CLI, run the following command:

    gcloud init
  9. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  10. Google Cloud 프로젝트에 결제가 사용 설정되어 있는지 확인합니다.

  11. Security posture service and Security Command Center management API를 사용 설정합니다.

    gcloud services enable securityposture.googleapis.com  securitycentermanagement.googleapis.com
  12. 프로젝트 ID를 복사합니다.
    gcloud projects describe PROJECT_ID
  13. Terraform을 초기화합니다.
    terraform init

상황 만들기 및 배포

  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:
    - policySetId: policySet1
    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
    

    ORGANIZATION_ID를 조직 ID로 바꿉니다.

  4. Cloud Shell에서 상황을 만듭니다.

    gcloud scc postures create organizations/ORGANIZATION_ID/locations/global/postures/example-standard --posture-from-file=example-standard.yaml
    
  5. 이 명령어로 생성되는 상황 버전 ID를 복사합니다.

  6. 프로젝트에 상황을 배포합니다.

    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_ID
    

    다음을 바꿉니다.

    • ORGANIZATION_ID: 조직 ID입니다.
    • POSTURE REVISION_ID: 복사한 상황 버전 ID입니다.
    • PROJECT_ID: 프로젝트 ID입니다.

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가 1000이 아닙니다.
    • 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 계정에 청구되지 않도록 하려면 리소스가 포함된 프로젝트를 삭제하거나 프로젝트를 유지하고 개별 리소스를 삭제하세요.

프로젝트 삭제

    Google Cloud 프로젝트를 삭제합니다.

    gcloud projects delete PROJECT_ID

다음 단계