# Key rotation

> Add a new version of the realm's secrets key; each secret in the realm's vault moves onto it at its next write, and nothing stops opening.

Rotating a key replaces it for everything written from then on, while everything written before still opens. A realm
administrator rotates the realm's secrets key, which seals the realm's own vault, whenever their key policy asks for
it. A user's own key is replaced for them after a nominee opens their vault through emergency access.

What's missing today: a secret moves onto the new version only when it is next written. There is no call yet that
moves every secret at once, and no way yet to list which secrets are still under an older version.

## Before you begin

- **Who:** a realm administrator holding `RealmOwner` in this realm or in a realm above it. See
  [Delegated administration](/platform/realms/delegated-administration/).
- **Where:** the API. There are no screens yet.
- **First:** the store is on, and the realm has its secrets key.

## How it works

Rotating the realm's secrets key adds a new version of it. From then on every write to the realm's vault seals under
the newest version, and each secret written after the rotation is sealed again whole, every version it holds, so one
write moves a secret onto the new key. A secret not written since stays under the older version, which still opens it.

A user's key is rotated by the store itself: after a nominee opens the user's vault through
[emergency access](/secrets-store/emergency-access/), the user's next access makes a new version of their key and
seals every grant's copy again. There is no call to rotate a user's or a group's key by hand.

## Set it up

As a realm administrator ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`):

```sh
curl -X PUT https://foobar.cvyuh.com/api/v0/am/keys/v1/secrets/encryption \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{}'
```

The realm answers `200` with the new `version`. To move one secret onto it now, a realm administrator writes it again.

## What users see

Nothing. Reads and writes go on as before; a secret written before the rotation opens under its older version.

## When it fails

| Answer | When | What to do |
|---|---|---|
| `403` | The caller isn't a realm administrator of this realm or a realm above | Ask someone holding `RealmOwner` |
| `401` | No session, or it has ended | Sign in again |

## Limits and defaults

| Limit | Value |
|---|---|
| Rotation schedule | none; you rotate when your policy asks |
| Versions kept | every version, until the key is destroyed whole |
| Withdrawing one version | not possible; see [Crypto-shredding](/secrets-store/keys-and-protection/crypto-shredding/) |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| You need every secret off the older version | Secrets move only at their next write | Write each secret in the realm's vault again; list them with `GET …/realm/metadata?list=true` |
| You can't tell which secrets have moved | No answer shows a secret's key version yet | Write every secret again after a rotation, so all are known to be on the newest version |

## Audit record

A rotation of the realm's secrets key isn't on the realm's audit trail yet. Keep your own record of when you rotate.
A secret written again after a rotation is a change record on the secret, as any write is. See
[Secrets audit trail](/secrets-store/keys-and-protection/audit-trail/).

## Good to know

- **Nothing stops opening.** A value sealed under an older version still opens by that version.
- **Paths don't move.** Where a secret is found doesn't depend on the key version, so a rotation never loses a path.
- **No single version is withdrawn.** The realm's secrets key is kept whole or destroyed whole.
- **Users' and groups' vaults aren't touched.** They are under their own keys.

## Coming next

One call that seals every secret in the realm's vault again under the newest version: see
[What's next for keys and protection](/secrets-store/keys-and-protection/whats-next/#realm-wide-reseal).

## Related

- [Rotate the secrets key](/secrets-store/guides/rotate-the-secrets-key/): the task, start to finish.
- [Sealing per owner](/secrets-store/keys-and-protection/sealing/): which key seals which vault.
