On-Demand Scanning API 可讓您掃描儲存在電腦本機或遠端 Artifact Registry 中的映像檔。您可以使用隨選掃描掃描 CI/CD 管道中的映像檔,瞭解系統安全漏洞和 Java (Maven) 套件安全漏洞,再決定是否要儲存在註冊資料庫中。如需定價資訊,請參閱「定價頁面」。
本頁面說明如何手動掃描容器映像檔,找出系統安全漏洞和 Maven 套件安全漏洞。
事前準備
-
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 API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
將 local-extract 元件新增至 Google Cloud CLI 安裝項目
使用 Google Cloud CLI 元件管理服務
首次執行掃描指令時,元件管理服務會提示您安裝必要元件。
使用系統套件管理工具
-
適用於 Debian/Ubuntu:
sudo apt install google-cloud-sdk-local-extract
-
Red Hat/Fedora/CentOS:
sudo dnf install google-cloud-sdk-local-extract
或者,您也可以在上一個指令中將
dnf替換為yum。
-
適用於 Debian/Ubuntu:
- 將 IAM 角色「隨選掃描管理員」授予要搭配隨選掃描功能使用的使用者或服務帳戶。如果您使用專案的擁有者帳戶執行掃描,可以略過這個步驟。
必要的角色
如要取得查看套件掃描結果所需的權限,請要求管理員授予您專案的下列 IAM 角色:
- Container Scanner 服務代理程式 (
roles/containerscanning.ServiceAgent) - 容器分析例項檢視者 (
roles/containeranalysis.occurrences.viewer) - 容器分析筆記檢視者 (
roles/containeranalysis.notes.viewer) - Artifact Registry 讀取者 (
roles/artifactregistry.reader)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
掃描容器映像檔
掃描區域網路:
gcloud artifacts docker images scan IMAGE_URI \ [--location=(us,europe,asia)] [--async]如果是本機圖片,請使用下列其中一種格式的 IMAGE_URI:
REPOSITORY:TAGREPOSITORY
遠端掃描:
gcloud artifacts docker images scan IMAGE_URI \ --remote [--location=(us,europe,asia)] [--async]如果是遠端圖片,請使用下列其中一種 IMAGE_URI 格式:
HOSTNAME/PROJECT_ID/REPOSITORY_ID/IMAGE_ID@sha256:HASHHOSTNAME/PROJECT_ID/REPOSITORY_ID/IMAGE_ID:HASHHOSTNAME/PROJECT_ID/REPOSITORY_ID/IMAGE_IDHOSTNAME/PROJECT_ID/IMAGE_ID@sha256:HASHHOSTNAME/PROJECT_ID/IMAGE_ID:HASHHOSTNAME/PROJECT_ID/IMAGE_ID
如果是 Artifact Registry 中的映像檔,IMAGE_URI 必須包含
REPOSITORY_ID。
無論是本機或遠端掃描,您都可以使用下列選用旗標:
--location是選用旗標,可手動選取要執行掃描作業的多區域。選擇距離您實際位置較近的多區域,可盡量縮短延遲時間。可用的位置包括:us、europe和asia。預設位置為us。--async是選用旗標,可非同步執行掃描程序。如果省略這個旗標,終端機會封鎖,直到掃描程序完成。
同步掃描
以下範例顯示同步掃描的輸出內容,沒有 --async 標記:
$ gcloud artifacts docker images scan jenkins:2.60.3-alpine ✓ Scanning container image ✓ Locally extracting packages and versions from local container image ✓ Remotely initiating analysis of packages and versions ✓ Waiting for analysis operation to complete [projects/my-project/locations/us/operations/87d2e137-1d1c-4790-8e5e-daf6c96ae7d7] Done. done: true metadata: '@type': type.googleapis.com/google.cloud.ondemandscanning.v1.AnalyzePackagesMetadata createTime: '2021-01-05T23:03:04.185261Z' resourceUri: jenkins:2.60.3-alpine name: projects/my-project/locations/us/operations/87d2e137-1d1c-4790-8e5e-daf6c96ae7d7 response: '@type': type.googleapis.com/google.cloud.ondemandscanning.v1.AnalyzePackagesResponse scan: projects/my-project/locations/us/scans/2fe2bfb8-f0c5-4dd6-a8c8-38961869767a
使用掃描名稱擷取安全漏洞結果。以這個範例來說,掃描名稱是輸出訊息最後一行中 scan 的值。
非同步掃描
以下範例顯示執行非同步掃描的輸出內容:
$ gcloud artifacts docker images scan jenkins:2.60.3-alpine --async ✓ Scanning container image ✓ Locally extracting packages and versions from local container image ✓ Remotely initiating analysis of packages and versions Done. Check operation [projects/cloud-project/locations/us/operations/2e1a6b1f-16e5-4427-ac86-72c998a3dd16] for status. metadata: '@type': type.googleapis.com/google.cloud.ondemandscanning.v1.AnalyzePackagesMetadata createTime: '2021-01-05T23:04:54.393510Z' resourceUri: jenkins:2.60.3-alpine name: projects/my-project/locations/us/operations/2e1a6b1f-16e5-4427-ac86-72c998a3dd16
這會啟動長時間執行的作業並傳回其 ID,不會封鎖終端機。使用作業 ID (輸出訊息最後一行中的 name 值) 輪詢作業。
輪詢長時間執行的作業
使用非同步掃描指令輸出內容中的作業 ID,檢查作業狀態:
gcloud artifacts docker images get-operation LRO_ID
其中的 LRO_ID 為長時間執行的作業 ID。
接續非同步掃描部分的範例,如要檢查作業狀態,請執行下列操作:
$ gcloud artifacts docker images get-operation \ projects/cloud-project/locations/us/operations/2e1a6b1f-16e5-4427-ac86-72c998a3dd16 done: true metadata: '@type': type.googleapis.com/google.cloud.ondemandscanning.v1.AnalyzePackagesMetadata createTime: '2021-01-05T23:04:54.393510Z' resourceUri: jenkins:2.60.3-alpine name: projects/my-project/locations/us/operations/2e1a6b1f-16e5-4427-ac86-72c998a3dd16 response: '@type': type.googleapis.com/google.cloud.ondemandscanning.v1.AnalyzePackagesResponse scan: projects/my-project/locations/us/scans/2fe2bfb8-f0c5-4dd6-a8c8-38961869767a
如果輸出內容包含 done: true 這一行,表示掃描作業已完成。使用掃描名稱擷取安全漏洞結果。
掃描名稱是輸出訊息最後一行中的 scan 值。
擷取掃描結果
如要在掃描作業完成後擷取掃描結果,請使用下列指令:
gcloud artifacts docker images list-vulnerabilities SCAN_NAME [--limit=X]
其中:
SCAN_NAME 是掃描名稱。掃描完成後,您可以執行同步掃描或輪詢長時間執行的作業,輸出內容的最後一行會顯示該值。
--limit=X是選用標記,可限制輸出內容中顯示的出現次數。X是數值。
例如:
createTime: '2021-06-29T17:01:18.819477Z'
kind: VULNERABILITY
name: projects/my-project/locations/us/occurrences/06305977-f557-4772-8586-4260684291d3
noteName: projects/my-project/notes/CVE-2014-0114
resourceUri: jenkins:2.60.3-alpine
updateTime: '2021-06-29T17:01:18.819477Z'
vulnerability:
cvssScore: 4.3
effectiveSeverity: MEDIUM
longDescription: http/conn/ssl/SSLConnectionSocketFactory.java in ...
packageIssue:
- affectedCpeUri: cpe:/o:alpine:alpine_linux:3.13
affectedPackage: org.apache.httpcomponents:httpclient
affectedVersion:
fullName: 4.0.2
kind: NORMAL
name: 4.0.2
effectiveSeverity: MEDIUM
fixedCpeUri: cpe:/o:alpine:alpine_linux:3.13
fixedPackage: org.apache.httpcomponents:httpclient
fixedVersion:
fullName: 4.3.6
kind: NORMAL
name: 4.3.6
packageType: MAVEN
relatedUrls:
- label: More Info
url: https://security-tracker.debian.org/tracker/CVE-2015-5262
severity: MEDIUM
shortDescription: CVE-2015-5262
這項指令的輸出內容是Grafeas 格式的發生事項清單。在本例中,系統顯示映像檔中發現一個中等嚴重程度的安全漏洞。Maven 套件安全漏洞包含 packageType:MAVEN 欄位。
掃描作業完成後,掃描結果會保留 48 小時。
後續步驟
- 在 Cloud Build pipeline 中使用 On-Demand Scanning API。
- 使用 Artifact Analysis 掃描並持續更新 Artifact Registry 中儲存的映像檔安全漏洞資訊。