在 Cloud Build 管道中使用 On-Demand Scanning,即可在容器映像檔含有嚴重程度符合預先定義層級的安全漏洞時,封鎖建構作業。
本教學課程說明如何使用 Cloud Build 從原始碼建構容器映像檔、掃描安全漏洞、檢查安全漏洞的嚴重程度,以及在沒有特定嚴重程度的安全漏洞時,將映像檔推送至 Artifact Registry。
建議您為本教學課程建立新的 Google Cloud 專案,並在獨立環境中完成步驟。
目標
- 使用 Cloud Build 建構映像檔。
- 使用 On-Demand Scanning 掃描建構的映像檔。
- 評估可接受的安全漏洞等級。
- 將映像檔儲存在 Artifact Registry 中。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the On-Demand Scanning, Cloud Build, and Artifact Registry APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the On-Demand Scanning, Cloud Build, and Artifact Registry APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init
必要的角色
您搭配 Cloud Build 使用的服務帳戶必須具備下列角色:
隨選掃描管理員 (
roles/ondemandscanning.admin)Artifact Registry 寫入者 (
roles/artifactregistry.writer)
預設的 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使用下列值:CRITICALHIGHMEDIUMLOW
您可以使用規則運算式指定多個嚴重程度。
在下列範例中,您同時指定
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 後,On-Demand Scanning 會掃描是否有 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 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。
刪除個別資源
移除存放區之前,請先確認要保留的映像檔均已存放於其他位置。
如要刪除存放區,請按照下列指示操作:
控制台
在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。
在存放區清單中,選取
ods-build-repo存放區。點選「刪除」。
gcloud
如要刪除 ods-build-repo 存放區,請執行下列指令:
gcloud artifacts repositories delete ods-build-repo --location=us-central1