How to replace PROCESS_EFFECT_ENCRYPTION_KEY and PROCESS_EFFECT_DERIVATION_KEY without making stored protected records unreadable, and what each key actually protects.
Signing, protected derivation and credential capture store their results (signatures, signed raw transactions, captured access tokens) as AES-256-GCM envelopes in the Mongo collection protected-effects-v1 (or, with STORAGE_DRIVER=postgres, the engine.protected_effects table), one record per handle. Each envelope names the key that sealed it (keyId), and its authenticated data binds the owning process id and the handle. Static provider credentials (DUNE_API_KEY, SLACK_BOT_TOKEN and so on) are never stored there. The default policies give every stored value a lifetime of at most one hour.
v1.<hmac>. Handles are stored, in protected-effects-v1 and in process state, and are always looked up as stored, never recomputed, so rotating this key needs no data migration and every issued handle keeps working. The one thing that changes: an effect occurrence that minted a value before the switch and is replayed after it gets a fresh handle and record instead of its original one. PROCESS_EFFECT_DERIVATION_KEY_PREVIOUS keeps that replay identity during the changeover.Re-encrypting protects records from the old key from now on. It does not take back anything copied while the old key was exposed.
| Variable | During rotation | After |
|---|---|---|
PROCESS_EFFECT_ENCRYPTION_KEY | new key | new key |
PROCESS_EFFECT_ENCRYPTION_KEY_ID | a new id, for example r20261022 | unchanged |
PROCESS_EFFECT_ENCRYPTION_KEY_PREVIOUS | the old key | removed |
PROCESS_EFFECT_ENCRYPTION_KEY_PREVIOUS_ID | unset (defaults to active, the id every record sealed so far carries), or the previous rotation's id | removed |
PROCESS_EFFECT_DERIVATION_KEY | new key | new key |
PROCESS_EFFECT_DERIVATION_KEY_PREVIOUS | the old key | removed |
The app refuses to start if the previous key id equals the new id, if a previous key equals the new key, if an encryption key equals a derivation key, or if a key is not 32 bytes of base64. npm run check:deployment reports the same problems by field.
Deploy the code first, with no variable changes. With no new variables the keyring is exactly what it was (one key, id active). Do not set the new variables on a build without this keyring: it would seal under active with the new key and stop reading every existing record.
Generate the new keys on a trusted machine: openssl rand -base64 32, twice. Store them in the password manager. Never paste them into chat or logs.
Set the rotation variables from the table on every service that has the effect keys (the worker, and any web service that has signing configured), in one staged change, and deploy. From now on new records are sealed under the new id and both keys can be read.
Reseal, from a shell inside a service that has the variables:
railway ssh --service <service> npm run effects:rotate-key -- --dry-run # counts only; expect 0 unreadable, 0 unknown key id npm run effects:rotate-key -- --apply # reseals each record with a compare-and-swap npm run effects:rotate-key -- --verify # every record opens with the new key, none with the old
The command reads the same variables as the app, plus MONGO_URL (or, with STORAGE_DRIVER=postgres, DATABASE_URL: it reseals the engine.protected_effects table instead). It never prints key bytes, plaintext or the database credentials. Exit code 0 means OK, 1 means some records need attention (listed by handle), and 2 means it refused before touching storage. Re-running --apply after it succeeds changes nothing.
Retire the old keys at least one hour after step 3, once no protected step that started before step 3 is still running. Re-run --apply and then --verify right before this, and continue only if verify prints OK. Then remove PROCESS_EFFECT_ENCRYPTION_KEY_PREVIOUS, PROCESS_EFFECT_ENCRYPTION_KEY_PREVIOUS_ID and PROCESS_EFFECT_DERIVATION_KEY_PREVIOUS and deploy.
Confirm with npm run effects:rotate-key -- --verify once more. With the previous key gone it checks that every record opens with the new key.
protected-store before dispatching anything. To avoid this, run step 3 when no Safe or Sheets step is in progress, and deploy every keyed service together.active after --apply ran, removing the previous key makes that record unreadable. The re-run of --apply and --verify in step 5 catches this.--dry-run and --apply list them as unreadable and never modify them. Investigate before step 5: once the previous key is removed they can never be opened.Before step 5, roll back by swapping roles rather than deleting variables: set PROCESS_EFFECT_ENCRYPTION_KEY to the old key with id active, and PROCESS_EFFECT_ENCRYPTION_KEY_PREVIOUS to the new key with PROCESS_EFFECT_ENCRYPTION_KEY_PREVIOUS_ID set to the new id. Both sets of records stay readable. After --apply, the same command with the roles swapped reseals everything back.
File-backed stores (STORAGE_DRIVER=file) cannot be resealed: their file names are one-way digests of the handles, which are part of each envelope's authenticated data. For those stores, keep the previous key in the keyring.