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
- 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Run, Artifact Registry, Binary Authorization APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Run, Artifact Registry, Binary Authorization APIs.
- Install the Google Cloud CLI.
-
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:
Click Create service to display the Create service form:
In the form that displays, do the following:
- Select Cloud Run as your development platform.
- Select the region where you want your service located.
- Specify the name you want to give to your service—for example,
test-service
. Click Next to continue to the Configure the service's first revision page.
In the form, do the following:
- Select Deploy one revision from an existing container image.
Use
us-docker.pkg.dev/cloudrun/container/hello
as the container image.Expand the Advanced settings section.
Click the Security tab.
Select the Verify container deployment with Binary Authorization checkbox:
By default, the Binary Authorization policy allows all images to be deployed.
Click Next to continue to the Configure how this service is triggered page:
Select Allow unauthenticated invocations to be able to open the result in your web browser
Click Create to deploy the image to Cloud Run and 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 view the default policy, do the following:
-
Click Edit Policy.
In Project Default Rule, note that the option Allow All Images is selected.
Now, modify the policy to block all images from being deployed, by doing the following:
Go to the Binary Authorization page in the Google Cloud console.
Click Edit Policy.
In Default rule, select Disallow All Images.
Click Save Policy.
Redeploy the service
Test the updated policy by deploying a new revision.
To deploy the image, do the following:
Click the name of the service you deployed earlier in this guide.
Click Edit and deploy new revision.
Click Deploy.
You see an error message similar to the following:
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
Reset the policy to allow all images
To reset the policy to allow all images, do the following:
Go to the Binary Authorization page in the Google Cloud console.
Click Edit Policy.
Select Allow All Images.
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:
Locate the service you want to delete in the services list, and click its checkbox to select it.
Click Delete. This deletes all revisions of the service.
To disable Binary Authorization, see Disabling Binary Authorization.
What's next
Use breakglass to bypass Binary Authorization enforcement.
Use the
built-by-cloud-build
attestor to deploy only images built by Cloud Build.Configure the Binary Authorization policy by using the Google Cloud console or the command-line tool.
View Binary Authorization for Cloud Run events in Cloud Audit Logs.