启用 Binary Authorization for Cloud Run

本指南介绍如何设置 Binary Authorization 以对 Cloud Run 服务和工作强制执行基于政策的部署。

准备工作

执行以下操作,以设置 Cloud Run 并启用 API:

  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.

    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. 安装 Google Cloud CLI。

  5. 配置 gcloud CLI 以使用您的联合身份。

    如需了解详情,请参阅使用联合身份登录 gcloud CLI

  6. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init

在现有 Cloud Run 服务中启用 Binary Authorization

您可以针对现有服务启用 Binary Authorization 强制执行。如需在启用强制执行后将其激活,您可能需要部署一个修订版本或更新服务流量。

您可以使用 控制台 Google Cloud 或 Google Cloud CLI 针对现有服务启用 Binary Authorization 强制执行:

控制台

  1. 前往 Google Cloud 控制台的 Cloud Run 页面。

    转到 Cloud Run

  2. 点击服务。

  3. 点击 Security(安全)标签。

  4. 如需对该服务启用 Binary Authorization 强制执行,请点击启用

  5. 可选:如需配置 Binary Authorization 政策,请点击配置政策

gcloud

针对该服务启用 Binary Authorization 并进行部署:

gcloud run services update SERVICE_NAME --binary-authorization=default

SERVICE_NAME 替换为您的服务的名称。

YAML

  1. 如果您要创建新的服务,请跳过此步骤。如果您要更新现有服务,请下载其 YAML 配置

    gcloud run services describe SERVICE --format export > service.yaml
  2. 按如下所示更新 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
  3. 使用以下命令将服务的配置替换为新配置:

gcloud run services replace service.yaml

针对现有 Cloud Run 作业启用 Binary Authorization

您可以使用 Google Cloud 控制台或 Google Cloud CLI 针对现有作业启用 Binary Authorization 强制执行:

控制台

  1. 前往 Google Cloud 控制台的 Cloud Run 作业页面。

    转到 Cloud Run

  2. 点击作业以打开作业详细信息。

  3. 点击配置标签页。

  4. Binary Authorization 下,从政策列表中选择一个政策。

  5. 点击应用以针对作业启用 Binary Authorization 强制执行。

  6. 可选:如需配置 Binary Authorization 政策,请点击配置政策

gcloud

如需针对作业启用 Binary Authorization,请执行以下命令:

gcloud run jobs update JOB_NAME --binary-authorization=POLICY

请替换以下内容:

  • JOB_NAME:您的作业的名称。
  • POLICY:您要应用的政策。如需使用默认政策,请使用值 default

我们建议您通过配置组织政策来要求 Cloud Run 使用 Binary Authorization。如果未配置此政策,则 Binary Authorization 可能会被 Cloud Run 开发者停用。

查看政策

如需查看政策,请点击查看政策

如需了解详情,请参阅配置 Binary Authorization 政策

服务或作业部署失败

如果您的服务或作业因违反 Binary Authorization 政策而无法部署,您可能会看到如下错误:

Revision REVISION_NAME uses an unauthorized container image.
Container image IMAGE_NAME is not authorized by policy.

此错误还包含有关映像为何违反政策的信息。在这种情况下,您可以使用 Breakglass 绕过政策强制执行并部署映像。