使用 Cloud Run 設定二進位授權政策

本快速入門導覽課程說明如何使用 Cloud Run,設定及測試二進位授權政策中的基本規則。

在本快速入門導覽課程中,您將使用二進位授權控管 Cloud Run 服務的部署作業。

事前準備

  1. 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

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

  3. Enable the Cloud Run, 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

  4. Install the Google Cloud CLI.

  5. Configure the gcloud CLI to use your federated identity.

    For more information, see Sign in to the gcloud CLI with your federated identity.

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

    gcloud init

建立啟用二進位授權的服務

如要建立已啟用二進位授權的 Cloud Run 服務,請按照下列步驟操作:

  1. 前往 Cloud Run。

  2. 在「Deploy a web service」(部署網路服務) 部分,點選「Deploy container」(部署容器)。

    在顯示的表單中指定下列資訊:

    • 「Container image URL」(容器映像檔網址):輸入要部署的容器映像檔網址。例如:us-docker.pkg.dev/cloudrun/container/hello。
    • 服務名稱:輸入服務名稱。例如:test-service。
    • 區域:選取要部署服務的區域。
    • 驗證:選取「允許公開存取」,以便在網路瀏覽器中開啟結果。
  3. 按一下「編輯進階設定」,開啟其他設定。

  4. 按一下 [Security] (安全性) 分頁標籤。

  5. 在「Binary Authorization」(二進位授權) 專區,勾選「Verify container deployment with Binary Authorization」(以二進位授權驗證容器部署) 核取方塊。在下拉式選單中,保留選取的「default」政策。

  6. 按一下「建立」,將映像檔部署至 Cloud Run。等待部署作業完成。

    服務已部署完成。修訂版本須遵守二進位授權政策強制執行。

更新二進位授權政策,禁止所有映像檔

二進位授權政策包含一項預設規則。這項規則會控管您剛才建立的 Cloud Run 服務部署作業。

根據預設,這項規則允許部署所有容器映像檔。

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

  1. 前往「二進位授權」

  2. 點選「Edit policy」。在「Edit policy」(編輯政策) 頁面的「Default rule」(預設規則) 部分,系統會預先選取「Allow all images」(允許所有映像檔) 規則。

  3. 選取「Disallow all images」,修改政策來禁止部署所有映像檔。

  4. 按一下 [Save Policy] (儲存政策)。

重新部署服務

部署新修訂版本,測試更新後的政策。

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

  1. 前往 Cloud Run。

  2. 按一下您稍早依本指南部署的服務名稱。

  3. 點選「重新部署」。

  4. 點選「部署新的修訂版本」。

您會看到類似下列的錯誤訊息:

Service update rejected by Binary Authorization policy: Revision
REVISION 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

將政策重設為允許所有映像檔

如要將政策重設為允許所有映像檔,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Binary Authorization」頁面。

    前往「二進位授權」

  2. 點選「編輯政策」。

  3. 選取「允許所有映像檔」。

  4. 按一下「儲存政策」即可儲存政策。

現在可以部署映像檔了。

清除所用資源

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

如要刪除在 Cloud Run 中建立的服務,請按照下列步驟操作:

  1. 前往 Cloud Run。

  2. 在服務清單中找出您要刪除的服務,然後按一下核取方塊來選取。

  3. 按一下「Delete」(刪除)。這項操作會刪除服務的所有修訂版本。

如要停用二進位授權,請參閱「停用二進位授權」。

後續步驟