將應用程式部署至 GKE 並查看安全性深入分析資訊

瞭解如何將刻意設計的易受攻擊容器部署至 GKE 叢集,並在安全防護機制資訊主頁中取得安全漏洞的安全性深入分析資訊。GKE 安全防護機制資訊主頁會顯示已知作業系統安全漏洞的相關資訊。如要掃描 Go 或 Java 套件中的語言安全漏洞,請參閱建構應用程式並查看安全性深入分析

目標

  • 使用 Cloud Build 建構容器化應用程式,並推送至 Artifact Registry。
  • 在 Cloud Deploy 中建立推送管道。
  • 將應用程式部署至預先發布的 GKE 叢集,並升級至正式版叢集。
  • 使用 Google Cloud 控制台中的安全狀況資訊主頁,查看已部署應用程式的安全性弱點洞察資料。

事前準備

  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. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

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

    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. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Artifact Registry, Cloud Build, Cloud Deploy, Google Kubernetes Engine, Container Security, and Container Analysis APIs:

    gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com clouddeploy.googleapis.com container.googleapis.com containersecurity.googleapis.com  containeranalysis.googleapis.com
  8. Install the Google Cloud CLI.

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

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

    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. Make sure that billing is enabled for your Google Cloud project.

  13. Enable the Artifact Registry, Cloud Build, Cloud Deploy, Google Kubernetes Engine, Container Security, and Container Analysis APIs:

    gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com clouddeploy.googleapis.com container.googleapis.com containersecurity.googleapis.com  containeranalysis.googleapis.com
  14. 完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。

準備環境

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

    export PROJECT_ID=$(gcloud config get project)
    
  2. 設定 Cloud Deploy 的預設 Google Cloud 區域:

    gcloud config set deploy/region us-central1
    
  3. 複製包含這項工作範例程式碼的 GitHub 存放區:

    git clone https://github.com/googlecloudplatform/software-delivery-shield-demo-java.git
    cd ~/software-delivery-shield-demo-java/backend
    
  4. 為 GKE 建立 IAM 服務帳戶

    要使用的叢集:

    gcloud iam service-accounts create sds-runtime \
        --display-name="Security insights with GKE service account"
    
  5. 將權限授予 IAM 服務帳戶:

    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member=serviceAccount:sds-runtime@${PROJECT_ID}.iam.gserviceaccount.com \
        --role="roles/container.nodeServiceAccount"
    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member=serviceAccount:sds-runtime@${PROJECT_ID}.iam.gserviceaccount.com \
        --role="roles/clouddeploy.jobRunner"
    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member=serviceAccount:sds-runtime@${PROJECT_ID}.iam.gserviceaccount.com \
        --role="roles/container.developer"
    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member=serviceAccount:sds-runtime@${PROJECT_ID}.iam.gserviceaccount.com \
        --role="roles/artifactregistry.reader"
    
  6. 授予預設的 Compute Engine 服務帳戶 GKE 叢集存取權。Cloud Deploy 會使用這項存取權,將應用程式部署至推送管道中的叢集。

    PROJECT_NUMBER="$(gcloud projects describe ${PROJECT_ID} --format='get(projectNumber)')"
    gcloud projects add-iam-policy-binding ${PROJECT_NUMBER} \
        --member=serviceAccount:${PROJECT_NUMBER}-compute@developer.gserviceaccount.com \
        --role=roles/container.developer
    

為映像檔建立 Artifact Registry 存放區

  1. 建立存放區:

    gcloud artifacts repositories create containers \
        --repository-format=docker \
        --location=us-central1 \
        --description="Security insights with GKE repository"
    
  2. 確認存放區是否存在:

    gcloud artifacts repositories list \
        --location=us-central1 \
        --filter="REPOSITORY:containers"
    

    輸出內容會顯示您建立的 containers 存放區。

建立 GKE 叢集

建立兩個 GKE 叢集,分別是名為 dev-cluster 的暫存叢集,以及名為 prod-cluster 的正式叢集。在 Autopilot 中,如果新叢集執行 1.27 以上版本,系統會自動啟用工作負載安全漏洞掃描功能。如果您使用 Standard 叢集,請指定 --workload-vulnerability-scanning=standard 旗標。

gcloud container clusters create-auto dev-cluster \
    --region=us-central1 \
    --release-channel=rapid \
    --service-account=sds-runtime@${PROJECT_ID}.iam.gserviceaccount.com
gcloud container clusters create-auto prod-cluster \
    --region=us-central1 \
    --release-channel=rapid \
    --service-account=sds-runtime@${PROJECT_ID}.iam.gserviceaccount.com

