Skip to content

Wrapping key escrow

A deployment’s wrapping key, PORTONA_WRAPPING_KEY, wraps each environment’s own data key. Like the master key, it’s a Worker secret that Cloudflare never returns, so a copy has to live somewhere else, or the secrets stored under it can’t be recovered.

portona-keys keeps that copy as an escrow: the key, encrypted to each of your team’s RSA SSH public keys, committed to the deployment repository. Creating the key needs no password manager, only a cf login that can write Workers and push access to the deployment repository, so an agent can do it. Opening the escrow needs one recipient’s private key, which never leaves their password manager.

Portona has three kinds of key. Before v0.6, one master key encrypted every secret in a deployment, and secrets were filed under their environment’s name, so renaming an environment stranded its secrets and recreating a name could hand the old secrets to a new environment. Data keys fix both: each environment gets its own key, bound to its ID.

Key What it encrypts Where it lives If it’s lost
Master key, BROKER_MASTER_KEY Every secret in the secrets table, which exchanges read today Worker secret; you keep a copy with its key id Every master-key row and backup is unreadable
Wrapping key, PORTONA_WRAPPING_KEY Only the data keys, never a secret directly Worker secret; escrowed in the deployment repository Every data key it wrapped, and so every value stored under them, is unreadable
Data key, one per environment That environment’s secrets, in secrets_v2 D1 data_keys, only ever stored wrapped Created on the first write; the wrapping key is what has to survive
  • A data key is random and never leaves the Worker unwrapped. The Worker creates it on the first admin write to an environment the ledger holds live, and stores it wrapped with AES-256-GCM. The wrap’s authenticated data names the deployment’s audience, the environment ID, the key’s version and the wrapping key’s id, so a wrapped key copied to another environment, version or deployment won’t unwrap.
  • Each data-keyed secret is bound to its environment’s ID, with its name and version, as authenticated data, so a ciphertext copied to another row or environment won’t decrypt.
  • The wrapping key is one small secret that guards many keys. It stays the same while data keys come and go, which is why it’s worth escrowing carefully. It must differ from both master keys; the Worker refuses one that matches.

Data keys are being introduced in phases, so any earlier release keeps working on the same database:

  • Since v0.6.0: with the wrapping key set, every admin change to a PORTONA_ACCESS environment writes the value both ways in one transaction: under the master key and under the environment’s data key.
  • Since v0.6.3, backfill: the Keys view’s Backfill data keys copies existing secrets of environments the ledger holds, a pass at a time, and never overwrites a change made while it runs. Secrets of PORTONA_POLICIES bundles and of names no longer configured are quarantined: they’re never copied until their environment moves to PORTONA_ACCESS with an ID. Once every secret of an environment is copied, its exchanges read the data-keyed copies, but only while a copy matches its master-key row’s version and IV; otherwise, and if the wrapping key or a copy fails, they read the master-key row. Writes stay dual, and the master-key rows stay complete.
  • Since v0.6.4: backups hold the data keys, still wrapped, and the data-keyed copies, and name the wrapping keys they need. Restoring a copy needs its wrapping key, so keep each escrow for as long as a backup names its key.
  • Next: a later release makes the data-keyed rows the only copy. From then on, losing the wrapping key loses the secrets, which is why its escrow comes first.
Once per recipientportona-keys initportona-keys recover
  1. Each recipient adds a public key to portona-recipients.pub, once, in a reviewed commit.
  2. portona-keys init reads the recipients from origin/main and generates the key in memory.
  3. It encrypts the key to each recipient, then commits and pushes the escrow file. If anything fails, it stops.
  4. Only once origin/main holds the escrow does it set the Worker secret.

portona-recipients.pub sits at the root of the deployment repository, with one public key per line, as in authorized_keys. Lines starting with # are comments.

  • RSA, 3072 bits or more. Ed25519 keys, the usual default, can only sign, so they can’t receive an encrypted key, and portona-keys refuses them along with shorter RSA keys.
  • Generated in the password manager. In 1Password, on a computer or a phone, create a new SSH Key item, choose Generate New Key, pick RSA and set 4096 bits. The private key stays in 1Password; copy only the public key, which starts with ssh-rsa. The item’s SHA256: fingerprint matches the escrow file’s.
  • Two or more keys, held by different people. init refuses one recipient unless you pass --single-recipient. Any one recipient can recover the key alone: this is 1-of-N, not a quorum.
  • Dedicated to escrow. Use a key that does nothing else, not your Git or server key.
  • Reviewed like access configuration. Whoever can add a line before a key is created can read that key. Changes to this file need the owner’s review, as access.ts does.

From the root of a clean checkout of the deployment repository at origin/main, with a cf login that can write Workers and permission to push to main:

Terminal window
bunx --no-install portona-keys init --mode staging

