# Rotate keys and secrets

Replace each key and secret ClipMux uses without stopping playback, and what to do when one leaks.

Source: https://clipmux.com/docs/self-host/operate/rotate-secrets

Every key ClipMux issues or holds can be replaced while the server keeps
running, and in most cases while viewers keep watching. This page covers
each one, in the order you're likely to need them.

The commands below are for the one-line install, run from `/opt/clipmux`,
where `clipmux admin` runs inside the app container:

```bash
cd /opt/clipmux
docker compose exec app clipmux admin list-keys --app my-app
```

## Before you start [#before-you-start]

- The owner's dashboard sign-in, and shell access to the server.
- Somewhere to put each new secret: your app's environment, your webhook
  receiver, or your offline backup.

## API keys [#api-keys]

1. Create a new key: in the dashboard, open **Apps & API**, choose the app,
   and on **API keys** choose **Create key**. It's shown once.
2. Put it in your app's backend as `CLIPMUX_API_KEY` and deploy.
3. Revoke the old one: **Revoke** next to it, or
   `clipmux admin revoke-key cmx_…`. It stops working at once.

## Signing keys [#signing-keys]

```bash
docker compose exec app clipmux admin rotate-signing-key --app my-app
```

This creates a new signing key and prints it once. The app's other keys are
retired when the longest token they may have signed runs out, so viewers
mid-session finish. Meanwhile, switch your backend's `CLIPMUX_SIGNING_KID`
and `CLIPMUX_SIGNING_KEY` to the new key.

**Revoke** on the app's **Signing keys** tab, or `clipmux admin revoke-key`
with the key's `kid`, ends a key at once: every token it signed stops
playing. Use that for a leaked key, not a routine rotation.

## Webhook secrets [#webhook-secrets]

Rotating takes two steps, so your receiver never sees a delivery it can't
verify:

1. On the app's **Webhooks** tab, choose **Rotate secret**. The new secret is
   shown once, and deliveries stay signed with the old one.
2. Give your receiver the new secret and deploy it.
3. Choose **Activate new secret**. Deliveries are signed with it from the
   next try.

Over the API, the steps are `POST /v1/webhook-endpoints/{id}/secret` and
`POST /v1/webhook-endpoints/{id}/secret/activate`. See
[Retries, redelivery and secret rotation](/docs/build/webhooks/retries).

## The content key secret [#the-content-key-secret]

The content key secret is what every video's encryption key is derived
from. In the dashboard, open **Settings**, and under
**Content encryption key** choose **Rotate key**. Or call
`POST /v1/content-keys/rotate` as the owner.

- New encodes use the new version. Existing videos keep theirs and keep
  playing. To move a video to the new version, re-encode it.
- With [Cloudflare delivery](/docs/self-host/delivery/cloudflare), the
  rotation needs a Cloudflare token, the same kind Connect uses. ClipMux
  redeploys the Worker with the new version before any video uses it. If
  the redeploy fails, the call answers `502` with the steps, and the new
  version stays unused; rotating again retries. Without a token, it answers
  `409 cloudflare_token_required`.

## The secrets key [#the-secrets-key]

The secrets key seals every secret ClipMux keeps in Postgres. It's
`CLIPMUX_SECRETS_KEY`, written as `v1:` and 64 hex digits; the one-line
install keeps it in `/opt/clipmux/.env`.

<Steps>
<Step>
### Make a new key with the next version [#make-a-new-key-with-the-next-version]

```bash
echo "v2:$(openssl rand -hex 32)"
```
</Step>

<Step>
### Give serve both keys [#give-serve-both-keys]

In `.env`, move the old value to `CLIPMUX_SECRETS_KEY_PREVIOUS` and put the
new one in `CLIPMUX_SECRETS_KEY`. Then restart: `docker compose up -d --wait`.
</Step>

<Step>
### Reseal [#reseal]

```bash
docker compose exec app clipmux admin reseal-secrets
```

It reseals every stored secret with the new key, and ends by saying
`CLIPMUX_SECRETS_KEY_PREVIOUS` can go.
</Step>

<Step>
### Drop the previous key [#drop-the-previous-key]

Remove `CLIPMUX_SECRETS_KEY_PREVIOUS` from `.env` and restart again. Then
[back up](/docs/self-host/operate/backups) the new key.

Keep the old key too, in your [recovery kit](/docs/self-host/operate/backups#recovery-kit),
until the oldest backup made under it has expired: each backup is encrypted to
the key that was current when it was taken, and the old key is the only thing
that opens it.
</Step>
</Steps>

serve refuses to start if a sealed value opens with neither key, and names
its column, so a mistake here stops the server rather than losing data.

## Provider credentials [#provider-credentials]

| Credential | How to replace it |
| --- | --- |
| Cloudflare delivery token | **Delivery**, **Connect again**, with a new token. R2's credentials derive from the token, so revoking the old one cuts off storage until you do. |
| Edge analytics token | **Replace token** in the **Edge analytics** card. |
| An R2 bucket added with a token | **Storage**, **Add R2 bucket** with the same bucket and a new token renews its keys. |
| S3 storage keys | **Rotate keys** on the bucket in **Storage**, or `PUT /v1/storage/{id}/credentials`. The old keys work until the new ones pass the checks. |
| Modal or RunPod | Connect the provider again on **Encoding**; it replaces the key. |
| Speech API or chapters LLM key | Connect again on **Captions & AI**. A failed connect keeps the old connection. |

## When a key leaks [#when-a-key-leaks]

1. Revoke it at once: `clipmux admin revoke-key ID` for an API key or a
   signing key, or the dashboard's **Revoke**.
2. Rotate whatever it could reach.
3. For a leaked content key secret, rotate it and re-encode the affected
   videos.
4. For a stolen Cloudflare token, revoke it in Cloudflare and rotate the
   content key secret: a token that can deploy a Worker can read Worker
   secrets.

## Check it works [#check-it-works]

- `clipmux admin list-keys --app my-app` shows the app's API and signing
  keys, with which are retired or revoked.
- After a webhook rotation, the endpoint's deliveries on the **Webhooks**
  tab keep succeeding.
- After rotating the secrets key, the server starts without the previous
  key, and `clipmux admin setup-test --wait` passes.

## Next [#next]

- [Back up and restore](/docs/self-host/operate/backups)
- [Security checklist](/docs/self-host/operate/security-checklist)
- [Security model](/docs/self-host/security-model)
