This guide shows you how to set up Binary Authorization to enforce
policy-based deployment of Cloud Run services and jobs.

> [!NOTE]
> **Note:** After Binary Authorization is enabled, it enforces any update to the Cloud Run service. When you implement a change that affects traffic, Binary Authorization enforces deployment on any Cloud Run revision receiving traffic.

## Before you begin

Set up Cloud Run and enable APIs, by doing the following:

## Enable Binary Authorization on an existing Cloud Run service

You can enable Binary Authorization enforcement on an existing service.
To activate enforcement after enabling it, you might need to deploy a
revision or update service traffic.

You can enable Binary Authorization enforcement on an existing service using
the Google Cloud console or Google Cloud CLI:

### Console

1. Go to the Cloud Run page in the Google Cloud console.

   [Go to Cloud Run](https://console.cloud.google.com/run?enableapi=true)
2. Click the service.

3. Click the **Security** tab.

4. To enable Binary Authorization enforcement on the service, click
   **Enable**.

5. Optional: To configure the Binary Authorization policy, click
   **Configure Policy**.

### gcloud

Enable Binary Authorization on the service and deploy it:

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

Replace `SERVICE_NAME` with a name for your service.

### YAML

1. If you are creating a new service, skip this step.
   If you are updating an existing service, download its [YAML configuration](https://docs.cloud.google.com/run/docs/reference/yaml/v1.md.txt):

   ```bash
   gcloud run services describe SERVICE --format export > service.yaml
   ```
2. Update the `run.googleapis.com/binary-authorization:` annotation as follows:

   ```yaml
   apiVersion: serving.knative.dev/v1
   kind: Service
   metadata:
     annotations:
       run.googleapis.com/binary-authorization: POLICY
     name: SERVICE
   spec:
     template:
   ```

   Replace the following:
   - <var translate="no">SERVICE</var>: the name of your Cloud Run
   - <var translate="no">POLICY</var>: set to `default`
3. Replace the service with its new configuration using the following command:

```bash
gcloud run services replace service.yaml
```

> [!NOTE]
> **Note:** We recommend that you [require Binary Authorization for Cloud Run](https://docs.cloud.google.com/binary-authorization/docs/run/requiring-binauthz-cloud-run.md.txt) by configuring an organization policy to do so. Binary Authorization can be disabled by Cloud Run developers if the policy is not configured.

## Enable Binary Authorization on an existing Cloud Run job

You can enable Binary Authorization enforcement on an existing job using the
Google Cloud console or Google Cloud CLI:

### Console

1. Go to the Cloud Run jobs page in the Google Cloud console.

   [Go to Cloud Run](https://console.cloud.google.com/run/jobs?enableapi=true)
2. Click the job to open the job details.

3. Click the **Configuration** tab.

4. Under *Binary Authorization*, select a policy from the policy list.

5. Click **Apply** to enable Binary Authorization enforcement on the job.

6. Optional: To configure the Binary Authorization policy, click
   **Configure Policy**.

### gcloud

Enable Binary Authorization on the job:

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

Replace the following:

- `JOB_NAME`: the name of your job.
- `POLICY`: the policy you want to apply. Use the value `default` to use the default policy.

We recommend that you [require Binary Authorization for Cloud Run](https://docs.cloud.google.com/binary-authorization/docs/run/requiring-binauthz-cloud-run.md.txt)
by configuring an organization policy to do so. Binary Authorization can be disabled
by Cloud Run developers if the policy is not configured.

### View the policy

To view the policy, click **View policy**.

For more information, see [Configuring a Binary Authorization policy](https://docs.cloud.google.com/binary-authorization/docs/configuring-policy-console.md.txt).

### Service or job deploy failure

If your service or job fails to deploy because it violates the Binary Authorization
policy, you might see an error like the following:

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

> [!NOTE]
> **Note:** The service or job displays a red exclamation point, but the service continues to serve the previously serving healthy revision.

The error also contains information about why the image violated the policy. In
this case, you can [use breakglass](https://docs.cloud.google.com/binary-authorization/docs/run/using-breakglass-cloud-run.md.txt)
to bypass policy enforcement and deploy the image.

> [!NOTE]
> **Note:** Using breakglass creates a log entry. [View breakglass events in Cloud Audit Logs](https://docs.cloud.google.com/binary-authorization/docs/run/viewing-audit-logs-cloud-run.md.txt).

## Enable Binary Authorization on a new service

You can enable Binary Authorization on a new service using the Google Cloud console
or Google Cloud CLI:

### Console

1. Go to the Cloud Run page:

   [Go to Cloud Run](https://console.cloud.google.com/run?enableapi=true)
2. Click **Create service**.

3. In the *Create service* form :

   1. Select Cloud Run as your development platform.
   2. Select the region where you want your service located.
   3. Enter the service name.
   4. Click **Next** to continue to the *Configure the service's first revision* page.
   5. Select **Deploy one revision from an existing container image**.
   6. Enter or select the image to deploy.
   7. Expand the **Advanced settings** section.
   8. Click the **Security** tab.
   9. Select the **Verify container deployment with Binary Authorization**
      checkbox.

      > [!NOTE]
      > **Note:** This checkbox might be disabled if your organization policy is set to require [Binary Authorization for
      > Cloud Run](https://docs.cloud.google.com/binary-authorization/docs/run/requiring-binauthz-cloud-run).

   10. Optional: Click **Configure policy** to configure the
       Binary Authorization policy. To learn more about configuring a
       policy, see [Configuring a policy](https://docs.cloud.google.com/binary-authorization/docs/configuring-policy-console.md.txt).

   11. Deploy the service.

### gcloud

Enable Binary Authorization on the service and deploy it:

```bash
  gcloud run deploy SERVICE_NAME --image=IMAGE_URL --binary-authorization=default --region=REGION
```

Replace the following:

- `SERVICE_NAME`: a name for your service.
- `IMAGE_URL`: the image you want to deploy.
- `REGION`: the region in which you want to deploy your service.

## Enable Binary Authorization on a new job

You can enable Binary Authorization on a new job using one of the following:

### Console

1. Go to the Cloud Run jobs page:

   [Go to Cloud Run jobs](https://console.cloud.google.com/run/jobs?enableapi=true)
2. Click **Create Job** to display the **Create job** form.

   1. In the form, specify the container image containing the job code or select from a list of containers previously deployed.
   2. The job name is automatically generated from the container image. You can edit or change the job name as needed. A job name cannot be changed after the job is created.
   3. Select the region where you want your job located.
   4. Specify the number of tasks that you want to run in the job. All of the tasks must succeed for the job to succeed. By default, the tasks execute in parallel.
3. Click **Container, Variables \& Secrets, Connections, Security** to
   set additional job properties.

   - Click the **Settings** tab. In the **Resources** section, configure
     the following:

     1. In the **Memory** menu, specify the amount of memory required. The default is the minimum required, 512 MiB.
     2. In the **CPU** menu, specify the amount of CPU required. The default is the minimum required, 1 CPU.
   - In the **Task capacity** section:

     1. Under **Task timeout**, specify the maximum amount of time in
        seconds that the task can run, up to
        168 hours (7 days). Each task must complete within
        the specified time. The default is
        10 minutes.

     2. Under **Number of retries per failed task**, specify the number of
        retries in case of task failures. The default
        is 3 retries.

   - In the **Parallelism** section:

     1. In most cases you can select **Run as many tasks concurrently as possible**,.
     2. If you need to set a lower limit due to scaling constraints on resources your job accesses, select **Limit the maximum number of concurrent tasks** and specify the number of concurrent tasks in the **Custom parallelism limit** field.
4. When you are finished configuring your job, click **Create** to
   create the job in Cloud Run.

5. To execute the job, see [execute jobs](https://docs.cloud.google.com/run/docs/execute/jobs.md.txt) or
   [execute jobs on a schedule](https://docs.cloud.google.com/run/docs/execute/jobs-on-schedule.md.txt).

### gcloud

1. Create a new job with Binary Authorization enabled:

   ```bash
   gcloud run jobs create JOB_NAME \
     --image IMAGE_URL OPTIONS \
     --binary-authorization=POLICY \
     --region=REGION
   ```

   Replace the following:
   - `JOB_NAME`: the name of the job you want to create. You can omit this parameter, but you will be prompted for the job name if you omit it.
   - `POLICY`: the policy you want to apply. Use the value `default` to use the default policy.
   - `IMAGE_URL`: a reference to the [container image](https://docs.cloud.google.com/run/docs/building/containers.md.txt)---for example, `us-docker.pkg.dev/cloudrun/container/job:latest`.
   - `REGION`: the region where this job will run.
   - `OPTIONS`: any of the available options described in the Cloud Run [job creation page](https://docs.cloud.google.com/run/docs/create-jobs.md.txt#command-line).
2. Wait for the job creation to finish. Upon successful completion, the
   console displays a success message.

3. To execute the job, see [Execute jobs](https://docs.cloud.google.com/run/docs/execute/jobs.md.txt) or
   [execute jobs on a schedule](https://docs.cloud.google.com/run/docs/execute/jobs-on-schedule.md.txt).

When you create a new job, the Cloud Run [service agent](https://docs.cloud.google.com/iam/docs/service-agents.md.txt)
needs to be able to access the container, which is the case by default.

### YAML

1. Create a new `job.yaml` file with this content:

   ```yaml
   apiVersion: run.googleapis.com/v1
   kind: Job
   metadata:
     name: JOB
     annotations:
       run.googleapis.com/binary-authorization: POLICY
   spec:
     template:
       spec:
         containers:
         - image: IMAGE
   ```

   Replace the following:
   - <var translate="no">JOB</var>: the name of your Cloud Run job
   - <var translate="no">IMAGE</var>: the URL of your container image
   - <var translate="no">POLICY</var>: set to `default`
2. Deploy the new job using the following command:

   ```bash
   gcloud run jobs replace job.yaml
   ```

## What's next

- Use the `built-by-cloud-build` attestor to [deploy only images built by Cloud Build](https://docs.cloud.google.com/binary-authorization/docs/deploy-cloud-build.md.txt).
- Configure the Binary Authorization policy using the [Google Cloud console](https://docs.cloud.google.com/binary-authorization/docs/configuring-policy-console.md.txt) or the [command-line tool](https://docs.cloud.google.com/binary-authorization/docs/configuring-policy-cli.md.txt).
- [Use attestations](https://docs.cloud.google.com/binary-authorization/docs/attestations.md.txt) to deploy only signed container images.
- [Watch a video](https://www.youtube.com/watch?v=b7GdpEEvGDQ) on how to use Binary Authorization in Cloud Run so that only approved builds are deployed.