# Rotate secrets

Source: https://dropshelf.ai/docs/self-host/operate/rotate-secrets

Replace the session secrets, the connector secrets, the OIDC client secret and the database password of a dropshelf instance installed with Helm.

Rotate a secret on a schedule, or at once when it may have been exposed. This page covers the four
secrets a dropshelf instance holds, for an instance installed with the Helm chart.

## Prerequisites [#prerequisites]

- `kubectl` access to the `dropshelf` namespace.
- `openssl`, to generate new values.
- The Secret `dropshelf-secrets`, which the chart reads every secret setting from.

## How a rotation reaches the pods [#how-a-rotation-reaches-the-pods]

Every key in `dropshelf-secrets` becomes an environment variable, and the server reads it at startup.
The chart does not roll the pods when the Secret changes, so every rotation ends with a restart:

```bash
kubectl -n dropshelf rollout restart deployment/dropshelf
kubectl -n dropshelf rollout status deployment/dropshelf
```

On a single host with Docker Compose, change the same variable where you set it and recreate the
container.

## Rotate the session secrets [#rotate-the-session-secrets]

The session secrets encrypt the cookies that keep members signed in to the dashboard and to drops.
`DROPSHELF_AUTH_SESSION_SECRETS` is a comma-separated list: the first secret signs new cookies, and
every secret in the list still opens them. So you add the new secret first and remove the old one a
day later, and members notice nothing.

To rotate the session secrets:

1. Put a new secret at the front of the list, keeping the current ones:

   ```bash
   current=$(kubectl -n dropshelf get secret dropshelf-secrets -o go-template='{{index .data "DROPSHELF_AUTH_SESSION_SECRETS" | base64decode}}')
   kubectl -n dropshelf patch secret dropshelf-secrets --type merge \
     -p "{\"stringData\":{\"DROPSHELF_AUTH_SESSION_SECRETS\":\"$(openssl rand -hex 32),$current\"}}"
   ```

   Each secret must be at least 32 bytes, and `openssl rand -hex 32` prints 64 characters.

