自動掃描包裹

本文說明如何啟用 Container Scanning API、將映像檔推送至 Artifact Registry,以及查看映像檔中發現的安全漏洞清單。

Artifact Analysis 會將安全漏洞資訊儲存為附註。每當某個映像檔有相關聯的註記時,系統便會建立相對應的例項。詳情請參閱總覽定價文件。

事前準備

  1. 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.
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry and Container Scanning APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Artifact Registry and Container Scanning APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init
  14. 在 Artifact Registry 中建立存放區,然後將含有應用程式碼的套件推送至該存放區。如果您不熟悉如何管理 Artifact Registry 中的套件,請參閱下列主題:
    1. 在 Artifact Registry 中儲存 Java 套件
    2. 在 Artifact Registry 中儲存 Node.js 套件
    3. 在 Artifact Registry 中儲存 Python 套件

啟用這項 API 也會啟用 Artifact Registry 中的語言套件掃描功能。請參閱支援的套件類型

查看套件安全漏洞

將新套件上傳至 Artifact Registry 時,Artifact Analysis 會掃描這些套件。這項掃描會擷取套件及其依附元件的相關資訊。

您可以使用 Google Cloud 控制台、Google Cloud CLI 或 Container Analysis API,查看 Artifact Registry 中映像檔的安全漏洞例項。如果映像檔有安全漏洞,您就可以取得詳細資料。

只要映像檔和套件在過去 30 天內曾遭提取,Artifact Analysis 就會持續掃描。30 天後,系統將停止更新掃描過的映像檔和套件中繼資料,結果也會過時。

Artifact Analysis 會封存過時逾 90 天的中繼資料。您只能使用 API 評估這類封存的中繼資料。 如要重新掃描中繼資料已過時或已封存的映像檔,請提取該映像檔。 中繼資料重新整理作業最多可能需要 24 小時才能完成。如果套件的後設資料過時或已封存,就無法重新掃描。

在 Google Cloud 控制台中查看發生次數

如要查看套件中的安全漏洞,請按照下列步驟操作:

  1. 取得存放區清單。

    開啟「Repositories」(存放區) 頁面

  2. 在存放區清單中,按一下存放區。

  3. 在套件清單中,按一下套件名稱。

    每個套件的所有安全漏洞均會顯示在「Vulnerabilities」(安全漏洞) 資料欄中。

    掃描後發現有安全漏洞的套件

  4. 如要查看套件的安全漏洞清單,請按一下「Vulnerabilities」(安全漏洞) 資料欄中的連結。

    「掃描結果」部分會顯示掃描的套件類型摘要、安全漏洞總數、有修正檔的安全漏洞、沒有修正檔的安全漏洞,以及有效嚴重程度。

    「掃描結果」頁面會列出安全漏洞、修正程式和有效嚴重程度

    安全漏洞表格會列出每個發現的安全漏洞的常見安全漏洞與資料外洩風險 (CVE) 名稱、有效嚴重程度、常見安全漏洞評分系統 (CVSS) 分數、修正 (如有)、內含安全漏洞的套件名稱,以及套件類型。您可以篩選及排序這些檔案,依副檔名檢查特定檔案、目錄或檔案類型。 Google Cloud 主控台會在表格中顯示最多 1200 個網頁的安全性弱點。

  5. 如要查看特定 CVE 的詳細資料,請按一下 CVE 名稱。

  6. 如要查看安全漏洞發生詳細資料,例如版本號碼和受影響的位置,請在安全漏洞名稱所在列中,按一下「查看」或「查看已修正」。如果安全漏洞沒有修正,連結文字會顯示「查看」;如果安全漏洞已修正,連結文字則會顯示「查看已修正」

使用 gcloud 查看發生次數

如要查看 Artifact Registry 中套件的發生事項,請執行 gcloud artifacts versions describe 指令:

gcloud artifacts versions describe VERSION \
    --location=LOCATION --repository=REPOSITORY --package=PACKAGE --show-package-vulnerability

其中:

  • VERSION 是您要查看例項的套件版本。
  • LOCATION 是存放區的區域或多區域位置
  • REPOSITORY 是儲存套件的存放區名稱。
  • PACKAGE 是存放區中的套件名稱。 您無法使用此指令指定套件標記。

使用 API 查看發生次數

如何取得專案的例項清單:

 curl -X GET -H "Content-Type: application/json" -H \
    "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences

如何取得專案的安全漏洞匯總資料:

 curl -X GET -H "Content-Type: application/json" -H \
    "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences:vulnerabilitySummary

如要取得特定例項的詳細資料:

 curl -X GET -H "Content-Type: application/json" -H \
    "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences/OCCURRENCE_ID

篩選例項

您可以在 gcloud 指令和 Artifact Analysis API 中使用篩選字串,在查看之前先篩選例項。下列各節說明支援的搜尋篩選器。

查看特定類型的例項

