TOKEN_ENCRYPTION_KEY encrypts every secret TRUSTIVAN has to hand back to a
third party: linked provider tokens, webhook signing secrets, single sign-on
client secrets, TOTP seeds, stored registry, git, cloud and Kubernetes
credentials, and notification destination secrets. This page is the procedure
for replacing it.
How rotation works
Each ciphertext is stored with the version of the key that sealed it, in its own column. Decryption looks up the key for the version the row names; encryption always uses the current one. So rotation is four states, and only the last can lose data:
Three variables carry it:
A deployment that has never rotated sets only the first, and stays on version 1
with no change in behaviour.
Before you start
- Have the current key somewhere other than the running deployment. You are about to move it into a second variable, and a typo there is recoverable only if you still have the original.
- Take a database backup. See Backup and recovery.
- Know how your deployment passes variables. The production Compose file
passes
TOKEN_ENCRYPTION_KEY_VERSIONandTOKEN_ENCRYPTION_KEYS_PREVIOUStoapi,workerandreencryptfrom the environment file. Before 2026-10-13 it did not, and an override file was needed; see Production Compose. - Rehearse on a restored copy. The keyring and the sweep are covered by the repository’s automated tests. The project’s own limitation notes record that the procedure has not yet been exercised against a live deployment, so run it once against a restore of your database before you run it against the real one.
Procedure
1
Generate the new key
2
Add it as the current key and keep the old one
On every backend process (each API replica, each worker, and wherever
you run Then restart them. New writes now use version 2 and old rows still open.
Nothing is broken, and nothing is finished.Workers need the same values as the API: a worker opens stored credentials
to pull private images and clone private repositories.
reencrypt), set all three variables. For a first rotation, from
version 1 to version 2:3
Confirm the processes started
Startup refuses a keyring that would lose data, so a process that is
running has a complete one. It exits with a named error when:
TOKEN_ENCRYPTION_KEY_VERSIONis less than 1;- the version is above 1 and any version below it is missing from
TOKEN_ENCRYPTION_KEYS_PREVIOUS; TOKEN_ENCRYPTION_KEYS_PREVIOUScontains the current version;- an entry is not
version:hexkey, names a version twice, is not hex, or does not decode to 32 bytes.
4
See what is still on the old key
Run Rows marked
reencrypt report as the schema owner, the
same role migrate uses:<- not yet re-encrypted are the work.5
Dry-run, then apply
plan writes nothing. It proves the old key opens the rows and the new one
seals them. An UNREADABLE count above zero usually means a key is missing
from TOKEN_ENCRYPTION_KEYS_PREVIOUS; fix that before apply. The
command reference lists the
other causes.apply can run while the deployment serves traffic. It works in batches of
500 rows, updates the ciphertext and its version in one statement, and
leaves alone any row another writer has already moved. If it is
interrupted, run it again.6
Verify
7
Retire the old key
Remove the old entry from
TOKEN_ENCRYPTION_KEYS_PREVIOUS and restart.
Leave TOKEN_ENCRYPTION_KEY_VERSION at its new value. Keep the retired key
in your secret manager for as long as you keep database backups taken
before the sweep: a restore of one of those brings back rows only that key
can open.report.
Row binding
A ciphertext is sealed to the row that holds it. The organization id (where the table has one), the row’s id and the secret’s purpose are passed to AES-GCM as associated data, so they are authenticated without being stored. A value copied from one row into another, in the same column or a different organization, fails to open exactly as a tampered one does. Bound ciphertext starts withv2:.
Ciphertext written before this existed has no binding and no prefix. It still
opens, and reencrypt apply rewrites it in the bound form whether or not you
are rotating a key: a row on the current key in the old format is work for the
sweep too. report counts those rows in its UNBOUND column.
What stays open until you close it. While the old format is accepted, an
old-format value (from before the sweep, or from a backup taken before it) can
still be copied between rows of the same column. Setting
SECRETS_REQUIRE_BOUND=true on every backend process makes them refuse
old-format ciphertext. Do that only when report ends with:
- A row still in the old format stops opening. With the switch on,
applycounts such a row asUNREADABLEand does not upgrade it, because upgrading it would turn a copied value into a valid one. - Restoring a backup taken before the sweep brings old-format rows back. Switch
it off, run
apply, checkreport, switch it on again. reportdoes not count queued notifications and mail. Those rows are sealed to their own id from the moment this version is running, and the ones queued earlier are delivered or abandoned within the retry schedule. A message queued before the upgrade and still pending when you set the switch is failed permanently, so leave a day between upgrading and switching it on.
SECRETS_REQUIRE_BOUND is not passed by the
production Compose file at this release. Add it with an override file, as
Production Compose shows.
What else changes with the key
Two keys are derived from the currentTOKEN_ENCRYPTION_KEY and are not
versioned. They change the moment the new key becomes current, whatever the
sweep does:
Plan the rotation with the second one in mind if your teams have triaged a
large number of secret findings.
JWT_SECRET is a separate secret with its own two-step rotation. See
Environment variables.
What this does not do
- No automatic or scheduled rotation. Every step above is something an operator runs.
- This key is not the only layer. Organization secrets are sealed under per-organization data keys that a KMS (AWS KMS, Cloud KMS, Vault Transit) can wrap instead of this key; see Envelope encryption.
- No recovery of a lost key. Rotation lets you replace a key you still hold. Ciphertext sealed under a key nobody has is not recoverable, and each affected integration has to be reconnected by the customer.
Related
reencrypt: the command’s subcommands, output and exit status.- Credentials: which secrets are encrypted and which are only hashed.
- Upgrades: restarting processes in the right order.