2. Restart the pods, as in [How a rotation reaches the pods](#how-a-rotation-reaches-the-pods). From
   then on, new cookies are signed with the new secret.

3. Wait for `auth.session.ttl`, 24 hours by default. A cookie is renewed under the new secret once
   half of that time has passed, and any cookie not renewed by then has expired.

4. Keep only the first secret in the list:

   ```bash
   newest=$(kubectl -n dropshelf get secret dropshelf-secrets -o go-template='{{index .data "DROPSHELF_AUTH_SESSION_SECRETS" | base64decode}}' | cut -d, -f1)
   kubectl -n dropshelf patch secret dropshelf-secrets --type merge \
     -p "{\"stringData\":{\"DROPSHELF_AUTH_SESSION_SECRETS\":\"$newest\"}}"
   ```

5. Restart the pods again.

If a session secret has leaked, skip the wait. Replace the whole list with one new secret and restart:
every member is signed out of the dashboard and of every drop, and signs in again. Personal access
tokens, and the sign-ins of the `dropshelf` CLI and agents, carry on either way.

### Confirm it worked [#confirm-it-worked]

Print the list the Secret holds, one secret per line:

```bash
kubectl -n dropshelf get secret dropshelf-secrets \
  -o go-template='{{index .data "DROPSHELF_AUTH_SESSION_SECRETS" | base64decode}}{{"\n"}}' | tr , '\n'
```

After step 4 it prints one line. Then check that every pod started after your last change:

```bash
kubectl -n dropshelf get pods -l app.kubernetes.io/name=dropshelf \
  -o custom-columns=NAME:.metadata.name,START:.status.startTime
```

A pod that started before the change still holds the old list, so restart again.

## Rotate the connector secrets [#rotate-the-connector-secrets]

The connector secrets seal the credentials of the databases that connectors read, and each member's
connection to an MCP server. They are separate
from the session secrets and rotate the same way: the first one seals, and every one in the list
opens. This applies only when `connectors.enabled` is `true`.

To rotate the connector secrets:

1. Put a new secret at the front of `DROPSHELF_CONNECTORS_SECRETS`, keeping the current ones:

   ```bash
   current=$(kubectl -n dropshelf get secret dropshelf-secrets -o go-template='{{index .data "DROPSHELF_CONNECTORS_SECRETS" | base64decode}}')
   kubectl -n dropshelf patch secret dropshelf-secrets --type merge \
     -p "{\"stringData\":{\"DROPSHELF_CONNECTORS_SECRETS\":\"$(openssl rand -hex 32),$current\"}}"
   ```

2. Restart the pods, so every replica can open credentials under both secrets.

3. Seal every stored credential again with the new secret:

   ```bash
   kubectl exec -n dropshelf deploy/dropshelf -- /dropshelf-server connectors reseal
   ```

   The output is similar to:

   ```text
   4 source credentials sealed with the first of connectors.secrets; the others may now be removed
   ```

4. Keep only the first secret in the list:

   ```bash
   newest=$(kubectl -n dropshelf get secret dropshelf-secrets -o go-template='{{index .data "DROPSHELF_CONNECTORS_SECRETS" | base64decode}}' | cut -d, -f1)
   kubectl -n dropshelf patch secret dropshelf-secrets --type merge \
     -p "{\"stringData\":{\"DROPSHELF_CONNECTORS_SECRETS\":\"$newest\"}}"
   ```

5. Restart the pods again.

The same command seals members' connections to MCP servers again, so no member has to connect
again.

A credential that a source reads from a `DROPSHELF_CONNECTOR_` environment variable is not sealed.
Rotate it by changing that variable in the Secret and restarting.

### Confirm it worked [#confirm-it-worked-1]

On **Admin > Connectors**, open a database, and on its **Connection** tab select **Test connection**. A source whose credential
cannot be opened fails the test. The event log has a `connector.secrets.reseal` line with the count.

## Rotate the OIDC client secret [#rotate-the-oidc-client-secret]

dropshelf uses the client secret only when a member signs in. Members who are already signed in stay
signed in while you rotate it.

To rotate the OIDC client secret:

1. At your identity provider, create a new client secret for the dropshelf client. If the provider
   allows two secrets at once, keep the old one active for now.

2. Store the new secret:

   ```bash
   kubectl -n dropshelf patch secret dropshelf-secrets --type merge \
     -p '{"stringData":{"DROPSHELF_AUTH_OIDC_CLIENT_SECRET":"<new-client-secret>"}}'
   ```

3. Restart the pods.

4. Delete the old secret at your identity provider.

If your provider holds only one secret per client, sign-ins fail from step 1 until the restart
finishes. Do the steps without a pause.

### Confirm it worked [#confirm-it-worked-2]

Sign in to `https://drop.example.com` in a private browser window. The dashboard opens.

## Rotate the database password [#rotate-the-database-password]

The database password is part of `DROPSHELF_DATABASE_URL`. Postgres checks a password only when a
connection opens, so open connections keep working while you rotate it.

To rotate the database password:

1. Generate a new password. A hex value needs no escaping inside a URL.

   ```bash
   openssl rand -hex 24
   ```

2. Set it on the database role, in `psql` as a user allowed to change that role:

   ```sql
   ALTER ROLE dropshelf WITH PASSWORD '<new-password>';
   ```

3. Store the URL with the new password:

   ```bash
   kubectl -n dropshelf patch secret dropshelf-secrets --type merge \
     -p '{"stringData":{"DROPSHELF_DATABASE_URL":"postgres://dropshelf:<new-password>@<postgres-host>:5432/dropshelf?sslmode=require"}}'
   ```

4. Restart the pods.

Do steps 2 to 4 without a pause. Until the restart finishes, a pod that opens a new connection with
the old password is refused.

### Confirm it worked [#confirm-it-worked-3]

Ask the instance for readiness:

```bash
curl https://drop.example.com/readyz
```

```text
ready
```

An answer of `not ready: store: ...` means the pods cannot reach Postgres. Check the URL in the
Secret, and see [Troubleshooting](/docs/self-host/operate/troubleshooting).

## Next steps [#next-steps]

- [Install with Helm](/docs/self-host/install/helm)
- [Identity provider](/docs/self-host/configure/identity-provider)
- [Turn on features](/docs/self-host/configure/features)
- [Helm chart values](/docs/reference/helm-values#secrets)
- [Configuration](/docs/reference/configuration#auth)
