自動掃描 Python 套件

瞭解如何啟用自動掃描功能、將容器映像檔推送到 Artifact Registry,以及查看映像檔中發現的安全漏洞清單。

每當映像檔推送至 Artifact Registry 時,Artifact Analysis 就會掃描安全漏洞。平台管理員和應用程式開發人員可以使用這項自動掃描功能,協助找出軟體交付管道中的風險。

本快速入門導覽課程會使用簡單的公開套件,示範其中一種套件掃描類型。根據預設,啟用掃描 API 後,Artifact Analysis 會掃描多種套件類型的安全漏洞。支援的套件類型包括:OS、Go、Java (Maven)、Python 和 Node.js (npm)。

事前準備

  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. Install the Google Cloud CLI.

  3. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  4. 如要初始化 gcloud CLI,請執行下列指令:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  7. Enable the Artifact Registry and Container Scanning APIs:

    gcloud services enable containerscanning.googleapis.com  artifactregistry.googleapis.com
  8. Install the Google Cloud CLI.

  9. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  10. 如要初始化 gcloud CLI,請執行下列指令:

    gcloud init
  11. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  13. Enable the Artifact Registry and Container Scanning APIs:

    gcloud services enable containerscanning.googleapis.com  artifactregistry.googleapis.com
  14. 在 Artifact Registry 中建立 Docker 存放區

    您必須建立 Docker 存放區,以便儲存本快速入門導覽課程所用的範例映像檔。

    1. 在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。

      開啟「存放區」頁面

    2. 點選 [Create Repository] (建立存放區)

    3. 在「Create Repository」(建立存放區) 頁面中,輸入下列設定:

      • Name (名稱):quickstart-docker-repo
      • 格式:Docker
      • 模式:標準
      • 位置類型:區域
      • Region (區域):us-central1
    4. 點選「建立」

    畫面會顯示「Repositories」頁面。您的「quickstart-docker-repo」存放區會新增至存放區清單。

    設定驗證機制

    如要使用 Artifact Registry 推送或提取映像檔,請先設定 Docker,透過 Google Cloud CLI 驗證傳送至 Artifact Registry 的要求。

    在 Cloud Shell 或本機殼層中,為 us-central1 區域的 Docker 存放區設定驗證機制:

    gcloud auth configure-docker us-central1-docker.pkg.dev
    

    取得套件

    1. 切換至要儲存容器映像檔的目錄。

    2. 將圖片複製到本機目錄。舉例來說,您可以使用 TensorFlow。這張映像檔含有 Python,且存在一些安全漏洞。

    本指南僅說明如何掃描圖片及查看結果。不需要事先瞭解 TensorFlow。

    執行下列指令來取得映像檔:

    docker pull amancevice/pandas:1.2.5-jupyter
    

    使用存放區名稱為套件加上標記

    如要將套件推送至 Artifact Registry,您需要設定 docker push 指令,將映像檔推送至特定位置。

    執行下列指令,將映像檔標記為 quickstart-image:tag1

    docker tag amancevice/pandas:1.2.5-jupyter \
    us-central1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1

    其中:

    • PROJECT 是您的 Google Cloud 專案 ID。如果您的專案 ID 包含冒號 (:),請參閱網域範圍專案
    • us-central1 是 Artifact Registry 存放區的區域。
    • docker.pkg.dev 是 Docker 存放區的主機名稱。
    • quickstart-image 是您要用於存放區的映像檔名稱。 可以使用和本機映像檔不同的名稱。
    • tag1 是您要新增至 Docker 映像檔的標記。如果您沒有指定標記,Docker 會套用預設標記 latest

    將映像檔推送至 Artifact Registry

    將新的 Python 套件上傳至 Artifact Registry 時,構件分析會自動掃描容器映像檔中的這些套件。

    如要將映像檔推送至 Artifact Registry 中的 Docker 存放區,請執行下列指令:

    docker push us-central1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1

    PROJECT 替換為 Google Cloud專案 ID。如果您的專案 ID 包含冒號 (:),請參閱網域範圍專案

    查看安全漏洞掃描結果

    如要查看映像檔中的安全漏洞:

    1. 取得存放區清單。

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

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

    3. 在圖片清單中,按一下圖片名稱。

      每個映像檔摘要的安全漏洞總數會顯示在「Vulnerabilities」(安全漏洞) 資料欄中。

      發生安全漏洞的映像檔螢幕擷取畫面

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

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

      螢幕截圖:「掃描結果」部分顯示安全漏洞、修正和有效嚴重程度

      安全漏洞表格會列出每個安全漏洞的「常見安全漏洞與資料外洩風險 (CVE)」名稱、實際嚴重程度、「常見安全漏洞評分系統 (CVSS)」分數、修正 (如有)、內含安全漏洞的套件名稱,以及套件類型。

      您可以篩選及排序這些檔案,依檔案副檔名檢查特定檔案、目錄或檔案類型。

      Google Cloud 控制台最多會在表格中顯示 1200 個安全漏洞。如果映像檔有超過 1200 個安全漏洞,您必須使用 gcloud 或 API 查看完整清單。

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

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

    查看結果的其他方式

    控制台最多會顯示 1200 個安全漏洞。 Google Cloud 如果映像檔有超過 1200 個安全漏洞,您必須使用 gcloud 或 API 查看完整清單。

    清除所用資源

    如要避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請刪除含有這些資源的 Google Cloud 專案。

    如果您是為了本指南建立新專案,現在可以刪除該專案。

      Delete a Google Cloud project:

      gcloud projects delete PROJECT_ID

    後續步驟