為 GKE 設定二進位授權政策

本快速入門指南說明如何在二進位授權政策中設定及測試基本規則。

在本快速入門導覽課程中,您將查看及設定政策中的預設規則,預設規則允許部署所有映像檔。您會在 Google Kubernetes Engine (GKE) 叢集部署容器映像檔,藉此測試這項功能。接著,您會將預設規則設為禁止部署所有映像檔,並嘗試部署映像檔。

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自體驗產品的實際應用成效。新客戶還能獲得價值 $300 美元的免費抵免額,能用於執行、測試及部署工作負載。
  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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  5. Enable the Artifact Registry, Binary Authorization APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  6. Install the Google Cloud CLI.

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

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

    gcloud init
  9. 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

  10. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  12. Enable the Artifact Registry, Binary Authorization APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  13. Install the Google Cloud CLI.

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

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

    gcloud init
  16. 安裝 kubectl。

必要的角色

如要取得使用 GKE 設定二進位授權政策所需的權限,請要求管理員在專案中授予您下列 IAM 角色:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

建立已啟用二進位授權強制執行功能的叢集

現在,您要建立啟用二進位授權的 GKE 叢集。這是要執行已部署容器映像檔的叢集。

二進位授權適用於 Autopilot 或 Standard 叢集。

Google Cloud 控制台

下列步驟會設定 Autopilot 叢集。

  1. 在 Google Cloud 控制台中,前往 GKE「Kubernetes clusters」(Kubernetes 叢集) 頁面:

    前往 GKE

  2. 點選「建立」。

  3. 在「建立 Autopilot 叢集」中,執行下列步驟:

    1. 在「Name」(名稱) 欄位中,輸入 test-cluster。

    2. 在「Region」(區域) 選單中,選取「us-central1」。

    3. 展開「進階設定」部分。

    4. 按一下「Security」連結,顯示「Security」面板。

    5. 在「Security」(安全性) 面板中,勾選「Enable Binary Authorization」(啟用二進位授權) 核取方塊。

    6. 選取「僅強制執行」。

    7. 依序點選「下一步」和「下一步:檢查並建立」。

    8. 如要開始建立叢集,請按一下「建立」。

gcloud

執行 gcloud container clusters create 並啟用 --binauthz-evaluation-mode=PROJECT_SINGLETON_POLICY_ENFORCE 旗標。

gcloud container clusters create \
    --binauthz-evaluation-mode=PROJECT_SINGLETON_POLICY_ENFORCE \
    --zone us-central1-a \
    test-cluster

建立叢集可能需要幾分鐘的時間。

預設政策

根據預設,二進位授權政策會允許部署所有容器映像檔。

Google Cloud 控制台

如要查看預設政策,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「二進位授權」頁面。

    前往二進位授權

    控制台會顯示政策的詳細資料。

  2. 點選「編輯政策」。

  3. 在「專案預設規則」中,選取「允許所有映像檔」選項。

gcloud

如要查看預設政策,請匯出政策 YAML 檔案,如下所示:

gcloud container binauthz policy export

根據預設,檔案包含下列內容:

globalPolicyEvaluationMode: ENABLE
defaultAdmissionRule:
  evaluationMode: ALWAYS_ALLOW
  enforcementMode: ENFORCED_BLOCK_AND_AUDIT_LOG
name: projects/PROJECT_ID/policy

REST API

如要查看預設政策,請按照下列步驟以 JSON 格式擷取:

curl \
    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    -H "x-goog-user-project: ${PROJECT_ID}" \
    "https://binaryauthorization.googleapis.com/v1/projects/${PROJECT_ID}/policy"

這個指令會產生下列輸出內容:

{
  "name": "projects/PROJECT_ID/policy",
  "globalPolicyEvaluationMode": "ENABLE",
  "defaultAdmissionRule": {
    "evaluationMode": "ALWAYS_ALLOW",
    "enforcementMode": "ENFORCED_BLOCK_AND_AUDIT_LOG"
  }
}

測試違規處置政策

您可以嘗試將範例容器映像檔部署至叢集,測試強制執行政策。

在本快速入門導覽課程中,您將使用 Artifact Registry 中路徑 us-docker.pkg.dev/google-samples/containers/gke/hello-app 的範例容器映像檔。這是 Google 建立的公開容器映像檔,內含「Hello, World!」範例應用程式。

Google Cloud 控制台

如要測試政策,請按照下列步驟操作:

  1. 前往Google Cloud 控制台的 GKE「Clusters」(叢集) 頁面。

    前往 GKE

  2. 點選「Deploy」(部署)。

    控制台會提示您輸入部署作業的詳細資料。

  3. 在「Deployment name」(部署作業名稱) 欄位中輸入 hello-server。

  4. 按一下「下一步:容器詳細資料」。

  5. 選取「現有容器映像檔」。

  6. 輸入 us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 做為容器映像檔路徑。

  7. 點選「Deploy」(部署)。

kubectl

如要測試政策,請按照下列步驟操作:

  1. 更新本機 kubeconfig 檔案:

    gcloud container clusters get-credentials \
        --zone us-central1-a \
        test-cluster
    

    這會提供在 GKE 中存取叢集所需的憑證和端點資訊。

  2. 部署映像檔:

    kubectl run hello-server --image us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 --port 8080
    

現在,請確認二進位授權是否允許部署作業。

Google Cloud 控制台

