本指南說明如何設定 Binary Authorization,以強制執行 Cloud Run 服務和工作的政策式部署作業。
事前準備
設定 Cloud Run 並啟用 API,方法如下:
- 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.
- 
    
    
      
        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
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Cloud Run, Artifact Registry, Binary Authorization APIs. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
      Install the Google Cloud CLI. 
- 
          如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。 
- 
        如要初始化 gcloud CLI,請執行下列指令: gcloud init
- 
    
    
      
        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
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Cloud Run, Artifact Registry, Binary Authorization APIs. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
      Install the Google Cloud CLI. 
- 
          如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。 
- 
        如要初始化 gcloud CLI,請執行下列指令: gcloud init
- 前往 Google Cloud 控制台的 Cloud Run 頁面。 
- 按一下所需服務。 
- 按一下 [Security] (安全性) 分頁標籤。 
- 如要啟用服務的二進位授權強制執行功能,請按一下「啟用」。 
- 選用:如要設定二進位授權政策,請按一下「設定政策」。 
- 如要建立新服務,請略過這個步驟。 如要更新現有服務,請下載其 YAML 設定: - gcloud run services describe SERVICE --format export > service.yaml 
- 將 - run.googleapis.com/binary-authorization:註解更新為以下內容:- apiVersion: serving.knative.dev/v1 kind: Service metadata: annotations: run.googleapis.com/binary-authorization: POLICY name: SERVICE spec: template: - 更改下列內容: - SERVICE:Cloud Run 的名稱
- POLICY:設為 default
 
- 使用下列指令,以新設定取代服務: 
- 前往 Google Cloud 控制台的 Cloud Run 工作頁面。 
- 按一下工作即可開啟工作詳細資料。 
- 按一下 [設定] 標籤。 
- 在「二進位授權」下方,從政策清單中選取政策。 
- 按一下「套用」,即可對工作啟用二進位授權強制執行功能。 
- 選用:如要設定二進位授權政策,請按一下「設定政策」。 
- JOB_NAME:工作名稱。
- POLICY:要套用的政策。如要使用預設政策,請使用- default值。
- 前往 Cloud Run 頁面: 
- 按一下「Create service」(建立服務)。 
- 在「建立服務」表單中: - 選取 Cloud Run 做為開發平台。
- 選取您要使用服務的地區。
- 輸入服務名稱。
- 點選「下一步」,繼續前往「設定服務的第一個修訂版本」頁面。
- 選取「透過現有的容器映像檔部署單一修訂版本」。
- 輸入或選取要部署的映像檔。
- 展開「進階設定」部分。
- 按一下 [Security] (安全性) 分頁標籤。
- 選取「以二進位授權驗證容器部署」核取方塊。 
- 選用:按一下「設定政策」,設定二進位授權政策。如要進一步瞭解如何設定政策,請參閱「設定政策」 
- 部署服務。 
 
- SERVICE_NAME:服務名稱。
- IMAGE_URL:要部署的映像檔。
- REGION:要部署服務的區域。
- 建立啟用二進位授權的新工作: - gcloud run jobs create JOB_NAME \ --image IMAGE_URL OPTIONS \ --binary-authorization=POLICY \ --region=REGION - 更改下列內容: 
- 等待工作建立完成。成功完成後,控制台會顯示成功訊息。 
- 建立新的 - job.yaml檔案,並加入以下內容:- apiVersion: run.googleapis.com/v1 kind: Job metadata: name: JOB annotations: run.googleapis.com/binary-authorization: POLICY spec: template: spec: containers: - image: IMAGE - 更改下列內容: - JOB:Cloud Run 工作的名稱
- IMAGE:容器映像檔的網址
- POLICY:設為 default
 
- 使用下列指令部署新工作: - gcloud run jobs replace job.yaml 
- 使用 built-by-cloud-build簽署者只部署 Cloud Build 建構的映像檔。
- 使用Google Cloud 控制台或指令列工具設定二進位授權政策。
- 使用認證,只部署已簽署的容器映像檔。
- 觀看影片,瞭解如何在 Cloud Run 中使用二進位授權,確保只部署核准的建構作業。
在現有 Cloud Run 服務上啟用二進位授權
您可以在現有服務上啟用二進位授權強制執行功能。 啟用強制執行後,您可能需要部署修訂版本或更新服務流量,才能啟動強制執行。
您可以使用 Google Cloud 控制台或 Google Cloud CLI,在現有服務上啟用二進位授權強制執行功能:
主控台
gcloud
在服務上啟用二進位授權並部署:
gcloud run services update SERVICE_NAME --binary-authorization=default
將 SERVICE_NAME 改為服務名稱。
YAML
gcloud run services replace service.yaml
在現有 Cloud Run 工作上啟用二進位授權
您可以使用Google Cloud 控制台或 Google Cloud CLI,在現有工作上啟用二進位授權強制執行功能:
主控台
gcloud
在工作中啟用二進位授權:
gcloud run jobs update JOB_NAME --binary-authorization=POLICY
更改下列內容:
建議您為 Cloud Run 啟用二進位授權,方法是設定組織政策。如果未設定政策,Cloud Run 開發人員可以停用二進位授權。
查看政策
如要查看政策,請按一下「查看政策」。
詳情請參閱設定二進位授權政策。
服務或工作部署失敗
如果服務或工作因違反二進位授權政策而無法部署,您可能會看到類似下列內容的錯誤訊息:
Revision REVISION_NAME uses an unauthorized container image. Container image IMAGE_NAME is not authorized by policy.
錯誤訊息也會說明圖片違反政策的原因。在這種情況下,您可以使用急用權限略過政策強制執行,並部署映像檔。
在新服務上啟用二進位授權
您可以使用 Google Cloud 控制台 或 Google Cloud CLI,在新服務上啟用二進位授權:
主控台
gcloud
在服務上啟用二進位授權並部署:
  gcloud run deploy SERVICE_NAME --image=IMAGE_URL --binary-authorization=default --region=REGION
更改下列內容:
在新工作中啟用二進位授權
您可以使用 Google Cloud CLI,在新工作中啟用二進位授權:
gcloud
建立新工作時,Cloud Run 服務代理程式必須能夠存取容器 (預設情況下可以)。