你可以使用 kind 值,依發生類型篩選。請參閱可用的種類

以下範例說明如何篩選套件安全漏洞事件:

gcloud

使用 Google Cloud CLI 掃描套件時,不支援篩選發生項目。

API

在 API 查詢中,使用下列篩選運算式:

kind="PACKAGE_VULNERABILITY" AND resourceUrl="RESOURCE_URL"

其中:

  • PACKAGE_VULNERABILITY 是例項的種類
  • RESOURCE_URL 是套件的完整網址,格式為 projects/PROJECT_ID/locations/LOCATION/repositories/REPOSITORY_ID/packages/PACKAGE_ID/PACKAGE_TYPE/VERSION_ID
    • PROJECT_ID 是您的 Google Cloud 控制台專案 ID
    • LOCATION 是存放區的區域或多區域位置
    • REPOSITORY 是儲存映像檔的存放區名稱。
    • PACKAGE_ID 是儲存圖片的套件名稱。
    • PACKAGE_TYPE 是應用程式語言套件類型。 可用的類型包括:pythonPackagesmavenArtifactsnpmPackages
    • VERSION 是套件的版本號碼。

您可以使用 hasPrefix 函式,以更廣泛的範圍進行篩選。

舉例來說,下列篩選器會針對相同套件的多個版本,篩選出特定種類的例項:

kind="NOTE_KIND" AND has_prefix(resourceUrl, "RESOURCE_URL_PREFIX")

其中:

  • RESOURCE_URL_PREFIX:指定資源網址的子字串。
    • 如要篩選套件的所有版本,請省略版本。請使用下列格式:projects/PROJECT_ID/locations/LOCATION/repositories/REPOSITORY_ID
    • 如要篩選專案中的所有套件,請只指定資源位置和專案。請使用下列格式:projects/PROJECT_ID/locations/LOCATION

查看安全漏洞例項

您可以使用 gcloud CLI 或 Artifact Analysis API,查看安全漏洞發生次數的清單。

gcloud

如要擷取套件的安全漏洞例項清單,請使用 --show-package-vulnerability 旗標:

gcloud artifacts versions describe VERSION_ID --repository=REPOSITORY_ID
  --package=GROUP_ID:ARTIFACT_ID --show-package-vulnerability

其中:

  • VERSION 是套件的版本號碼。
  • REPOSITORY 是儲存套件的存放區名稱。
  • GROUP_ID 是套件的群組 ID。
  • ARTIFACT_ID 是套件的構件 ID。

API

在 API 查詢中,使用下列篩選運算式:

GET https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences?filter=kind%3D%22VULNERABILITY%22%20AND%20resourceUrl%3D%22ENCODED_RESOURCE_URL%22

其中:

  • ENCODED_RESOURCE_URL 是圖片的編碼路徑。如要瞭解編碼,請參閱網址編碼

如要進一步瞭解如何使用 API,請參閱 projects.occurrences.get

查看與特定記事相關聯的圖片

您可以擷取與特定註記 ID 相關聯的資源清單。例如,您可以使用特定 CVE 安全漏洞來列出映像檔。

如要列出專案中與特定註記相關聯的的所有映像檔,請使用下列篩選器運算式:

gcloud

使用 Google Cloud CLI 掃描套件時,不支援篩選發生項目。

API

GET https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences?filter=noteProjectId%3D%22goog-vulnz%22%20AND%20resourceUrl%3D%22ENCODED_RESOURCE_URL%22%20AND%20noteId%3D%22NOTE_ID%22

其中:

  • goog-vulnz 是 Artifact Analysis 漏洞分析的 PROVIDER_PROJECT_ID。如果您在自訂專案中使用構件分析,可以將這個值替換為自己的供應商專案 ID。
  • ENCODED_RESOURCE_URL 是圖片的編碼路徑。如要瞭解編碼,請參閱網址編碼
  • NOTE_ID 是註記的 ID。舉例來說,當您在構件分析掃描結果中看到安全性弱點時,通常會使用類似 CVE-2019-12345CVE ID 格式

如要查看特定註記的特定映像檔,請使用下列篩選器運算式:

gcloud

使用 Google Cloud CLI 掃描套件時,不支援篩選發生項目。

API

在 API 查詢中,加入下列篩選運算式:

resourceUrl="RESOURCE_URL" AND noteProjectId="goog-vulnz" \
AND noteId="NOTE_ID"

其中:

  • RESOURCE_URL 是套件的完整網址,格式為 projects/PROJECT_ID/locations/LOCATION/repositories/REPOSITORY_ID/packages/PACKAGE_ID/PACKAGE_TYPE/VERSION_ID
  • goog-vulnz 是 Artifact Analysis 漏洞分析的 PROVIDER_PROJECT_ID
  • NOTE_ID 是註記的 ID。安全性相關註記的格式通常為 CVE-2019-12345

後續步驟