如要確認映像檔是否已部署,請前往 Google Cloud 控制台的 GKE「Workloads」(工作負載) 頁面。

前往 GKE

部署作業的工作負載會顯示綠色圖示,表示映像檔已成功部署。

kubectl

如要確認映像檔已部署,請按照下列步驟操作:

kubectl get pods

這個指令會輸出類似以下的訊息,表示部署成功:

NAME                            READY     STATUS    RESTARTS   AGE
hello-server-579859fb5b-h2k8s   1/1       Running   0          1m

請務必刪除部署作業,才能繼續進行下一個步驟:

Google Cloud 控制台

如要刪除部署作業,請按照下列步驟操作:

  1. 返回Google Cloud 控制台的 GKE「Workloads」(工作負載) 頁面。

    前往 GKE

  2. 選取 hello-server 工作負載。

  3. 點選「刪除」。

kubectl

如要刪除部署作業,請按照下列步驟操作:

kubectl delete deployment hello-server

設定違規處置政策,禁止顯示所有映像檔

現在請修改政策,封鎖所有映像檔,不允許部署。

Google Cloud 控制台

如要修改政策,請按照下列步驟操作:

  1. 返回 Google Cloud 控制台的「Binary Authorization」(二進位授權) 頁面。

    前往二進位授權

  2. 點選「編輯政策」。

  3. 選取「禁止所有圖片」。

  4. 點選 [儲存政策]。

gcloud

如要修改政策,請按照下列步驟操作:

  1. 匯出政策 YAML 檔案:

    gcloud container binauthz policy export  > /tmp/policy.yaml
    
  2. 在文字編輯器中,將 evaluationMode 從 ALWAYS_ALLOW 變更為 ALWAYS_DENY。

    政策 YAML 檔案應如下所示:

    globalPolicyEvaluationMode: ENABLE
    defaultAdmissionRule:
      evaluationMode: ALWAYS_DENY
      enforcementMode: ENFORCED_BLOCK_AND_AUDIT_LOG
    name: projects/PROJECT_ID/policy
    
  3. 將政策 YAML 檔案匯回二進位授權:

    gcloud container binauthz policy import /tmp/policy.yaml
    

REST API

如要修改政策,請按照下列步驟操作:

  1. 建立文字檔案,並以 JSON 格式輸入更新後的政策:

    cat > /tmp/policy.json << EOM
    {
      "name": "projects/${PROJECT_ID}/policy",
      "globalPolicyEvaluationMode": "ENABLE",
      "defaultAdmissionRule": {
        "evaluationMode": "ALWAYS_DENY",
        "enforcementMode": "ENFORCED_BLOCK_AND_AUDIT_LOG"
      }
    }
    EOM
    
  2. 將更新後的政策傳送至 REST API:

    curl -X PUT \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
        -H "x-goog-user-project: ${PROJECT_ID}" \
        --data-binary @/tmp/policy.json  \
        "https://binaryauthorization.googleapis.com/v1/projects/${PROJECT_ID}/policy"
    

重新測試政策

再次測試政策,將範例容器映像檔部署至叢集。這次,二進位授權會禁止部署映像檔。

Google Cloud 控制台

部署映像檔:

  1. 前往Google Cloud 控制台的 GKE「Clusters」(叢集) 頁面。

    前往 GKE

  2. 點選「Deploy」(部署)。

    控制台會提示您輸入部署作業的詳細資料。

  3. 在「Deployment name」(部署作業名稱) 欄位中輸入 hello-server。

  4. 按一下「下一步:容器詳細資料」。

  5. 選取「現有容器映像檔」。

  6. 輸入 us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 做為容器映像檔路徑。

  7. 點選「Deploy」(部署)。

kubectl

部署映像檔:

kubectl run hello-server --image us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 --port 8080

現在可以確認政策是否遭到封鎖:

Google Cloud 控制台

如要確認映像檔未部署,請執行下列操作:

返回Google Cloud 控制台的 GKE「Workloads」(工作負載) 頁面。

前往 GKE

容器映像檔的工作負載會顯示紅色圖示,表示映像檔部署失敗。

kubectl

如要確認映像檔未部署,請執行下列指令:

kubectl get pods

指令會列印下列訊息,表示映像檔未部署:

No resources found.

您可以進一步瞭解部署作業:

kubectl get event --template \
'{{range.items}}{{"\033[0;36m"}}{{.reason}}:{{"\033[0m"}}{{.message}}{{"\n"}}{{end}}'

您會看到類似下列內容的回覆:

FailedCreate: Error creating: pods POD_NAME is forbidden: admission webhook "imagepolicywebhook.image-policy.k8s.io" denied the request: Image IMAGE_NAME denied by Binary Authorization default admission rule. Denied by always_deny admission rule

輸出內容如下:

  • POD_NAME:Pod 的名稱。
  • IMAGE_NAME:映文件名稱。
  • ATTESTOR_NAME:驗證者名稱。

清除所用資源

為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。

刪除在 GKE 建立的叢集:

控制台

如要刪除叢集,請按照下列步驟操作:

  1. 前往Google Cloud 控制台的 GKE「Clusters」(叢集) 頁面。

    前往 GKE

  2. 選取 test-cluster 叢集,然後按一下「刪除」。

gcloud

如要刪除叢集,請按照下列步驟操作:

gcloud container clusters delete \
    --zone=us-central1-a \
    test-cluster

後續步驟