> For the complete documentation index, see [llms.txt](https://docs.p0.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.p0.dev/integrations/resource-integrations/harbor/installation.md). Agents can also search and read this documentation through its [MCP server](https://docs.p0.dev/~gitbook/mcp).

# Installing Harbor

Install the P0 Harbor integration: set up the Harbor admin account, store its credential, deploy the connector, and add each Harbor instance.

{% hint style="info" %}
The Harbor integration is in beta. Functionality and configuration may change.
{% endhint %}

You install Harbor in two parts. A **Harbor connector** is P0's connector, deployed in your AWS account or Google Cloud project. A **Harbor instance** is one Harbor that a connector reaches, with its own admin account and secret. Install a connector once, then add each instance, either from the P0 app or with the P0 Terraform provider. For how the parts fit together, see [Harbor](https://docs.p0.dev/integrations/resource-integrations/harbor.md#how-it-works).

The shell commands on this page work as written in bash, zsh and fish. PowerShell and cmd quote differently.

## Before you begin

Each Harbor instance must:

* Run **Harbor 2.12.0 or later**. The connector checks the version, and P0 doesn't install an older one.
* Sign people in through **OIDC**. LDAP and database sign-in aren't supported: the connector signs in to Harbor with an OIDC ID token, which Harbor accepts only in OIDC mode.
* Be served at a public **`https` address**, with a certificate that's valid for its host and comes from a public certificate authority. The connector calls the instance over the internet.

You also need:

* **An admin of your identity provider**, to create the admin account's user and, where your provider allows it, add a client secret for P0 to Harbor's OIDC client.
* **A Harbor system administrator**, to make that user a Harbor system administrator.
* **P0 installed in the cloud the connector runs in.** P0 invokes the connector through its IAM management there, and the connector reads each instance's secret from that cloud's secret manager:

| The connector runs on | Install, for the connector's account or project                                                                               | The secret is in      | Install, for the secret's account or project                                                                             |
| --------------------- | ----------------------------------------------------------------------------------------------------------------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| AWS Lambda            | [AWS](https://docs.p0.dev/integrations/resource-integrations/aws.md#setting-up-aws-iam-management)                            | AWS Secrets Manager   | The [AWS Secrets Manager](https://docs.p0.dev/integrations/resource-integrations/aws-secrets-manager.md) integration     |
| Google Cloud Run      | [Google Cloud](https://docs.p0.dev/integrations/resource-integrations/google-cloud.md#install-iam-management-on-each-project) | Google Secret Manager | The [Google Secret Manager](https://docs.p0.dev/integrations/resource-integrations/google-secret-manager.md) integration |

* [**Terraform**](https://developer.hashicorp.com/terraform/install), to deploy the connector:
  * **AWS Lambda:** Terraform 1.4 or later, run on Linux or macOS with Docker running and the AWS CLI signed in to the connector's account. Lambda runs images only from Amazon ECR, so the connector's Terraform copies P0's image from Docker Hub into an ECR repository with a shell script. The apply fails on Windows, and on remote runs that have no Docker, such as HCP Terraform's.
  * **Google Cloud Run:** Terraform 1.2 or later, and the `hashicorp/google` provider 6.0.0 or later. Cloud Run pulls P0's image from Docker Hub itself, so you don't need Docker. The machine that runs Terraform still has to reach Docker Hub, because every plan reads the image's digest there. The [Service Usage API](https://console.cloud.google.com/apis/library/serviceusage.googleapis.com) must be enabled in the connector's project, because the connector's Terraform enables the IAM and Cloud Run APIs through it.
  * **Both:** the `kreuzwerker/docker` provider 3.3.0 or later, below 5.0. The connector's Terraform declares it in a `required_providers` block of its own. If your configuration already has a `required_providers` block, add the `docker` entry to it, at 3.3 or later, and delete the generated block: Terraform refuses a second one.

The secret can be in a different account or project from the connector, in the same cloud. On AWS, the connector and the secret must both be in commercial regions, not in GovCloud, China, ISO or European Sovereign Cloud regions. For either one, the installer refuses another region with `Harbor supports only AWS commercial regions for now.`

P0 grants access only to people who already have a Harbor user. For what each requestor needs, see [Requesting Harbor access](https://docs.p0.dev/integrations/resource-integrations/harbor/requesting-access.md).

## Step 1: Set up the admin account

The connector signs in to each Harbor instance as a dedicated admin account from your identity provider. Every call P0 makes on the instance, for project roles and for system administrator, signs in as that account. Set one up for each instance, and use it for nothing else.

{% hint style="warning" %}
**The admin account is a Harbor system administrator.** It can make anyone a member of any project at any role, and a system administrator. Its refresh token and the client secret in the admin credential are together as sensitive as an administrator's password: anyone who holds both can sign in to Harbor as an administrator. Keep them only in the instance's secret, readable only by the connector. To cut the connector off, revoke the admin account's refresh token or sessions in your identity provider, or revoke P0's client secret if it has one of its own.
{% endhint %}

1. **Create a dedicated user in your identity provider**, such as `harbor-automation@<your-domain>`.
2. **Sign in to Harbor once as that user**, which creates its Harbor user.
3. **Make it a Harbor system administrator.** As a Harbor administrator, open **Administration**, then **Users**, select the user and set it as an administrator. Your identity provider's Harbor admin group isn't enough: P0 checks Harbor's administrator flag.
4. **Choose the client secret P0 uses.** Where your identity provider allows more than one secret on a client, add a client secret for P0 to Harbor's OIDC client: you can revoke it without touching Harbor's. If your provider allows only one, use Harbor's existing client secret. With a shared secret:
   * To rotate it later, change it in Harbor's OIDC settings and in the instance's secret together, as in [#change-the-client-secret](#change-the-client-secret "mention"). Until both have the new secret, P0 can't grant or revoke access on the instance.
   * To cut off P0 alone, revoke the admin account's refresh token or sessions in your identity provider. Revoking the secret cuts off Harbor's own sign-in too.

### Get a refresh token for the admin account

The connector signs in as the admin account with a refresh token from Harbor's OIDC client. Get one with your identity provider's standard OAuth authorization code flow, with the scopes `openid email` and offline access. These steps use placeholders: replace each `<...>` with your own value.

1. **Find your provider's endpoints.** Open `<issuer>/.well-known/openid-configuration`, where `<issuer>` is the issuer in Harbor's OIDC settings, such as `https://accounts.google.com`. Note its `authorization_endpoint` and `token_endpoint`.
2. **Add a temporary redirect URI** to Harbor's OIDC client in your identity provider, such as `http://localhost:8085`. Nothing has to listen there. Remove it in the last step. At Google, a new redirect URI can take a few minutes to start working.
3. **Open the authorization URL in a private browser window**, filled in with your values on one line:

   ```
   <authorization_endpoint>?response_type=code&client_id=<client-id>&redirect_uri=http://localhost:8085&scope=openid%20email&state=p0&access_type=offline&prompt=consent
   ```

   `<client-id>` is Harbor's OIDC client ID. `access_type=offline` and `prompt=consent` are Google's way to ask for a refresh token. With most other providers, add `offline_access` to the scope instead: `scope=openid%20email%20offline_access`.
4. **Sign in as the admin account and consent.** The browser then opens the redirect URI, which doesn't load. Copy the value of `code` from the address bar.
5. **Exchange the code for tokens** at the token endpoint, with the client ID and the client secret from [#step-1-set-up-the-admin-account](#step-1-set-up-the-admin-account "mention"). The code works once, and only for a few minutes. The command holds the client secret, so run it in a shell that doesn't save its history, such as `fish --private`:

   ```bash
   curl -s <token_endpoint> \
     -d grant_type=authorization_code \
     -d code=<code> \
     -d redirect_uri=http://localhost:8085 \
     -d client_id=<client-id> \
     --data-urlencode client_secret=<client-secret>
   ```

   The answer's `refresh_token` is what P0 needs. If it has none, start again from step 3 with offline access in the request.
6. **Remove the temporary redirect URI** from Harbor's OIDC client.

If your provider rotates refresh tokens, turn rotation off for Harbor's OIDC client. The connector only reads the secret and never stores a new refresh token, so with rotation the stored token stops working after the connector's first call, and grants and revokes fail as in [#sign-the-admin-account-in-again](#sign-the-admin-account-in-again "mention").

### Check the credential

Before you store the credential, check that your identity provider accepts it the way the connector uses it. This command asks for new tokens with the refresh token, and sends the client ID and secret with HTTP Basic authentication, as the connector does. It prints `ok`, or the provider's OAuth error code, and never the tokens. It needs [`jq`](https://jqlang.org/). Run it with rotation off, as above, in the same private shell:

```bash
curl -s <token_endpoint> \
  -u "<client-id>:<client-secret>" \
  -d grant_type=refresh_token \
  --data-urlencode refresh_token=<refresh-token> \
  | jq -r '.error // "ok"'
```

* **`ok`:** the credential works. Store it in [#step-2-store-the-admin-credential](#step-2-store-the-admin-credential "mention").
* **`invalid_client` or `unauthorized_client`:** the provider doesn't accept the client ID and secret. Check that the ID is Harbor's OIDC client ID and the secret is a current secret of that client.
* **`invalid_grant`:** the provider doesn't accept the refresh token. Get a new one, as above.

If your provider's discovery document lists only `client_secret_post` under `token_endpoint_auth_methods_supported`, the connector sends the client ID and secret in the request body instead. Then replace the `-u` line with `-d client_id=<client-id> --data-urlencode client_secret=<client-secret> \`.

## Step 2: Store the admin credential

Save the admin credential as a file, such as `harbor-admin.json`, with these four fields:

```json
{
  "issuer": "<issuer>",
  "clientId": "<client-id>",
  "clientSecret": "<client-secret>",
  "refreshToken": "<refresh-token>"
}
```

`issuer` is the issuer in Harbor's OIDC settings, `clientId` is Harbor's OIDC client ID, and `clientSecret` is the client secret from [#step-1-set-up-the-admin-account](#step-1-set-up-the-admin-account "mention"), P0's own or Harbor's. Store the file as the instance's secret, in the secret manager of the connector's cloud. You choose the secret's name, and you enter it in P0 in [#step-4-add-the-harbor-instance](#step-4-add-the-harbor-instance "mention"). Each instance has one secret.

For Google Secret Manager, with `gcloud` signed in to an account that can create secrets in the project:

```bash
gcloud secrets create harbor-prod-admin \
  --project <project-id> \
  --data-file harbor-admin.json
```

For AWS Secrets Manager, with the AWS CLI signed in to the secret's account:

```bash
aws secretsmanager create-secret \
  --name harbor/prod/admin \
  --secret-string file://harbor-admin.json \
  --region <region>
```

Then delete the file. Don't pass the JSON on the command line, where your shell history keeps it.

A secret's name follows its secret manager's rules:

| Secret manager        | Secret name                                        |
| --------------------- | -------------------------------------------------- |
| AWS Secrets Manager   | Up to 512 letters, digits and `/_+=.@-`            |
| Google Secret Manager | Up to 255 letters, digits, underscores and hyphens |

For a secret in a different AWS account from the connector, the name can't end in a hyphen and six characters, such as `-AbCdEf`: Secrets Manager can't find a secret in another account by such a name, and P0 refuses it. Use another name, or a secret in the connector's own account.

On AWS, a secret in a different account from the connector must be encrypted with a customer-managed KMS key, and both the secret's resource policy and the key's policy must allow the connector's role. Pass the key to `create-secret` with `--kms-key-id <kms-key-arn>`. The instance's Terraform covers both policies, in [#step-4-add-the-harbor-instance](#step-4-add-the-harbor-instance "mention").

## Step 3: Install the connector

One connector can serve several Harbor instances, so skip this step if you already have a connector that should reach the new instance. Every instance behind a connector shares that connector's email domain pattern (see [#the-email-domain-pattern](#the-email-domain-pattern "mention")).

{% hint style="warning" %}
**A connector's ID and where it runs can't change once you add it.** To move a connector, add a new one.
{% endhint %}

1. Open the P0 dashboard at [p0.app](https://p0.app) and log in to your organization.
2. In the sidebar, click **Integrations**. Under **Resources**, click **Harbor**.
3. Select the **Harbor connector** component, and add a connector.
4. Enter an **ID** for the connector, such as `prod`. Each Harbor instance names its connector by this ID. Use lowercase letters, digits and hyphens, starting and ending with a letter or digit, up to 63 characters.
5. Under **Connector hosting**, choose **AWS Lambda** or **Google Cloud Run**, and fill in the fields:

| Hosting          | Field                | What to enter                                                                                                                                                                                                  |
| ---------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| AWS Lambda       | **AWS account ID**   | The account the connector runs in. The list shows the accounts where P0's AWS IAM management is installed.                                                                                                     |
| AWS Lambda       | **Connector name**   | The Lambda function's name, which is also the name of the IAM role it runs as. Use 2 to 64 characters: lowercase letters, digits and single hyphens, starting with a letter and ending with a letter or digit. |
| AWS Lambda       | **Connector region** | An AWS region, such as `us-west-2`                                                                                                                                                                             |
| Google Cloud Run | **GCP project ID**   | The project the connector runs in. The list shows the projects where P0's Google Cloud IAM management is installed.                                                                                            |
| Google Cloud Run | **Connector name**   | The Cloud Run service's name, which is also the ID of the service account it runs as. Use 6 to 30 characters: lowercase letters, digits and hyphens, starting with a letter and ending with a letter or digit. |
| Google Cloud Run | **Connector region** | A Google Cloud region, such as `us-west1`                                                                                                                                                                      |

6. Continue to the connector's Terraform. P0 fills it in with the values you entered.
7. Add the Terraform to your configuration, set the email domain pattern variable it declares (see [#the-email-domain-pattern](#the-email-domain-pattern "mention")), and apply it.
8. Continue in the installer. P0 checks that it can invoke the connector. IAM changes can take a few seconds to take effect, so if P0 is denied access right after you apply, wait and try again.

Keep your copy of the connector's Terraform. You change the email domain pattern and the connector's image in it later.

### The email domain pattern

The connector gives Harbor access only to people whose email domain matches a regular expression that you set. The connector's Terraform declares it as a required variable, and sets it on the connector as the `ALLOWED_EMAIL_DOMAIN_PATTERN` environment variable. P0 never stores or sends the pattern, and can't change it.

The variable's name ends in `_allowed_email_domain_pattern` and carries the connector's ID, with each hyphen written as an underscore. For the connector `prod-eu` it's `p0_harbor_connector_prod_eu_allowed_email_domain_pattern`. Copy the name from the Terraform that P0 generated.

Write the pattern this way:

* **Match the whole domain.** The connector tests the pattern against the text after the `@`, and the pattern has to match all of it. Leave out the `@`, and leave out `^` and `$`: the connector anchors the pattern itself.
* **Write each dot as `[.]`.** A bare `.` matches any character, so `example.com` also allows `exampleXcom`. Write `example[.]com`.
* **Separate several domains with `|`**, as in `example[.]com|example[.]net`.
* **Name subdomains if you want them.** `example[.]com` doesn't match `mail.example.com`. To allow every subdomain too, use `([a-z0-9-]+[.])*example[.]com`.

Capitalization doesn't matter.

Set the variable in a `.tfvars` file:

```terraform
p0_harbor_connector_prod_eu_allowed_email_domain_pattern = "example[.]com"
```

To pass it on the command line instead, single-quote it so the shell leaves the brackets alone:

```bash
terraform apply -var 'p0_harbor_connector_prod_eu_allowed_email_domain_pattern=example[.]com'
```

While the pattern is unset, blank, or isn't a regular expression that compiles, the connector refuses every request, and P0 can't finish installing an instance through it.

To change the pattern later, change the variable and apply again. The connector uses the new pattern from its next call, and nothing changes in P0. A person whose domain the new pattern no longer allows keeps the access they already have until it ends, and P0 still revokes it then.

### What the connector's Terraform creates on AWS

| Resource                                               | What it's for                                                                                                                                                                                       |
| ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| An ECR repository, named after the connector           | Holds the copy of P0's connector image, `p0security/p0-connector-harbor`, that Lambda runs. Its tags can't be overwritten, so pushing to the repository can't replace the image the connector runs. |
| A CloudWatch log group, `/aws/lambda/<connector-name>` | The connector's logs, kept for 30 days. Change `log_retention_days` in the Terraform to keep them for longer or shorter.                                                                            |
| An IAM role, named after the connector                 | The identity the connector runs as. The connector's Terraform lets it write its own logs. Each Harbor instance's Terraform adds read access to that instance's secret.                              |
| The Lambda function                                    | Runs the connector. It calls your Harbor instances over the internet, so it has no VPC configuration.                                                                                               |
| A policy on P0's role in the account                   | `lambda:InvokeFunction` on the connector's function, and nothing else.                                                                                                                              |

### What the connector's Terraform creates on Google Cloud

| Resource                                                                            | What it's for                                                                                                                                                                                                                                          |
| ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| The IAM and Cloud Run APIs, enabled in the connector's project                      | The service account and the Cloud Run service. Destroying the Terraform leaves them enabled, since other services may use them. If the first apply fails right after enabling them, apply again: a newly enabled API can take a few minutes to answer. |
| A service account, whose ID is the connector's name                                 | The identity the connector runs as. It has no role of its own. Each Harbor instance's Terraform adds read access to that instance's secret.                                                                                                            |
| The Cloud Run service, named after the connector                                    | Runs P0's connector image, `p0security/p0-connector-harbor-gcloud`, pulled from Docker Hub by tag and digest. Its `INVOKER_SA_EMAIL` environment variable is P0's own service account, and the connector accepts calls from that account alone.        |
| `roles/run.invoker` and `roles/run.viewer` on the service, for P0's service account | Lets P0 call the connector, and read the service to find its address. Nothing on the project, and no one else.                                                                                                                                         |
| A check of the image's digest                                                       | Reads the tag's digest from Docker Hub on every plan, through a `docker` provider configured not to need a Docker daemon, and stops the plan if the tag no longer matches the pinned digest.                                                           |

{% hint style="warning" %}
**Don't destroy and recreate the connector's service account.** Google Cloud treats a recreated service account as a new identity, even with the same email, so it loses the read access that each Harbor instance granted it. If you recreate it, apply each instance's Terraform again, and deploy the connector again.
{% endhint %}

## Step 4: Add the Harbor instance

{% hint style="warning" %}
**None of an instance's settings can change once you add it, so check each one before you continue.** To use another connector, secret manager or secret name, you delete the instance and add it again. For another address, you add a new instance with its own ID. Read [#remove-or-replace-an-instance](#remove-or-replace-an-instance "mention") first.
{% endhint %}

1. On the Harbor integration's page in P0, select the **Harbor instance** component, and add an instance.
2. Enter an **ID** for the instance, such as `prod`. People name the instance by this ID when they request access, and access policies match it. It follows the same rule as a connector's ID.
3. Fill in the fields:

| Field                                               | What to enter                                                                                                                                                                                                                                 |
| --------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Connector**                                       | The connector that reaches this instance. The list shows the connectors you've finished installing.                                                                                                                                           |
| **Harbor URL**                                      | The instance's address, as `https://<host>` or `https://<host>:<port>`, such as `https://harbor.example.com`. Write it in lowercase, with nothing after the host or port: no trailing slash, no path, no username or password, and no `:443`. |
| **Secret manager provider**                         | **AWS Secrets Manager** for a connector on AWS Lambda, or **Google Secret Manager** for one on Google Cloud Run                                                                                                                               |
| **Installation**, under **Secret manager provider** | The account or project the secret is in. The list shows the ones where P0's integration for that secret manager is installed.                                                                                                                 |
| **Secrets region**, for AWS Secrets Manager         | The secret's region, such as `us-west-2`                                                                                                                                                                                                      |
| **Admin credential secret**                         | The name of the secret from [#step-2-store-the-admin-credential](#step-2-store-the-admin-credential "mention"). Enter the name, never an ARN or a resource name, and never the credential itself.                                             |

4. Continue to the instance's Terraform, and apply it after the connector's. It gives the connector read access to this instance's secret, and to no other secret. On AWS, if a customer-managed KMS key encrypts the secret, set `kms_key_arn` in the Terraform to the key's ARN, not an alias, before you apply.

   For a secret on AWS in a different account from the connector, P0 gives you three pieces, in order:

   1. **Terraform for the connector's account:** read access on the connector's role. The plan fails until you set `kms_key_arn`.
   2. **Terraform for the secret's account:** the secret's resource policy, letting the connector's role read the secret. Apply it with credentials for the secret's account. **It replaces any resource policy the secret already has.** If the secret has one, add the statement to that policy instead of applying this. To check, run `aws secretsmanager get-resource-policy --secret-id <secret-name> --region <region>` in the secret's account.
   3. **A statement for the KMS key's policy,** as JSON. Add it to the policy of the key that encrypts the secret, beside the statements already there. Don't replace the key's policy with it.

   If the connector's role is ever deleted and created again, apply the second piece again and add the key statement again: AWS's policies name the old role, not the new one.
5. Continue in the installer. P0 checks the instance through the connector, in this order:
   1. The connector can read the secret, and the secret holds the admin credential.
   2. The connector's email domain pattern is set, and compiles.
   3. Your identity provider signs the admin account in with the credential.
   4. The address answers as Harbor does, runs Harbor 2.12.0 or later, and signs people in through OIDC.
   5. Harbor accepts the admin account's sign-in, and the account has Harbor's administrator flag.
   6. Harbor reports its version.

P0 stops at the first check that fails, and its message names the check, such as `while signing the admin account in with its identity provider`, and says what to fix. If the fix is in Harbor, in your identity provider, in the secret's contents, in the connector's read access or in the email domain pattern, make it and try again. If the fix is the address, the connector, the secret manager or the secret's name, delete the instance and add it again with the right value.

{% hint style="warning" %}
**Keep the connector's read access to the secrets of its own instances, and to nothing else.** P0 names the secret in each call to the connector, so any other secret the connector can read, whether through a wildcard or through a grant on the whole account or project, is one a call could point it at.
{% endhint %}

The connector needs these permissions on each instance's secret, which the instance's Terraform grants:

| Secret manager        | What the connector's identity needs                                                                                                                                                                                       |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| AWS Secrets Manager   | `secretsmanager:GetSecretValue` on the secret, and `kms:Decrypt` on its key if a customer-managed KMS key encrypts it                                                                                                     |
| Google Secret Manager | `roles/secretmanager.secretAccessor` and `roles/secretmanager.viewer`, both on the secret. The connector reads the secret's latest version and then that version's metadata, and the accessor role covers only the first. |

{% hint style="success" %}
People can now request roles on the instance's projects, and system administrator on the instance. See [Requesting Harbor access](https://docs.p0.dev/integrations/resource-integrations/harbor/requesting-access.md).
{% endhint %}

## Install with Terraform

{% hint style="info" %}
The `p0_harbor_*` resources require P0 Terraform provider [v0.69.0](https://github.com/p0-security/terraform-provider-p0/releases/tag/v0.69.0) or later.
{% endhint %}

Instead of the installer, you can install Harbor with the P0 Terraform provider (see [Manage integrations as code with the P0 Terraform provider](https://docs.p0.dev/getting-started/get-started-with-the-p0-terraform-provider.md)). You still set up the admin account and store its credential, as in [#step-1-set-up-the-admin-account](#step-1-set-up-the-admin-account "mention") and [#step-2-store-the-admin-credential](#step-2-store-the-admin-credential "mention"). Each resource's page in the Terraform registry has a complete example for each cloud, including the cloud resources. Create the resources in this order:

1. **Stage the connector** with [`p0_harbor_connector_staged`](https://registry.terraform.io/providers/p0-security/p0/latest/docs/resources/harbor_connector_staged). Set `id` and `hosting`. For Google Cloud Run, P0 answers with `hosting.connector_service_account`, the service account the connector must run as.
2. **Deploy the connector** where `hosting` says, and set its `ALLOWED_EMAIL_DOMAIN_PATTERN` environment variable. The pattern isn't an attribute of any P0 resource. Write it as described in [#the-email-domain-pattern](#the-email-domain-pattern "mention"), such as `"example[.]com"`. Pin the connector's image to a published tag and that tag's digest: copy both from the latest `p0_harbor_connector` example in the Terraform registry. P0 publishes the tags on Docker Hub.
   * **AWS Lambda:** use the image `p0security/p0-connector-harbor`, copied into Amazon ECR. Name the function's role after the connector, and give P0's AWS IAM management role `lambda:InvokeFunction` on the function.
   * **Google Cloud Run:** use the image `p0security/p0-connector-harbor-gcloud`, running as `hosting.connector_service_account`. Give P0's Google Cloud service account `roles/run.invoker` and `roles/run.viewer` on the service, and set the connector's `INVOKER_SA_EMAIL` environment variable to that same service account's email. The connector refuses every call while `INVOKER_SA_EMAIL` is unset.
3. **Install the connector** with [`p0_harbor_connector`](https://registry.terraform.io/providers/p0-security/p0/latest/docs/resources/harbor_connector), with the same `id` and `hosting` as the staged resource. P0 checks that it can invoke the connector.
4. **Give the connector read access to the secret**, with the permissions in [#step-4-add-the-harbor-instance](#step-4-add-the-harbor-instance "mention").
5. **Add each instance** with [`p0_harbor_instance`](https://registry.terraform.io/providers/p0-security/p0/latest/docs/resources/harbor_instance). Set `id`, `connector`, `url`, `vault` and `secret_name`, the name of the admin credential's secret, and make it depend on the secret's read access.

For example, an instance on a connector that runs on Google Cloud Run:

```terraform
resource "p0_harbor_instance" "prod" {
  id        = "prod"
  connector = p0_harbor_connector.prod.id
  url       = "https://harbor.example.com"

  vault = {
    type       = "gcp-sm"
    project_id = "my-secrets-project"
  }
  secret_name = "harbor-prod-admin"

  depends_on = [
    google_secret_manager_secret_iam_member.harbor_accessor,
    google_secret_manager_secret_iam_member.harbor_viewer,
  ]
}
```

On AWS, the provider's example names the secret `harbor/prod/admin`.

Creating the instance has P0 check it through the connector, as in [#step-4-add-the-harbor-instance](#step-4-add-the-harbor-instance "mention"). If a check fails, the apply fails with P0's message, and Terraform marks the resource as tainted. Fix what the message names and apply again: Terraform deletes the instance and creates it again.

A plan refuses a change to an installed instance's `url`. Changing its `id`, `connector`, `vault` or `secret_name` replaces the instance, and changing a connector's `id` or `hosting` replaces the connector. Read [#remove-or-replace-an-instance](#remove-or-replace-an-instance "mention") before you apply a replacement.

### Import what you added in the P0 app

If you already added a connector or an instance in the P0 app, the apply fails and says to import it. Import a connector into both of its resources, and an instance into its one:

```bash
terraform import p0_harbor_connector_staged.prod prod
terraform import p0_harbor_connector.prod prod
terraform import p0_harbor_instance.prod prod
```

## Change the install later

### Sign the admin account in again

Your identity provider can expire or revoke the admin account's refresh token: after a suspension, an idle timeout, a session or reauthentication policy such as Google Workspace's session control, or, depending on the provider, a password change. At Google:

* A refresh token that goes unused for six months expires.
* If Harbor's OIDC client belongs to an app with the External user type in Testing status, its refresh tokens expire after seven days. Internal and published apps don't have this limit.
* Google keeps at most 100 refresh tokens per Google account for each client, and invalidates the oldest past that without warning.

Nothing warns you ahead of time. Then:

* The instance's install check fails `while signing the admin account in with its identity provider`.
* Grants and revokes of both accesses, project roles and system administrator, fail with a message that says to sign the admin account in again.
* A system administrator revoke that fails leaves the person a system administrator until it succeeds.

To fix it:

1. Get a new refresh token, as in [#get-a-refresh-token-for-the-admin-account](#get-a-refresh-token-for-the-admin-account "mention").
2. Write the admin credential file again with the new `refreshToken`, and store it in the same secret as a new version. Then delete the file. For Google Secret Manager:

   ```bash
   gcloud secrets versions add <secret-name> \
     --project <project-id> \
     --data-file harbor-admin.json
   ```

   For AWS Secrets Manager:

   ```bash
   aws secretsmanager put-secret-value \
     --secret-id <secret-name> \
     --secret-string file://harbor-admin.json \
     --region <region>
   ```
3. Click **Retry revoke** on each failed revoke.

The connector reads the secret on every call, so it uses the new version from its next call. Nothing changes in P0 or in the Terraform.

### Change the client secret

If the client secret in the admin credential is revoked or rotated in your identity provider, every grant and revoke on the instance fails with a message that starts `The identity provider at <provider> rejected the client ID and secret in the Harbor admin credential's secret`. The instance's install check fails `while signing the admin account in with its identity provider`.

To fix it, put a current secret of Harbor's OIDC client in the admin credential's `clientSecret`. The refresh token belongs to the client, not to one of its secrets, so it keeps working and you don't sign the admin account in again. These commands change only `clientSecret`, and store the result as a new version of the same secret. They need `jq`. The command holds the client secret, so run it in a shell that doesn't save its history, such as `fish --private`.

For Google Secret Manager:

```bash
gcloud secrets versions access latest --secret <secret-name> --project <project-id> \
  | jq --arg s '<client-secret>' '.clientSecret = $s' \
  | gcloud secrets versions add <secret-name> --project <project-id> --data-file -
```

For AWS Secrets Manager:

```bash
aws secretsmanager get-secret-value --secret-id <secret-name> --region <region> \
  --query SecretString --output text \
  | jq --arg s '<client-secret>' '.clientSecret = $s' \
  | aws secretsmanager put-secret-value --secret-id <secret-name> --region <region> \
      --secret-string file:///dev/stdin
```

The connector uses the new version from its next call. Click **Retry revoke** on each revoke that failed.

If P0 shares Harbor's client secret, change it in Harbor's OIDC settings at the same time: under **Administration**, **Configuration**, **Authentication**, set **OIDC Client Secret**.

### Update the connector

To move the connector to a new image, change the image's pin in your copy of the connector's Terraform and apply it again. On both clouds the pin is `image_tag` and `image_digest`, in the Terraform's `locals`. For P0's current build, copy both from the latest `p0_harbor_connector` example in the Terraform registry. Nothing changes in P0.

* **AWS Lambda:** Terraform copies the new image into your ECR repository and redeploys the function, so run it where Docker is running, as for the first apply. To find a tag's digest, run `docker buildx imagetools inspect p0security/p0-connector-harbor:<tag>` and copy the `Digest` it prints.
* **Google Cloud Run:** Terraform redeploys the Cloud Run service with the new image, which Cloud Run pulls from Docker Hub. You don't need Docker. Pin the tag's own digest: the multi-platform index digest, not the digest of one platform's image, which Docker Hub also lists on the tag's page. The plan compares the pin with the tag's index digest, so a per-platform digest stops it. If you have Docker, `docker buildx imagetools inspect p0security/p0-connector-harbor-gcloud:<tag>` prints the index digest as `Digest`.

If the plan stops with `The connector image's tag doesn't resolve to the digest pinned here`, the tag on Docker Hub and the digest you pinned are different images. Terraform changes nothing: edit the pin so the tag and its digest match, and apply again.

### Remove a connector

Remove each Harbor instance that uses the connector first, as in [#remove-or-replace-an-instance](#remove-or-replace-an-instance "mention"), and destroy each instance's Terraform, which removes its read grant. Then destroy the connector's Terraform, and delete the connector in P0.

With the Terraform provider, P0 refuses to destroy a `p0_harbor_connector` while an instance uses it. The P0 app doesn't stop you from deleting such a connector, and its instances then can't grant or revoke access until you add a connector with the same ID again.

* **AWS Lambda:** each instance's grant is a policy on the connector's role, and Terraform doesn't delete a role that still has one, so destroying the connector's Terraform first fails.
* **Google Cloud Run:** destroying the connector deletes its service account. A read grant left behind on a secret names an account that no longer exists: remove it with that instance's Terraform, or from the secret's permissions.

### Remove or replace an instance

{% hint style="warning" %}
**Removing an instance from P0 doesn't revoke the access P0 granted on it.** People keep the roles P0 gave them in Harbor, and P0 can't revoke those roles while the instance isn't installed.
{% endhint %}

Before you remove or replace an instance, let its open requests end, or revoke them. Check for open requests on the **Activity** tab of [Access Management](https://docs.p0.dev/access-management/access-management.md).

* **To change the connector, the secret manager or the secret's name**, delete the instance and add it again under the same ID. With Terraform, changing `connector`, `vault` or `secret_name` does the same. A revoke that comes due while the instance isn't installed fails, and needs **Retry revoke** once the instance is installed again.
* **To move to another address**, add a new instance with its own ID, and remove the old one once its open requests have ended. The new ID is what people name in their requests, and what the `instance` filter in your access policies matches, so update both.
* **If you remove an instance for good while requests are open**, those requests show as errored when they end, with a message that starts `P0 is not installed for the instance` and names the instance. A Harbor admin sets those people's roles by hand, on each project's **Members** tab.
