您可以使用「Analyze Code Security」動作,驗證屬於 GitHub Actions 工作流程的基礎架構即程式碼 (IaC)。驗證 IaC 可讓您判斷 Terraform 資源定義是否違反現有的機構政策,以及套用至 Google Cloud 資源的安全狀態分析偵測器。
如要進一步瞭解 IaC 驗證,請參閱「根據貴機構的政策驗證 IaC Google Cloud 」。
事前準備
完成這些工作,即可開始使用 GitHub Actions 進行 IaC 驗證。
啟用 Security Command Center Premium 級別或 Enterprise 級別
確認已在機構層級啟用 Security Command Center Premium 方案或 Enterprise 方案。
啟用 Security Command Center 會啟用 securityposture.googleapis.com
和 securitycentermanagement.googleapis.com
API。
建立服務帳戶
建立可供「Analyze Code Security」動作使用的服務帳戶。
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
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.
- Click Continue.
-
Click Done to finish creating the service account.
如要進一步瞭解 IaC 驗證權限,請參閱機構層級啟用適用的 IAM。
設定驗證方法
使用 GitHub 識別資訊提供者設定 Workload Identity 聯盟。如需操作說明,請參閱「Workload Identity 聯盟」。
取得 Workload Identity 聯盟 ID 權杖的網址。例如
https://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/providers/PROVIDER_ID
。請考量下列事項:
PROJECT_NUMBER
是您在其中設定 Workload Identity Federation 的Google Cloud 專案專案編號。POOL_ID
是集區名稱。PROVIDER_ID
是識別資訊提供者的名稱。
在工作流程中新增「Authenticate to Google Cloud」(向 Google Cloud 進行驗證) 動作,驗證 IaC 驗證動作。
定義政策
定義機構政策和安全狀態分析偵測器。如要使用安全防護機制定義這些政策,請完成「建立及部署防護機制」中的工作。
建立 Terraform 方案 JSON 檔案
建立 Terraform 程式碼。如需操作說明,請參閱「建立 Terraform 程式碼」。
在 GitHub Actions 中初始化 Terraform。舉例來說,如果您使用 HashiCorp - Setup Terraform 動作,請執行下列指令:
- name: Terraform Init id: init run: terraform init
建立 Terraform 計畫檔案:
- name: Create Terraform Plan id: plan run: terraform plan -out=TF_PLAN_FILE
將
TF_PLAN_FILE
替換為 Terraform 方案檔案的名稱。例如:myplan.tfplan
。將方案檔案轉換為 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
替換成 Terraform 方案檔案的名稱 (JSON 格式)。例如:mytfplan.json
。
將動作新增至 GitHub Actions 工作流程
- 在 GitHub 存放區中,瀏覽至您的工作流程。
- 開啟工作流程編輯器。
- 在 GitHub Marketplace 側欄中,搜尋「Analyze Code Security」。
- 在「安裝」部分,複製語法。
- 將語法貼到工作流程中做為新步驟。
替換下列值:
workload_identity_provider
,並提供 Workload Identity Federation ID 權杖的網址連結。service_account
,並填入您為動作建立的服務帳戶電子郵件地址。- 將
organization_id
改成您的 Google Cloud 機構 ID。 scan_file_ref
則改成 JSON 格式的 Terraform 方案檔案路徑。failure_criteria
,並設定失敗門檻條件,決定動作何時會失敗。系統會根據 IaC 驗證掃描遇到的嚴重、高、中和低嚴重性問題數量,設定閾值條件。failure_criteria
會指定允許的各嚴重程度問題數量,以及問題的彙整方式 (AND
或OR
)。舉例來說,如果希望動作在遇到一個嚴重問題或一個高嚴重程度問題時失敗,請將failure_criteria
設為Critical:1,High:1,Operator:OR
。預設值為Critical:1,High:1,Medium:1,Low:1,Operator:OR
,表示如果 IaC 驗證掃描作業發現任何問題,動作就必須失敗。
您現在可以執行工作流程,驗證 Terraform 方案檔案。如要手動執行工作流程,請參閱「手動執行工作流程」。
查看 IaC 違規報告
在 GitHub 存放區中,按一下「Actions」並選取工作流程。
按一下工作流程的最新執行作業。
在「構件」部分,違規事項報告 (
ias-scan-sarif.json
) 會以 zip 檔案形式提供。這份報表包含下列欄位:- 「
rules
」欄位,說明 Terraform 方案違反的政策。每項規則都包含ruleID
,可與報表中的結果相符。 results
欄位:說明違反特定規則的建議資產修改內容。
- 「
請先解決 Terraform 程式碼中的所有違規問題,再套用程式碼。
後續步驟
- 在 GitHub 中查看 analyze-code-security-scc 動作的原始碼。