建構應用程式並查看安全性深入分析資訊
本快速入門導覽課程說明如何建構應用程式,並在 Google Cloud 控制台的「安全性深入分析」面板中,查看建構作業的安全洞察資訊。
您將學會以下內容:
- 使用 Cloud Build 建構 Java 應用程式並將其容器化,然後將容器映像檔推送至 Artifact Registry Docker 存放區。
查看建構作業的下列安全性深入分析:
- 軟體構件供應鏈級別 (SLSA):根據 SLSA 規格,識別軟體建構程序的成熟度。
- 建構成果中的安全漏洞。
- 建構構件的軟體物料清單 (SBOM)。
- 建構來源:一組可驗證的建構中繼資料。包括建構的映像檔摘要、輸入來源位置、建構工具鍊、建構步驟和建構時間長度等詳細資料。
事前準備
- 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Artifact Registry, and Container Scanning APIs:
gcloud services enable cloudbuild.googleapis.com
artifactregistry.googleapis.com containerscanning.googleapis.com -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Artifact Registry, and Container Scanning APIs:
gcloud services enable cloudbuild.googleapis.com
artifactregistry.googleapis.com containerscanning.googleapis.com
準備環境
將專案 ID 設為環境變數:
export PROJECT_ID=$(gcloud config get project)
複製包含 Java 程式碼範例的存放區,以便建構及容器化:
git clone https://github.com/googlecloudplatform/software-delivery-shield-demo-java.git cd software-delivery-shield-demo-java/backend
為映像檔建立 Artifact Registry 存放區
在
us-central1
位置中新建名為containers
的 Docker 存放區,並將說明設為「Docker repository」(Docker 存放區):gcloud artifacts repositories create containers \ --repository-format=docker \ --location=us-central1 --description="Docker repository"
確認存放區是否已成功建立:
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
查看安全性深入分析資訊
Google Cloud 控制台中的 Cloud Build UI 包含「安全性深入分析」面板,可顯示建構作業的相關安全性資訊,例如 SLSA 層級、依附元件中的任何安全漏洞,以及建構出處。
如要查看「安全性深入分析」面板,請按照下列步驟操作:
在 Google Cloud 控制台中開啟「建構記錄」頁面:
選取您的專案並按一下 [Open] (開啟)。
在「Region」(區域) 下拉式選單中,選取「us-central1」。
在建構版本表格中,找出您剛執行的建構版本所在資料列。
在「安全性洞察資料」欄下方,按一下「查看」。
建構版本的「安全性深入分析」面板會顯示:
這個面板會顯示下列資訊:
SLSA 等級:這項建構作業已達到 SLSA 第 3 級。按一下「瞭解詳情」連結,即可瞭解這個安全等級的意義。
安全漏洞:在構件中發現的任何安全漏洞。按一下映像檔名稱 (java-guestbook-backend),查看已掃描安全漏洞的構件。
Artifact Registry 中建構的容器映像檔的依附元件。
建構作業詳細資料:建構作業的詳細資料,例如建構者和查看記錄的連結。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請刪除含有這些資源的 Google Cloud 專案。
停用 Container Scanning API:
gcloud services disable containerscanning.googleapis.com --force
刪除 Artifact Registry 存放區:
gcloud artifacts repositories delete containers \ --location=us-central1 --async
您已成功刪除在本快速入門導覽課程中建立的存放區。
後續步驟
- 進一步瞭解 Cloud Build 中的安全性深入分析資訊面板
- 瞭解如何在部署至 Cloud Run 時查看安全性深入分析資訊
- 瞭解如何在部署至 GKE 時查看安全性深入分析資訊
- 進一步瞭解軟體供應鏈安全性