叢集建立程序最多可能需要五分鐘。您也可以更新現有 GKE 叢集,啟用工作負載安全漏洞掃描功能。

建構映像檔

使用 Cloud Build 建構及提交映像檔:

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

建構完成時,輸出內容會類似以下內容:

DONE
-----------------------------------------------------------------------------
ID: 3e23094f-7f57-4449-bc68-51c37hn34d03
CREATE_TIME: 2022-09-19T15:41:07+00:00
DURATION: 54S
SOURCE: gs://my-project_cloudbuild/source/1663602066.777581-6ebe4b2d6fd741ffa18936d7f.tgz
IMAGES: us-central1-docker.pkg.dev/PROJECT_ID/containers/java-guestbook-backend:quickstart
STATUS: SUCCESS

使用 Cloud Deploy 將映像檔部署至 GKE

  1. 使用專案 ID 更新 Cloud Deploy 設定檔:

    sed -i "s/PROJECT_ID/${PROJECT_ID}/g" clouddeploy.yaml
    
  2. 註冊管道和目標:

    gcloud deploy apply --file=clouddeploy.yaml
    
  3. 如要確認管道是否存在,請前往 Google Cloud 控制台的「Delivery pipelines」(推送管道) 頁面:

    前往推送管道

    管道清單會顯示新管道 guestbook-app-delivery

  4. 按一下管道名稱即可監控進度。「Delivery pipeline details」(交付管道詳細資料) 頁面隨即開啟。

  5. 在 Cloud Deploy 中建立新版本:

    gcloud deploy releases create guestbook-release-001 \
        --delivery-pipeline=guestbook-app-delivery \
        --images=java-guestbook-backend=us-central1-docker.pkg.dev/${PROJECT_ID}/containers/java-guestbook-backend:quickstart
    

    新發行內容會顯示在「Delivery pipeline details」(交付管道詳細資料) 頁面的「Releases」(發行內容) 專區。

  6. 在「推送管道詳細資料」頁面中,監控「管道視覺化」檢視畫面,直到 dev-cluster 顯示「升級」按鈕為止。你可能需要重新整理頁面。

  7. 按一下 dev-cluster 視覺化效果中的「升級」

  8. 在「升級版本」窗格中,按一下「升級」,確認要將版本升級至正式版叢集。

  9. 如要確認發布作業是否成功,請查看「版本」部分。 「最近一次的推出作業狀態」欄會顯示 Successfully deployed to prod-cluster

檢視安全漏洞

在本節中,您可以使用安全狀態資訊主頁,查看作業系統安全漏洞洞察資料。將工作負載部署至叢集後,資訊主頁會顯示執行中工作負載的安全性弱點資訊。

  1. 前往 Google Cloud 控制台的「GKE security posture」(GKE 安全防護機制) 頁面。

    前往 GKE 安全防護機制

  2. 如要查看掃描結果,請重新整理頁面。初始掃描最多可能需要 15 分鐘才能完成。

  3. 在「GKE security posture」(GKE 安全防護機制) 頁面中,查看「Workload OS vulnerability」(工作負載 OS 安全漏洞) 區段。這個部分會列出影響已部署工作負載的頂尖 CVE。

  4. 如要查看詳細資料,請按一下「查看所有安全漏洞問題」。系統會開啟「疑慮」分頁,並套用「安全漏洞」疑慮類型的篩選條件。表格會顯示各個安全漏洞的概況及其影響。

  5. 如要查看特定安全漏洞的詳細資料,請按一下表格中的問題名稱。「Vulnerability」(安全漏洞) 窗格隨即開啟。您可以在這個窗格中執行下列操作:

    • 詳閱 CVE 的詳細說明,包括受影響的版本、套件和 CVSS 分數。
    • 查看建議採取的行動,例如說明文件和修補程式版本資訊,以解決問題。
    • 在「受影響的工作負載」分頁中,查看受安全漏洞影響的特定工作負載。

清除所用資源

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

刪除個別資源

  1. 刪除 Cloud Deploy 管道:

    gcloud deploy delivery-pipelines delete guestbook-app-delivery --force
    
  2. 刪除 GKE 叢集:

    gcloud container clusters delete dev-cluster \
        --region=us-central1
    gcloud container clusters delete prod-cluster \
        --region=us-central1
    
  3. 刪除 Artifact Registry 存放區:

    gcloud artifacts repositories delete containers \
        --location=us-central1
    
  4. 刪除 IAM 服務帳戶:

    gcloud iam service-accounts delete sds-runtime@${PROJECT_ID}.iam.gserviceaccount.com
    

刪除專案

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

後續步驟