在 Cloud Build 管道中使用 On-Demand Scanning,即可在容器映像檔含有嚴重程度符合預先定義層級的安全漏洞時,封鎖建構作業。
本教學課程說明如何使用 Cloud Build 從原始碼建構容器映像檔、掃描安全漏洞、檢查安全漏洞的嚴重程度,以及在沒有特定嚴重程度的安全漏洞時,將映像檔推送至 Artifact Registry。
建議您為本教學課程建立新的 Google Cloud 專案,並在獨立環境中完成步驟。
目標
- 使用 Cloud Build 建構映像檔。
- 使用隨選掃描功能掃描建構的映像檔。
- 評估可接受的安全漏洞等級。
- 將映像檔儲存在 Artifact Registry 中。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。
事前準備
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the On-Demand Scanning, Cloud Build, and Artifact Registry APIs.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the On-Demand Scanning, Cloud Build, and Artifact Registry APIs.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
On-Demand Scanning 管理員 (
roles/ondemandscanning.admin
)Artifact Registry 寫入者 (
roles/artifactregistry.writer
)
必要的角色
您搭配 Cloud Build 使用的服務帳戶必須具備下列角色:
預設的 Cloud Build 服務帳戶具備相同專案中 Artifact Registry 存放區的必要權限。如果存放區與您用於 Cloud Build 的專案相同,您只需要授予「隨選掃描管理員」角色。
如果您使用使用者提供的 Cloud Build 服務帳戶,則必須授予這兩個角色。
準備來源檔案
在本教學課程中,您將從 Dockerfile 建構映像檔。Dockerfile 是原始碼檔案,其中包含 Docker 建構映像檔的指令。
開啟終端機,建立名為
ods-tutorial
的新目錄,然後前往該目錄:mkdir ods-tutorial && cd ods-tutorial
建立名為
Dockerfile
的檔案,並在當中加入下列內容:# Debian10 image FROM gcr.io/google-appengine/debian10:latest # Ensures that the built image is always unique RUN apt-get update && apt-get -y install uuid-runtime && uuidgen > /IAMUNIQUE
建立 Artifact Registry 存放區
將專案 ID 設為您啟用 API 的專案:
gcloud config set project PROJECT_ID
在
us-central1
位置中新建名為ods-build-repo
的 Docker 存放區:gcloud artifacts repositories create ods-build-repo --repository-format=docker \ --location=us-central1 --description="Repository for scan and build"
確認存放區是否已成功建立:
gcloud artifacts repositories list
建構及掃描
在本節中,您將使用建構設定檔執行建構管道。建構設定檔會根據您的規格,指示 Cloud Build 執行多項工作。
在
ods-tutorial/
資料夾中,建立cloudbuild.yaml
檔案,並加入下列內容:steps: - id: build name: gcr.io/cloud-builders/docker entrypoint: /bin/bash args: - -c - | docker build -t us-central1-docker.pkg.dev/$_PROJECT_ID/ods-build-repo/ods-test:latest -f ./Dockerfile . && docker image inspect us-central1-docker.pkg.dev/$_PROJECT_ID/ods-build-repo/ods-test:latest --format \ '{{index .RepoTags 0}}@{{.Id}}' > /workspace/image-digest.txt && cat image-digest.txt - id: scan name: gcr.io/google.com/cloudsdktool/cloud-sdk entrypoint: /bin/bash args: - -c - | gcloud artifacts docker images scan us-central1-docker.pkg.dev/$_PROJECT_ID/ods-build-repo/ods-test:latest \ --format='value(response.scan)' > /workspace/scan_id.txt - id: severity check name: gcr.io/google.com/cloudsdktool/cloud-sdk entrypoint: /bin/bash args: - -c - | gcloud artifacts docker images list-vulnerabilities $(cat /workspace/scan_id.txt) \ --format='value(vulnerability.effectiveSeverity)' | if grep -Exq $_SEVERITY; \ then echo 'Failed vulnerability check' && exit 1; else exit 0; fi - id: push name: gcr.io/cloud-builders/docker entrypoint: /bin/bash args: - -c - | docker push us-central1-docker.pkg.dev/$_PROJECT_ID/ods-build-repo/ods-test:latest images: ['us-central1-docker.pkg.dev/$_PROJECT_ID/ods-build-repo/ods-test:latest']
這個檔案包含先前在 Artifact Registry 中建立的位置和存放區。如果決定使用其他值,請相應修改
cloudbuild.yaml
檔案。建構指令中的PROJECT_ID
和SEVERITY
值會傳遞至指令碼。指定要封鎖的
SEVERITY
漏洞等級,然後開始建構。SEVERITY
可使用下列值:CRITICAL
HIGH
MEDIUM
LOW
您可以使用規則運算式指定多個嚴重程度。
在下列範例中,您同時指定
CRITICAL
和HIGH
嚴重程度值。這會指示 Cloud Build 檢查嚴重程度為HIGH
以上的安全漏洞。gcloud builds submit --substitutions=_PROJECT_ID=PROJECT_ID,_SEVERITY='"CRITICAL|HIGH"' \ --config cloudbuild.yaml
地點
- PROJECT_ID 是您的專案 ID。
- SEVERITY 可讓您設定要封鎖的嚴重程度。如果隨選掃描發現符合任何指定嚴重性等級的安全漏洞,建構作業就會失敗。
瞭解結果
將 SEVERITY
值設為 CRITICAL|HIGH
後,隨選掃描功能會掃描是否有 HIGH
級別和更嚴重的 CRITICAL
級別的安全性漏洞。如果映像檔中沒有相符的安全漏洞,建構作業就會成功,Cloud Build 也會將映像檔推送至 Artifact Registry。
輸出結果會與下列內容相似:
DONE
--------------------------------------------------------------------------------------------------------------------------------------------
ID CREATE_TIME DURATION SOURCE IMAGES STATUS
abb3ce73-6ae8-41d1-9080-7d74a7ecd7bc 2021-03-15T06:50:32+00:00 1M48S gs://ods-tests_cloudbuild/source/1615791031.906807-a648d10faf4a46d695c163186a6208d5.tgz us-central1-docker.pkg.dev/ods-tests/ods-build-repo/ods-test (+1 more) SUCCESS
如果隨選掃描在映像檔中發現 HIGH
或 CRITICAL
安全漏洞,scan
建構步驟就會失敗,後續建構步驟不會啟動,且 Cloud Build 不會將映像檔推送至 Artifact Registry。
輸出結果會與下列內容相似:
Step #2 - "severity check": Failed vulnerability check
Finished Step #2 - "severity check"
ERROR
ERROR: build step 2 "gcr.io/cloud-builders/gcloud" failed: step exited with non-zero status: 1
在本教學課程中,您的結果可能會有所不同,因為範例原始碼是公開發行的 Linux 發行版 debian10:latest
。Linux 發行版本和相關安全漏洞資料會持續更新。
如要瞭解其他有助於保護軟體供應鏈的 Google Cloud 工具和最佳做法,請參閱「軟體供應鏈安全性」。
如要進一步瞭解 Linux 漏洞管理最佳做法,請參加 Linux Foundation 提供的免費線上訓練課程。請參閱「開發安全軟體」。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。
刪除專案
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
刪除個別資源
移除存放區之前,請先確認要保留的映像檔均已存放於其他位置。
如要刪除存放區,請按照下列指示操作:
主控台
在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。
在存放區清單中,選取
ods-build-repo
存放區。點選「刪除」。
gcloud
如要刪除 ods-build-repo
存放區,請執行下列指令:
gcloud artifacts repositories delete ods-build-repo --location=us-central1