根據 Google Cloud 機構的政策驗證 IaC

本文說明如何根據您在 Google Cloud 機構中定義的機構政策安全狀態分析偵測器,驗證基礎架構即程式碼 (IaC)。您可以使用 Terraform 等語言,透過 IaC 建立及管理雲端資源,並透過開發工作流程部署資源。IaC 驗證功能僅支援機構政策和 Security Health Analytics 偵測器。

驗證 IaC 可讓您判斷新的或修改過的資源定義是否違反套用至Google Cloud 資源 (例如叢集、儲存空間或執行個體) 的現有政策。您可以使用安全防護機制定義這些政策,但 IaC 驗證功能會分析程式碼,並將程式碼與部署在 Google Cloud 機構中的防護機制所定義的任何有效政策進行比較。IaC 驗證功能可協助開發人員在將資產或資源套用至Google Cloud 環境前,找出並修正 IaC 設定中的任何安全性問題。

IaC 驗證功能支援 Terraform 計畫檔案。您可以使用 Google Cloud CLI 驗證 Terraform 方案,也可以將驗證程序整合至 Cloud Build、Jenkins 或 GitHub Actions 開發人員工作流程。

事前準備

完成這些工作,即可開始使用 IaC 驗證。

啟用 Security Command Center Premium 級別或 Enterprise 級別

確認已在機構層級啟用 Security Command Center Premium 方案或 Enterprise 方案

啟用 Security Command Center 會啟用 securityposture.googleapis.comsecuritycentermanagement.googleapis.com API。

設定權限

  1. Make sure that you have the following role or roles on the organization: Security Posture Shift-Left Validator

    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. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]

    如要進一步瞭解 IaC 驗證權限,請參閱「機構層級啟用適用的 IAM」。

    設定 Google Cloud CLI

    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.

    如要設定 gcloud CLI,讓它使用服務帳戶模擬功能向 Google API 進行驗證,而不是使用您的使用者憑證,請執行下列指令:

    gcloud config set auth/impersonate_service_account SERVICE_ACCT_EMAIL

    詳情請參閱「服務帳戶模擬」。

    定義政策

    定義機構政策安全狀態分析偵測器。如要使用安全防護機制定義這些政策,請完成「建立及部署防護機制」中的工作。

    建立 Terraform 程式碼

    使用開發人員工作流程和工具,建立包含要建立或修改 Google Cloud 資產的 Terraform 檔案。

    請考量下列事項:

    • 在 Terraform 設定中,填入每個資源或資產的父項屬性 (專案、資料夾或機構)。
    • 分別修改資產和政策。API 不支援驗證同時修改資產和政策的 Terraform 計畫檔案。
    • 請只使用支援的資產類型和政策。如需支援的素材資源類型和政策清單,請參閱「支援的 IaC 驗證素材資源類型和政策」。
    • 請參閱 IaC 驗證的限制
    • 請勿在 Terraform 方案檔案中加入密碼或其他個人識別資訊等私密資訊。如果驗證功能在資源變更中遇到標示為敏感的欄位,系統會移除這些欄位。

    建立 Terraform 程式碼後,即可執行 IaC 驗證報告。您可以使用 gcloud CLI、Cloud BuildJenkinsGitHub Actions

    使用 Google Cloud CLI 建立 IaC 驗證報告

    如要建立 IaC 驗證報告,請完成下列步驟:

    1. 在 gcloud CLI 中執行 terraform init

      確認您執行的是 Terraform 供應商 v5.5 以上版本。如有需要,請升級至最新版 Google 供應商:

      terraform init -upgrade
      
    2. 將 Terraform 方案檔案轉換為 JSON 格式:

      terraform plan -out TF_PLAN_FILENAME
      terraform show -json TF_PLAN_FILENAME > TF_PLAN_JSON_FILENAME.json
      

      更改下列內容:

      • TF_PLAN_FILENAME:Terraform 方案檔案的名稱。
      • TF_PLAN_JSON_FILENAME:檔案名稱,其中包含 JSON 格式的 Terraform 方案。
    3. 建立 IaC 驗證報告:

      gcloud scc iac-validation-reports create PARENT \
        --tf-plan-file=TF_PLAN_JSON_FILENAME.json
      

      更改下列內容:

      • PARENT:要建立 IaC 驗證報告的 Google Cloud 機構。格式為 organizations/ORGANIZATION_ID/locations/LOCATIONLOCATIONglobal
      • TF_PLAN_JSON_FILENAME:包含要驗證 IaC 方案的 JSON 檔案路徑。

      舉例來說,如要在 organizations/3589215982/locations/global 機構下建立 IaC 驗證報告,並在 planFile.json 中加入 IaC 計畫,請執行下列指令:

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

      這項指令會傳回建立 IAC 驗證報告的作業詳細資料。如要取得作業相關資訊,請參閱「查看姿勢部署作業的相關資訊」。

    後續步驟