TOKEN_ENCRYPTION_KEY rotation. Adding a new key makes new
writes use it; reencrypt rewrites the rows still sealed under the old one.
Until it reports nothing left, the old key has to stay in
TOKEN_ENCRYPTION_KEYS_PREVIOUS.
The same pass rewrites ciphertext stored before values were
bound to their row, including rows
already on the current key. A deployment that has never rotated a key still has
that work to do once.
Synopsis
trustowl-reencrypt; in the image it is installed as /app/reencrypt.
Subcommands
apply is idempotent. A row already on the current version and bound is skipped
rather than re-sealed, so a second run rewrites nothing, and an interrupted run is
finished by running it again.
plan proves the keys work before anything is written: it decrypts with the
old key and seals with the new one, and discards the result. It reads a single
batch of 500 rows per column (per organization, for the tenant tables), so on a
large table its counts are a floor rather than a total. report has the exact
counts.
What it sweeps
Eight columns, each opened and re-sealed with the key derived for its own purpose:
Queued mail and notifications (
mail_outbox.payload_encrypted) are not swept.
A queued message is delivered or abandoned within the dispatcher’s retry
schedule, so there is no standing backlog to rewrite.
The role it must run as
The sweep reads every organization’s rows. Row-level security does not raise an error when it hides a row; it returns nothing. A role the policies filter would therefore count zero rows on the old key and tell you the key is safe to delete.reencrypt refuses to start in that position. Before report, plan or
apply does anything, it checks that the connected role is one of:
- a superuser or a role with
BYPASSRLS, or - a role covered by the
tenant_isolation_maintenancepolicy, which the migrations create for the role that ran them.
DATABASE_URL that migrate uses, not the
application’s login role. With the application role it exits non-zero and names
the role.
Environment it reads
reencrypt loads the full API configuration, so that the keys it uses are
exactly the keys the application uses. That means it needs everything the API
needs to start, including JWT_SECRET outside development, and it fails on
the same invalid settings.
DB_STATEMENT_TIMEOUT and DB_IDLE_IN_TRANSACTION_TIMEOUT are ignored here.
A sweep over every organization legitimately runs longer than a request may, so
the command opens its pool with both bounds removed.
Reading the output
report prints one line per column and version:
every row is on the current key: previous keys can now be retired.
When no row is unbound it says every row is bound to its record: SECRETS_REQUIRE_BOUND can be switched on. A database holding no ciphertext at
all prints no stored ciphertext.
plan and apply print one line per swept column:
UNREADABLE is the column to watch. It counts rows the sweep could not
open. Those rows are left exactly as they are, never rewritten and never
deleted, and the command exits non-zero. There are three causes:
- No key this process holds opens the row: a key was retired before the sweep
ran. Restore it to
TOKEN_ENCRYPTION_KEYS_PREVIOUSand run again. - The ciphertext was sealed for a different row. It does not belong where it is. Find out how it got there before doing anything else.
SECRETS_REQUIRE_BOUNDis on and the row is in the unbound format.
Running it
- Production Compose
- Published image
- From source
The shipped file has a The service receives only the variables the compose file names. The two
rotation variables are among them;
reencrypt service in the maintenance profile. It
never starts with up, and it connects as the owner through
MIGRATION_DATABASE_URL:SECRETS_REQUIRE_BOUND is not, at this
release. See
Production Compose.Exit status
report exiting 0 is not the signal that a key can be retired. Its last line
is.
Related
- Rotate the encryption key: the procedure this command is one step of.
migrate: the other command that runs as the schema owner.- Environment variables: the rotation variables and what startup refuses.