Effect key rotation

How to replace PROCESS_EFFECT_ENCRYPTION_KEY and PROCESS_EFFECT_DERIVATION_KEY without making stored protected records unreadable, and what each key actually protects.

What the keys protect

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.

  • Encryption key. Seals and opens the envelopes. Replacing it in place makes every existing envelope unreadable, so it is rotated through a keyring: the new key seals under a new key id while the old key stays readable until every envelope has been resealed.
  • Derivation key. An HMAC key that turns (process id, effect occurrence, slot) into the handle 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.

Variables

VariableDuring rotationAfter
PROCESS_EFFECT_ENCRYPTION_KEYnew keynew key
PROCESS_EFFECT_ENCRYPTION_KEY_IDa new id, for example r20261022unchanged
PROCESS_EFFECT_ENCRYPTION_KEY_PREVIOUSthe old keyremoved
PROCESS_EFFECT_ENCRYPTION_KEY_PREVIOUS_IDunset (defaults to active, the id every record sealed so far carries), or the previous rotation's idremoved
PROCESS_EFFECT_DERIVATION_KEYnew keynew key
PROCESS_EFFECT_DERIVATION_KEY_PREVIOUSthe old keyremoved

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.

Order of operations

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

Where it can fail

  • During the step 3 deploy. An old container (old variables) can briefly overlap a new one, and the web and worker services can restart at different moments. Anything a new container seals in that gap cannot be read by an old one, so the step using it fails closed with 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.
  • Retiring the previous key too early. If an old container sealed a record under active after --apply ran, removing the previous key makes that record unreadable. The re-run of --apply and --verify in step 5 catches this.
  • Records the old key does not open. --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.

Rollback

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.