# Configure Workload Identity Federation for Google Artifact Registry

Use Workload Identity Federation to let the Replicated proxy registry pull private images from Google Artifact Registry (GAR) with short-lived credentials. This also works for Artifact Registry repositories served on `gcr.io`.

You can configure this authentication method in the Vendor Portal or with the Vendor API v3. For API setup, see [Create an external registry with the specified parameters](https://replicated-vendor-api.readme.io/reference/createexternalregistry).

For authentication with a JSON key or token, see [Add and manage external registries](packaging-private-images).

## Overview

Google Cloud trusts the Replicated Vendor API as an OpenID Connect (OIDC) identity provider. Replicated uses your team's OIDC token to obtain a short-lived access token for your Google Cloud service account. The account's permissions determine which repositories Replicated can pull images from.

## Prerequisites

* A private Docker repository in Artifact Registry with an image to test.
* A Google Cloud service account to use for image pulls.
* Permission to configure a workload identity pool and update Identity and Access Management (IAM) policies for the service account and repositories.

## Step 1. Get your Replicated issuer and team ID

1. In the [Vendor Portal](https://vendor.replicated.com), open your application and go to **Image Registries**.
1. To connect a repository, click **Add external registry** and select **Google Artifact Registry** as the **Provider**. To migrate an existing registry, click **Edit** on its registry card.
1. For **Auth Type**, select **Workload Identity Federation**.
1. Expand **Set up access in Google Cloud**. Copy the issuer URL (`https://api.replicated.com/vendor`) and the team ID shown in the attribute condition.

## Step 2. Set up a workload identity pool

1. Create a workload identity pool for Replicated and add an OpenID Connect (OIDC) provider. Set the issuer URI to the URL from the Vendor Portal. Use the default audience.
1. Map `google.subject` to `assertion.sub`. Set the attribute condition to `assertion.sub == 'TEAM_ID'`, replacing `TEAM_ID` with your Replicated team ID. This condition limits federation through the provider to your team.
1. Copy the provider resource name. Use the numeric project number of the project that owns the pool. For example, `projects/123456789/locations/global/workloadIdentityPools/replicated-pool/providers/replicated-provider`.

For required APIs and Google Cloud setup steps, see [Configure Workload Identity Federation with other identity providers](https://docs.cloud.google.com/iam/docs/workload-identity-federation-with-other-providers).

## Step 3. Grant access to the service account and repositories

1. On the service account that Replicated uses, grant **Workload Identity User** (`roles/iam.workloadIdentityUser`) to your team's federated principal. Use the following member format:

   ```text
   principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/subject/TEAM_ID
   ```

   Replace `PROJECT_NUMBER` and `POOL_ID` with the project number and ID of your workload identity pool. Replace `TEAM_ID` with the same Replicated team ID used in the attribute condition.

1. On each Artifact Registry repository that Replicated pulls from, grant the service account **Artifact Registry Reader** (`roles/artifactregistry.reader`).

For steps to grant repository permissions, see [Access control with IAM](https://docs.cloud.google.com/artifact-registry/docs/access-control).

## Step 4. Configure the registry in the Vendor Portal

1. Return to the registry form in the Vendor Portal. When adding a registry, enter its hostname in **Hostname**. Workload Identity Federation accepts only `LOCATION-docker.pkg.dev` hostnames, such as `us-east1-docker.pkg.dev` or `us-docker.pkg.dev`, and the hostnames `gcr.io`, `us.gcr.io`, `eu.gcr.io`, and `asia.gcr.io`.
1. When adding a registry, you can set **Registry Name (optional)** to select the entry by name in a proxy image reference. See [How Replicated selects a registry entry](#how-replicated-selects-a-registry-entry).
1. In **Workload identity provider**, enter the provider resource name from step 2. In **Service account email**, enter the email of the account you granted repository access to in step 3.
1. To match images from a specific project or repository, enter its path in **Repository prefix (optional)**. For example, use `my-project` or `my-project/my-repository`.
1. For **App Access**, select **All apps** or choose **Specific apps** and select the applications that need access.
1. Enter and test a private image that matches the registry and repository prefix. For example, use `my-project/my-repository/my-image:v1.0.0` with a `pkg.dev` hostname or `my-project/my-image:v1.0.0` with `gcr.io`.
   * When adding a registry, click **Test**, enter the image in **Private image**, and click **Test** in the dialog. After the test passes, close the dialog and click **Save**.
   * When editing a registry, enter the image in **Private image** on the edit form and click **Test connection**. After the test passes, click **Save changes**.

Replicated tests the same private image again before saving a registry or changing its workload identity settings. You can save app access changes without retesting an unchanged workload identity.

## Step 5. Verify the registry connection in a test release

1. Use a fresh private image in a test release. Confirm that a customer with a valid test license can pull it through the Replicated proxy registry using your application's normal installation method.
1. If you distribute air gap bundles, build a fresh bundle for that test release and confirm that it contains the private image. See [View the images in an air gap bundle](replicated-onboarding-air-gap#view-the-images-in-an-air-gap-bundle).

## Switch an existing registry from a JSON key

To switch an existing registry, click **Edit** on its registry card in [step 1](#step-1-get-your-replicated-issuer-and-team-id), then follow the setup and verification steps. Replicated keeps the registry name and clears the saved JSON key from the registry entry when you save the workload identity settings.

If you need a rollback option, keep a secure copy of the JSON key and leave it active in Google Cloud while you verify the migration. To switch back, edit the registry, select **Service Account JSON Key**, re-enter the key, and click **Save changes**. After verification, you can retire the old key if no other workload uses it.

## How Replicated selects a registry entry

For hostname-based pulls, Replicated considers the registry entries that share the hostname and are available to the application. It selects the workload identity entry with the longest matching repository prefix. If the best match has an empty prefix, a matching entry with stored credentials takes precedence.

If multiple entries still tie for the longest matching prefix, the pull fails with a `multiple registries match this image` error. Give the entries distinct prefixes or use the registry name in the proxy image reference to select an entry.

### Examples

Suppose `my-app` can use two workload identity entries for `us-east1-docker.pkg.dev`. The `project-reader` entry has the prefix `my-project`, and `backend-reader` has the prefix `my-project/backend`.

The following reference uses `backend-reader` because `my-project/backend` is the longer match. A hostname-based reference to an image in `my-project/frontend` uses `project-reader`.

```text
proxy.replicated.com/proxy/my-app/us-east1-docker.pkg.dev/my-project/backend/api:v1.0.0
```

To use `project-reader` for the same backend image, replace the hostname in the proxy reference with the registry name. The image must match that entry's prefix, and its service account must have pull access.

```text
proxy.replicated.com/proxy/my-app/project-reader/my-project/backend/api:v1.0.0
```

In a separate setup for the same hostname and application, suppose `gar-json-key` uses a stored JSON key and `gar-keyless` uses workload identity with an empty prefix. The hostname-based reference uses `gar-json-key`. To select the workload identity entry, use its registry name in the reference.

```text
proxy.replicated.com/proxy/my-app/gar-keyless/my-project/backend/api:v1.0.0
```

## Related topics

* [Add and manage external registries](packaging-private-images)
* [About the Replicated proxy registry](private-images-about)