您可以使用 Jenkins 適用的 Google Analyze Code Security 外掛程式,驗證 Jenkins 專案中的基礎架構即程式碼 (IaC)。驗證 IaC 可讓您判斷 Terraform 資源定義是否違反套用至 Google Cloud 資源的現有機構政策和安全狀態分析偵測器。
如要進一步瞭解 IaC 驗證,請參閱「根據貴機構的政策驗證 IaC Google Cloud 」。
IaC 驗證僅適用於 Jenkins Freestyle 專案。
事前準備
完成這些工作,即可開始使用 Jenkins 進行 IaC 驗證。
啟用 Security Command Center Premium 級別或 Enterprise 級別
確認已在機構層級啟用 Security Command Center Premium 方案或 Enterprise 方案。
啟用 Security Command Center 會啟用 securityposture.googleapis.com
和 securitycentermanagement.googleapis.com
API。
建立服務帳戶
建立服務帳戶,供 Jenkins 適用的 Google Analyze Code Security 外掛程式使用。
-
Create a service account:
-
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.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
如要進一步瞭解 IaC 驗證權限,請參閱「機構層級啟用適用的 IAM」。
定義政策
定義機構政策和安全狀態分析偵測器。如要使用安全防護機制定義這些政策,請完成「建立及部署防護機制」中的工作。
安裝及設定外掛程式
- 在 Jenkins 控制台中,依序點選「Manage Jenkins」 >「Manage Plugins」。
- 在「Available」(可用) 分頁中,搜尋「google-analyze-code-security」。
- 完成安裝步驟。
- 依序點選「Manage Jenkins」 >「Configure System」。
- 在「Google Analyze Code Security」部分中,按一下「新增憑證」。
- 在「機構 ID」中,輸入包含要建立或修改的 Terraform 資源的 Google Cloud機構 ID。
- 在「Security Command Center Credential」(Security Command Center 憑證) 中,新增服務帳戶金鑰。
- 測試連線,驗證服務帳戶憑證。
- 按一下 [儲存]。
建立 Terraform 方案 JSON 檔案
建立 Terraform 程式碼。如需操作說明,請參閱「建立 Terraform 程式碼」。
在 Jenkins 控制台中,前往 Jenkins 任意樣式專案的「Configuration」(設定) 頁面。
按一下「Source Code Management」(原始碼管理)。
在「存放區網址」中,輸入您建立的 Terraform 程式碼網址。
按一下「建構步驟」。
新增下列步驟:
初始化 Terraform:
terraform init
建立 Terraform 計畫檔案。
terraform plan -out=TF_PLAN_FILE
將
TF_PLAN_FILE
替換為 Terraform 方案檔案的名稱。例如:myplan.tfplan
。將方案檔案轉換為 JSON 格式:
terraform show -no-color -json TF_PLAN_FILE > TF_PLAN_JSON_FILE
將
TF_PLAN_JSON_FILE
替換成 Terraform 方案檔案的名稱 (JSON 格式)。例如:mytfplan.json
。
將外掛程式新增至 Jenkins 專案
- 在 Jenkins 控制台中,前往 Jenkins 任意樣式專案的「Configuration」(設定) 頁面。
- 在「建構步驟」中,依序點選「新增建構步驟」 >「在建構期間執行程式碼掃描」。
- 輸入機構 ID。
- 以 JSON 格式提供 Terraform 方案檔案的路徑。
選用:設定建構失敗條件。失敗條件是根據 IaC 驗證掃描遇到的嚴重、高、中和低嚴重性問題數量而定。您可以指定允許的各嚴重程度問題數量,以及問題的彙整方式 (AND 或 OR)。
按一下「Fail on Asset Violation」(因資產違規而失敗)。
如要讓建構作業只有在達到所有嚴重程度層級的問題計數時才失敗,請選取「AND」。如要讓建構作業在達到任何嚴重程度的問題計數時失敗,請選取「OR」。舉例來說,如要讓建構作業在遇到一個嚴重問題或一個高嚴重程度問題時失敗,請將匯總值設為「OR」。
指出您允許的各種嚴重程度問題數量,超過此數量時建構作業就會失敗。
如不想指定失敗條件,請選取「忽略資產違規」。
按一下 [儲存]。
您現在可以執行建構作業,驗證 Terraform 方案檔案。
查看 IaC 違規報告
在 Jenkins 控制台中,按一下建構作業的最新工作流程。
按一下「狀態」。下列 HTML 檔案可做為建構構件:
- 如果外掛程式已執行,違規報告 (
GoogleAnalyzeCodeSecurity_ViolationSummary.html
)
報表會依違規事項的嚴重程度分類。違規事項部分會說明未符合的規則,以及違反規則的 Terraform 方案中的資產 ID。
- 如果建構失敗,系統會提供錯誤摘要報表
- 如果外掛程式已執行,違規報告 (
請先解決 Terraform 程式碼中的所有違規問題,再套用程式碼。