Storage, encryption and retention

Postgres

Karta keeps everything except message bodies in one Postgres database (Karta.Repo): organisations, users and memberships, servers, domains and DKIM keys, credentials, messages, recipients, delivery attempts, suppressions, tracked links and tracking events, and Oban's job table. Primary keys are UUIDs (version 7 for messages, recipients, deliveries and tracking rows, so they sort by time).

  • Connect with DATABASE_URL; POOL_SIZE connections per node.
  • The first migration runs CREATE EXTENSION IF NOT EXISTS citext.
  • Migrations run with /app/bin/migrate, never at start (Installation).
  • Behind pgbouncer in transaction mode, set KARTA_DATABASE_POOLER=pgbouncer.

The blob store

Message bodies are stored in S3-compatible object storage (Karta.Storage.BlobStore.S3), configured with KARTA_S3_*. Karta signs requests with AWS Signature Version 4 and uses path-style URLs (<endpoint>/<bucket>/<key>); no AWS SDK is involved. Garage, MinIO and AWS S3 work.

  • Objects are named messages/<message id>.
  • Uploads stream the encrypted body with an exact length; an S3 PUT is atomic, so a failed upload leaves nothing. Uploads are not retried by Karta (the client retries the whole message); reads and deletes are retried twice on transient errors. Each request times out after 30 seconds.
  • Give Karta's access key read, write and delete rights on its bucket only.

How a message is stored

A message is acknowledged only when both its body and its database row are stored (Karta.Messages):

  1. a row is inserted with the status pending;
  2. the body, with Karta's Received: field in front (and, for received mail, its Authentication-Results: field above that), is encrypted and uploaded;
  3. one transaction moves the row to accepted and inserts the recipients and delivery jobs.

If any step fails, the client gets a temporary error and what was written is removed at once or by the next retention run. A pending row older than 2 hours (an SMTP connection lives at most 1 hour) is treated as abandoned.

Encryption at rest

Message bodies (Karta.Storage.Cipher): AES-256-GCM over 64 KiB chunks, streamed both ways, so a message is never held whole in memory. Each object has its own key, derived with HKDF-SHA256 from the vault key, a random 32-byte salt and the object's name; the name binding means a stored body cannot be swapped for another message's. Chunk nonces encode the chunk's position and whether it is the last, so truncating, reordering or altering any byte makes decryption fail.

A body is decrypted chunk by chunk as it is used — sent, read by the API, or checked — and each chunk's tag is verified before its plaintext is handed on. A body that was cut short, cut at a chunk boundary, reordered or altered is an error at the chunk where it happens, never a shorter message. The store is read in pieces too: 64 KiB at a time from a local directory, and in ranges (:read_size, 4 MiB by default) from an S3-compatible one, so a message of any size costs the memory of a few pieces.

Secrets in the database (Karta.Vault, Karta.Crypto): DKIM private keys, the TOTP secrets of local login, webhooks' secrets (the private keys of SendGrid's format among them) and the user names and passwords of their URLs, and the key that signs webhooks in Postal's format are encrypted with AES-256-GCM under the vault key, with a fresh 96-bit IV each time. Each value carries the id of the key that encrypted it.

The secrets of endpoints that receive mail, and the user names and passwords of their URLs, are encrypted the same way.

Never stored in a reversible form: API key secrets (Argon2id hashes) and SMTP passwords (SCRAM-SHA-256 keys). See Security model.

The vault key

KARTA_VAULT_KEY is the base64 of exactly 32 random bytes:

openssl rand -base64 32

It is the root of:

  • the encryption of DKIM private keys;
  • the keys of every stored message body;
  • the HMAC keys of tracking tokens, VERP return paths, Mailgun event paging tokens, and the made-up SCRAM salts for unknown user names.

Losing the vault key loses every stored message and every DKIM key. Keep it in your secret store, back it up separately from the database and the bucket, and give it only to Karta.

Rotating the vault key

The vault supports several keys: the active key (KARTA_VAULT_KEY, labelled KARTA_VAULT_KEY_ID, default v1) encrypts and signs everything new; the retired keys (KARTA_VAULT_RETIRED_KEYS, id:base64 pairs separated by commas) still decrypt and verify.

To rotate:

  1. Add the current key to the retired keys under its id, e.g. KARTA_VAULT_RETIRED_KEYS=v1:<old key>.
  2. Set a new KARTA_VAULT_KEY and a new KARTA_VAULT_KEY_ID (e.g. v2).
  3. Deploy. New values use v2; old ones still decrypt under v1.
  4. Re-encrypt the secrets stored in the database (DKIM private keys, TOTP secrets, webhooks' secrets and credentials, the webhook signing keys) under the new key: bin/karta eval 'Karta.Release.reencrypt_secrets()'. It prints how many values of each column it rewrote, and changes nothing in a table with a value that no configured key decrypts.

Keep the retired key for as long as anything made with it matters:

  • Stored messages are never re-encrypted: keep the key until every message stored under it has passed its server's retention (at most 365 days).
  • Tracking links in mail already sent, VERP return paths of mail in flight and paging links keep working while the old key is retired, and stop working once it is removed.
  • DKIM private keys, TOTP secrets and webhooks' secrets and keys need the old key only until step 4 has run.

Retention

Each server keeps messages for retention_days (default 30, at most 365) and at most retention_size_mb of stored bodies (default 2048). The retention process (Karta.Messages.Retention) runs every hour, the first time a minute after start, and:

  1. removes abandoned pending messages and finishes interrupted removals;
  2. removes accepted messages older than the server's retention_days;
  3. removes a server's oldest accepted messages beyond its retention_size_mb.

A message with a recipient still queued or deferred is never removed by steps 2 and 3, however old or large, so queued mail is never lost to retention; it becomes removable when every recipient is delivered or bounced (at most 5 days later).

Removal deletes the blob first, then the row; recipients, delivery attempts, tracked links and tracking events are deleted with the message row. Work is done in batches of 500, at most 100 batches per step and run. Several nodes may run retention at once.

Finished Oban jobs are pruned after 7 days; delivery state lives in its own tables and is kept with the message.

Backups

See Upgrades and backups.