建構應用程式並查看安全性深入分析資訊

本快速入門導覽課程說明如何建構應用程式,並在 Google Cloud 控制台的「安全性深入分析」面板中,查看建構作業的安全洞察資訊。

您將學會以下內容:

  • 使用 Cloud Build 建構 Java 應用程式並將其容器化,然後將容器映像檔推送至 Artifact Registry Docker 存放區。
  • 查看建構作業的下列安全性深入分析:

    • 軟體構件供應鏈級別 (SLSA),可根據 SLSA 規格,判斷軟體建構程序的成熟度。
    • 建構成果中的安全漏洞
    • 建構構件的軟體物料清單 (SBOM)
    • 建構來源:一組可驗證的建構中繼資料。包括建構的映像檔摘要、輸入來源位置、建構工具鍊、建構步驟和建構時間長度等詳細資料。

事前準備

  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.

    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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • 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 Cloud Build, 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.

    gcloud services enable cloudbuild.googleapis.com  artifactregistry.googleapis.com  containerscanning.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.

    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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • 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 Cloud Build, 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.

    gcloud services enable cloudbuild.googleapis.com  artifactregistry.googleapis.com  containerscanning.googleapis.com
  14. 準備環境

    1. 將專案 ID 設為環境變數:

      export PROJECT_ID=$(gcloud config get project)
      
    2. 複製包含 Java 程式碼範例的存放區,以便建構及容器化:

      git clone https://github.com/googlecloudplatform/software-delivery-shield-demo-java.git
      cd software-delivery-shield-demo-java/backend
      

    為映像檔建立 Artifact Registry 存放區

    1. us-central1 位置中新建名為 containers 的 Docker 存放區,並將說明設為「Docker repository」(Docker 存放區):

      gcloud artifacts repositories create containers \
          --repository-format=docker \
          --location=us-central1 --description="Docker repository"
      
    2. 確認存放區是否已成功建立:

      gcloud artifacts repositories list
      

      顯示的存放區清單中會列出 containers

    建構應用程式

    使用 Cloud Build 建構 Java 應用程式並容器化。 下列指令會建構 Java 應用程式並將其容器化,然後將建構的容器儲存在 Artifact Registry Docker 存放區中:

    gcloud builds submit --config=cloudbuild.yaml --region=us-central1
    

    建構完成後,您會看到類似以下的成功狀態訊息:

    <pre class="none lang-sh">
    DONE
    -----------------------------------------------------------------------------
    ID: 3e08565f-7f57-4449-bc68-51c46cf33d03
    CREATE_TIME: 2022-09-19T15:41:07+00:00
    DURATION: 54S
    SOURCE: gs://sds-docs-project_cloudbuild/source/1663602066.777581-6ebe4b2d6fd741ffa18936d7f78055e9.tgz
    IMAGES: us-central1-docker.pkg.dev/sds-docs-project/containers/java-guestbook-backend:quickstart
    STATUS: SUCCESS
    </pre>
    

    為建構的映像檔產生 SBOM

    SBOM 是應用程式的完整清單,可識別軟體所依附的套件。內容可包括供應商的第三方軟體、內部構件和開放原始碼程式庫。

    為您在上節中建構的映像檔產生 SBOM:

    gcloud artifacts sbom export \
        --uri=us-central1-docker.pkg.dev/${PROJECT_ID}/containers/java-guestbook-backend:quickstart
    

    查看安全性深入分析資訊

    控制台的 Cloud Build UI 包含「安全洞察」 Google Cloud 面板,可顯示建構作業的相關安全資訊,例如 SLSA 層級、依附元件中的任何安全漏洞,以及建構出處。

    如要查看「安全性深入分析」面板,請按照下列步驟操作:

    1. 在 Google Cloud 控制台中開啟「建構記錄」頁面:

      開啟「建構記錄」頁面

    2. 選取您的專案並按一下 [Open] (開啟)

    3. 在「Region」(區域) 下拉式選單中,選取「us-central1」

    4. 在建構版本表格中,找出您剛執行的建構版本所在資料列。

    5. 在「安全性洞察資料」欄下方,按一下「查看」

    建構版本的「安全性深入分析」面板會顯示:

    這個面板會顯示下列資訊:

    • SLSA 級別:這個建構作業已達到 SLSA 第 3 級。按一下「瞭解詳情」連結,即可瞭解這個安全等級的意義。

    • 安全漏洞:在構件中發現的任何安全漏洞。按一下映像檔名稱 (java-guestbook-backend),查看已掃描安全漏洞的構件。

    • Artifact Registry 中建構的容器映像檔依附元件

    • 建構作業詳細資料:建構作業的詳細資料,例如建構工具和查看記錄的連結。

    清除所用資源

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

    1. 停用 Container Scanning API:

      gcloud services disable containerscanning.googleapis.com --force
      
    2. 刪除 Artifact Registry 存放區:

      gcloud artifacts repositories delete containers \
          --location=us-central1 --async
      

      您已成功刪除在本快速入門導覽課程中建立的存放區。

    後續步驟