Configure a Binary Authorization policy with Cloud Run

This quickstart shows how to configure and test a basic rule in a Binary Authorization policy with Cloud Run.

In this quickstart, you use Binary Authorization to control deployment of a Cloud Run service.

Before you begin

  1. 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.
  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. Verify that billing is enabled for your Google Cloud project.

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

  5. Install the Google Cloud CLI.

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

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

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

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

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

  11. Install the Google Cloud CLI.

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

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

    gcloud init

Create a service with Binary Authorization enabled

To create a Cloud Run service with Binary Authorization enabled, do the following:

  1. Go to Cloud Run

  2. In the Deploy a web service section, Deploy container.

    In the form that displays, specify the following information:

    • Container image URL: Enter the URL for the container image you want to deploy. For example, us-docker.pkg.dev/cloudrun/container/hello.
    • Service name: Enter the name you want to give to your service. For example, test-service.
    • Region: Select the region where you want to deploy your service.
    • Authentication: Select Allow public access so that you can open the result in your web browser.
  3. Click Edit advanced configuration to open additional settings.

  4. Click the Security tab.

  5. In the Binary Authorization section, select the Verify container deployment with Binary Authorization checkbox. Leave the default policy selected in the drop-down list.

  6. Click Create to deploy the image to Cloud Run. Wait for the deployment to finish.

    Your service is deployed. Revisions are subject to Binary Authorization policy enforcement.

Update the Binary Authorization policy to disallow all images

The Binary Authorization policy contains a default rule. This rule governs the deployment of the Cloud Run service you just created.

By default, the rule allows all container images to be deployed.

To edit the default policy, do the following:

  1. Go to Binary Authorization

  2. Click Edit policy. On the Edit policy page, the rule Allow all images is preselected in the Default rule section.

  3. Modify the policy to block all images from being deployed, by selecting Disallow all images.

  4. Click Save policy.

Redeploy the service

Test the updated policy by deploying a new revision.

To deploy the image, do the following:

  1. Go to Cloud Run

  2. Click the name of the service you deployed earlier in this guide.

  3. Click Redeploy.

  4. Click Deploy new revision.

You see an error message similar to the following:

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

Reset the policy to allow all images

To reset the policy to allow all images, do the following:

  1. Go to the Binary Authorization page in the Google Cloud console.

    Go to Binary Authorization

  2. Click Edit Policy.

  3. Select Allow All Images.

  4. To save the policy, click Save Policy.

You can now deploy images.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

To delete the service you created in Cloud Run, do the following:

  1. Go to Cloud Run

  2. Locate the service you want to delete in the services list, and click its checkbox to select it.

  3. Click Delete. This deletes all revisions of the service.

To disable Binary Authorization, see Disabling Binary Authorization.

What's next