Rotating the encryption key (self-hosting)

For whoever runs an Indrava server, how to replace ENCRYPTION_KEY without losing connected channels, using pnpm reseal.

Embed
Preview of the card
<a href="https://indrava.forumulate.com/help/rotating-the-encryption-key"><img src="https://indrava.forumulate.com/help/_api/public/orgs/e421ace4-3e78-4ed8-9818-7b4899cf3aa0/spaces/eb86896a-9649-48ed-a1a6-566b48188df7/pages/952e4f34-5da6-4584-91c0-38dd5ef8cc2f/social-card.svg?v=3e7ae33eec8b" alt="Rotating the encryption key (self-hosting)" width="600" height="315"></a>

This page is for whoever runs an Indrava server. On a server somebody else runs for you there is nothing to do.

ENCRYPTION_KEY seals what Indrava stores about your channels (the tokens the platforms gave it) and hashes the tokens of review, report and connect links. Replace it when it may have leaked, or on your own schedule. Done in the order below, no channel has to be reconnected and the server does not go down beyond one restart.

Steps

  1. Make a new key: openssl rand -hex 32.

  2. Set it as ENCRYPTION_KEY, and put the old key in ENCRYPTION_KEY_PREVIOUS. If older keys are already listed there, keep them, comma-separated, newest first. The api and the worker must have the same values.

  3. Restart the api and the worker together.

  4. Re-seal the stored channel credentials with the new key. From the repository, with both keys in .env:

    pnpm reseal

    In the api container, where pnpm is not installed, run the same script directly: node --env-file=.env scripts/reseal.ts.

  5. It prints one line: re-sealed 12, already current 0, unreadable 0. Run it again until it says re-sealed 0 and unreadable 0.

  6. Remove the old key from ENCRYPTION_KEY_PREVIOUS and restart.

What pnpm reseal does

It opens each stored channel credential with whichever listed key fits and seals it again with the new one. It works in batches, can run while the api and the worker are running, and can be run any number of times. It prints counts and channel ids, never a key or a token.

A channel it lists as unreadable was sealed with a key that is no longer listed anywhere. Put that key back in ENCRYPTION_KEY_PREVIOUS and run it again, or reconnect the channel on Channels.

Between the restart and the last step

  • New values are sealed, and new links hashed, with the new key at once.

  • Channels keep publishing: a credential not yet re-sealed still opens with the old key.

  • A review, report or connect link made before the rotation still works, and moves to the new key the first time somebody opens it.

  • With the local storage driver, a signed media download link made before the restart works until it runs out (15 minutes).

What does not survive removing the old key

Links cannot be re-hashed in bulk: only the token gives the new hash, and Indrava does not store tokens. A review or report link that nobody opened between the rotation and step 6 stops working when the old key is removed. Make a new link for it. To avoid this, leave the old key listed until the links you care about have been opened or have expired.

Do not remove the old key before step 5 is clean: a credential still sealed with it could no longer be opened, and its channel would have to be reconnected.

Updated