使用 Cloud Run 設定二進位授權政策
本快速入門導覽課程說明如何使用 Cloud Run,在二進位授權政策中設定及測試基本規則。
在本快速入門導覽課程中,您將使用 Binary Authorization 控制 Cloud Run 服務的部署作業。
事前準備
- 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.create
permission. 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.enable
permission. 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.create
permission. 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.enable
permission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
gcloud init
按一下「建立服務」,顯示「建立服務」表單:
在顯示的表單中,執行下列操作:
- 選取 Cloud Run 做為開發平台。
- 選取您要使用服務的地區。
- 指定要為服務命名的名稱,例如
test-service
。 點選「下一步」前往「設定服務的第一個修訂版本」頁面。
在表單中執行下列操作:
- 選取「透過現有的容器映像檔部署單一修訂版本」。
使用
us-docker.pkg.dev/cloudrun/container/hello
做為容器映像檔。展開「進階設定」部分。
按一下 [Security] (安全性) 分頁標籤。
勾選「以二進位授權驗證容器部署」核取方塊:
根據預設,二進位授權政策允許部署所有映像檔。
按一下「下一步」,繼續前往「設定這項服務的觸發方式」頁面:
選取「允許公開存取」,以便在網路瀏覽器中開啟結果。
按一下「建立」,將映像檔部署到 Cloud Run,然後等待部署作業完成。
服務已部署完成,修訂內容須遵守二進位授權政策。
-
按一下「編輯政策」。
在「專案預設規則」中,請注意「允許所有圖片」選項已選取。
前往 Google Cloud 控制台的「Binary Authorization」(二進位授權) 頁面。
按一下「編輯政策」。
在「預設規則」中,選取「禁止所有圖片」。
點選 [儲存政策]。
按一下您在本指南稍早部署的服務名稱。
按一下「編輯及部署新的修訂版本」。
按一下 [Deploy] (部署)。
前往 Google Cloud 控制台的「Binary Authorization」(二進位授權) 頁面。
按一下「編輯政策」。
選取「允許所有圖片」。
如要儲存政策,請按一下「儲存政策」。
在服務清單中找出您要刪除的服務,然後按一下核取方塊來選取。
按一下 [Delete] (刪除),如此便會刪除服務的所有修訂版本。
使用急用權限略過二進位授權強制執行。
使用
built-by-cloud-build
簽署者只部署 Cloud Build 建構的映像檔。使用認證。
使用 Google Cloud 控制台或指令列工具設定二進位授權政策。
建立啟用二進位授權的服務
如要建立啟用二進位授權的 Cloud Run 服務,請按照下列步驟操作:
更新二進位授權政策,禁止所有映像檔
二進位授權政策包含預設規則。這項規則會控管您剛才建立的 Cloud Run 服務部署作業。
根據預設,這項規則允許部署所有容器映像檔。
如要查看預設政策,請按照下列步驟操作:
現在請修改政策,禁止部署所有映像檔,方法如下:
重新部署服務
部署新修訂版本,測試更新後的政策。
如要部署映像檔,請按照下列步驟操作:
您看到類似以下的錯誤訊息:
Service update rejected by Binary Authorization policy: RevisionREVISION
uses unauthorized container image. Container image 'us-docker.pkg.dev/cloudrun/container/hello@SHA
' is not authorized by policy. Denied by an ALWAYS_DENY admission rule
重設政策,允許顯示所有圖片
如要將政策重設為允許顯示所有圖片,請按照下列步驟操作:
您現在可以部署映像檔。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。
如要刪除您在 Cloud Run 中建立的服務,請執行下列操作:
如要停用二進位授權,請參閱「停用二進位授權」。