In order, it:

  1. refuses if the checkout has local changes or isn’t exactly origin/main, so the recipients and config it uses are the reviewed ones;
  2. reads portona-recipients.pub from origin/main, and refuses a weak, malformed, duplicate or signing-only key;
  3. refuses if the Worker already has a wrapping key, reading cf’s list of secret names and failing closed if it can’t;
  4. writes the escrow to keys/<worker>-wrapping-<key id>.json, commits only that file and pushes it. A rejected push, such as one racing another init, stops it;
  5. checks that origin/main holds the escrow byte for byte, and that the Worker still has no wrapping key;
  6. sets the Worker secret, passing the key to cf on stdin, which deploys a new version.

It prints the key id, the Worker and the escrow file’s path, and never relays cf’s output. If cf fails at step 6, the Worker may or may not have the key: keep the escrow, which is already pushed, and check with cf workers secrets list.

The escrow file holds only ciphertext and fingerprints:

{
"format": "portona-escrow/2",
"account": "<Cloudflare account id>",
"worker": "portona-staging",
"binding": "PORTONA_WRAPPING_KEY",
"key_id": "<first 16 hex digits of the key's SHA-256>",
"created_at": "<when it was created>",
"recipients": [
{
"fingerprint": "SHA256:<as ssh-keygen -lf and 1Password show it>",
"comment": "<the public key's comment>",
"alg": "RSA-OAEP-256",
"ciphertext": "<base64>"
}
]
}

Each ciphertext is encrypted with an OAEP label naming the format, account, Worker, binding and key id, one per line. An escrow relabelled for another Worker or key won’t decrypt. Escrows from v0.6.1 (portona-escrow/1) have no label and no account, and still open.

Each recipient runs a recovery drill once after a key is created, and whenever they change devices. It decrypts the key and prints only its id. Compare that with the id on the admin page’s Keys view: the escrow file names a key id, but only the Worker shows which key is live. Run it on a machine you trust, because the private key leaves 1Password for the pipe:

Terminal window
op read "op://<vault>/<item>/private key" \
| bunx --no-install portona-keys recover keys/<file>.json --check

recover reads an unencrypted RSA private key on stdin, as PKCS#8 (-----BEGIN PRIVATE KEY-----), the form 1Password exports, or PKCS#1 (-----BEGIN RSA PRIVATE KEY-----). OpenSSH-format and passphrase-protected keys are refused. If op read doesn’t give you PEM, export the private key from the 1Password desktop app instead. recover finds the entry matching the key’s fingerprint, refuses a key that isn’t a recipient, and checks that the key it decrypts has the file’s key id. The drill proves the escrow opens; only the Keys view proves it’s the key the Worker uses.

To put the key back on a Worker that lost it, pipe it straight into cf. Without --check, recover writes the key to stdout, and refuses if stdout is a terminal:

Terminal window
op read "op://<vault>/<item>/private key" \
| bunx --no-install portona-keys recover keys/<file>.json \
| jq -Rn '{PORTONA_WRAPPING_KEY: {type: "secret_text", text: input}}' \
| bunx --no-install cf workers secrets bulk-edit --worker <worker> --secrets @/dev/stdin

Put back only the key the escrow names, and check its id matches the one you expect from the Worker’s history, not only the file. A different key would strand every data key the old one wrapped.

Recovery doesn’t depend on Portona. Any recipient can open their entry with OpenSSL, given the label as hex. OpenSSL returns the key’s 32 raw bytes; the final base64 turns them into the Worker secret’s form:

Terminal window
f=keys/<file>.json
label=$(jq -j '[.format, .account, .worker, .binding, .key_id] | join("\n")' $f | xxd -p | tr -d '\n')
jq -r '.recipients[0].ciphertext' $f | base64 -d \
| openssl pkeyutl -decrypt -inkey <private key file> \
-pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_md:sha256 -pkeyopt rsa_mgf1_md:sha256 \
-pkeyopt rsa_oaep_label:$label \
| base64

For a portona-escrow/1 file, leave out the label option.

  • Recipients are fixed when the key is created. Adding someone to portona-recipients.pub covers the next key, not the current one. portona-keys can’t yet re-escrow an existing key.
  • Removing a recipient doesn’t revoke them. The escrow file stays in the repository’s history, so a removed recipient’s private key still opens it. Treat their leaving like a lost key: the fix is a new wrapping key, which needs the data-key rotation that Portona doesn’t have yet.
  • origin/main is the review. init trusts whatever is on origin/main, so protect that branch: a recipient pushed there without review becomes a reader of the next key.
  • Concurrent runs are narrowed, not excluded. Git refuses the second of two racing pushes, and init checks the Worker again just before setting the key. Cloudflare has no conditional write, so a run started by other means in that last moment could still replace it. Run one at a time.
  • The machine that runs init holds the key in memory, as does cf. The cf login could set any Worker secret anyway, and init trusts the installed cf and cloudflare.config.ts. Run it where you’d run a deploy.
  • The recovery machine sees the private key. It leaves 1Password for the pipe. Run recovery only on a machine you trust, and send the key without --check only into cf.