Skip to content

Architecture and security

In one line: Portona hands a bundle of secrets to a workload only when it shows a signed, unexpired, never-before-used workload token, from Amp or GitHub Actions, whose claims match a policy that was reviewed in git, and it stores those secrets only as ciphertext.

Exchange: token in, secrets outAdmin, deploy and cron
Part Where it lives What it holds or does
broker-run In the workload’s Amp orb or GitHub Actions job (client/broker-run.ts) Mints a 60-second Amp token, or uses the job’s GitHub OIDC token, exchanges it, then runs your command with only the secrets you named. Has no dependencies beyond Bun.
Portona Worker Cloudflare Workers src/server.ts routes requests. src/http.ts serves /v1/exchange, src/admin.ts serves the admin page and /admin/api, and scheduled runs the daily cron. TanStack Start renders pages; Effect holds the domain logic.
D1 database Cloudflare secrets (ciphertext, with owner, rotate-by date and notes), federations, environments (the ledger of each name’s ID), data_keys and secrets_v2 (each environment’s wrapped data keys and the values under them, written alongside secrets), audit and used_jti.
R2 bucket Cloudflare backups/YYYY-MM-DD.json: one snapshot of the ledger, the encrypted rows with their details, the wrapped data keys and the data-keyed copies, the federations, and the key ids they need, kept 90 days.
Worker bindings Cloudflare, set at deploy BROKER_MASTER_KEY (a secret), BROKER_MASTER_KEY_PREVIOUS (only during rotation), PORTONA_WRAPPING_KEY (a secret, escrowed; see Wrapping key escrow), PORTONA_ACCESS, PORTONA_POLICIES, PORTONA_ADMIN, BROKER_AUDIENCE, EXCHANGE_RATE_LIMIT and PORTONA_MAINTENANCE (only during recovery).
Cloudflare Access In front of /admin and everything under it Signs admins in.
Workload identity ampcode.com and GitHub Actions Issue the tokens and publish the keys (JWKS) that sign them.
portona repository Public The generic engine. A v* tag builds portona-<version>.tgz, which holds the prebuilt Worker, migrations, portona-assemble, broker-run, portona-restore, portona-ledger and portona-keys.
Deployment repository Yours, usually private access.ts or policies.ts, plus cloudflare.config.ts with the account, names, audiences, D1, R2 and Access settings, portona-recipients.pub with the wrapping key’s recipients, and the escrows in keys/. It pins a release by URL, and its lockfile records the tarball’s hash.
  1. A workload runs broker-run --environment webapp/staging --env TWILIO_AUTH_TOKEN,… -- <command>. broker-run refuses a BROKER_URL that isn’t https (loopback excepted) and any name that isn’t an environment variable name.
  2. In an Amp orb it mints a token with amp orb id-token --audience <this deployment's audience> --subject-scope project --ttl-seconds 60. In a GitHub Actions job with id-token: write, it requests the job’s OIDC token for that audience.
  3. It sends POST /v1/exchange with the token as a bearer and {"bundle": "webapp/staging", "names": ["TWILIO_AUTH_TOKEN", …]} as the body (bundle is the older name for environment, accepted by every version). A redirect is an error, so the token can’t be forwarded elsewhere.
  4. The Worker runs its cheap checks before touching D1:
    • maintenance on: 503;
    • more than 30 requests a minute from one IP: 429;
    • no bearer token: 401;
    • a body over 16 KB, or one that doesn’t decode: exactly one of environment and bundle, and at most 64 environment variable names: 413 or 400.
  5. It verifies the token: signed with RS256 by a key in its issuer’s JWKS (Amp or GitHub Actions); this deployment’s audience; not expired, allowing 60 seconds of clock skew; a lifetime of at most one hour; jti and sub present. GitHub tokens from reusable workflows are refused here. A token that fails its signature is only logged. If the issuer’s keys can’t be fetched, the answer is 503, never a pass.
  6. It records the token’s jti in used_jti. The primary key makes a second use fail, even when two requests race. The row is kept until the token’s expiry plus the skew.
  7. It collects the environment’s grants whose principal the token matches exactly and which haven’t expired; they add up. Each requested name must be allowed by one of them and must exist; with no names, one must cover the whole environment. Otherwise the whole request is refused before anything is decrypted. An environment with an ID must then agree with the ledger, which records the first ID seen for each name, so a renamed or recreated environment can’t reach another’s secrets. An unknown environment, a mismatch, an ungranted name, a missing name and a ledger conflict all get the same 403 (see Environments and grants).
  8. It decrypts only the allowed secrets and writes an ok audit row with their names, the environment’s ID, the issuer and the grants used. It returns {secrets, expiresAt} with Cache-Control: no-store. expiresAt is when the workload’s token expires, not the secrets: Portona can’t expire a value once it is delivered.
  9. broker-run checks every --env name has a non-empty value, and stops if one is missing. It starts the command with only those names added, forwards signals and passes the exit code through. It never prints the values or writes them to disk.
  1. The admin opens /admin. Cloudflare Access signs them in, then adds its signed token to every request it lets through.
  2. The Worker checks that token itself: the signature against the team’s keys, the issuer, the application’s audience tag, the expiry, and that the email (lowercased) is on PORTONA_ADMIN’s allowlist.
  3. Changes also need an X-Broker-Request: 1 header and a same-origin Origin, and maintenance must be off.
  4. Setting a secret needs a configured environment, a name shaped like an environment variable, and a value of at most 16 KB. The write is a compare-and-swap on the version. Its audit row, with the admin’s email as actor, commits in the same batch.
  5. Values are typed into password fields and never sent back: the API returns names, versions, key ids and who updated them.
  1. Edit access.ts (or the older policies.ts) in the deployment repository. bun check fails on a misspelt claim or field, because both are typed against the engine’s schema.
  2. portona-assemble validates the access configuration and admin config strictly (an unknown key is an error) and wraps them around the prebuilt Worker.
  3. Deploy staging, then production, with cf deploy --prebuilt, ideally under a scoped OAuth login rather than a long-lived Cloudflare API token.
  4. Engine changes follow the same path from the other side: a v* tag runs CI and publishes the tarball, and the deployment repository moves to its URL in a reviewed commit.

The cron backs up a snapshot of the database, still encrypted, to R2, then deletes backups, audit rows and expired jti rows past their retention. Each task runs even if another fails, and any failure fails the run, so it shows in the Worker’s logs.

Set a new BROKER_MASTER_KEY with the old one as BROKER_MASTER_KEY_PREVIOUS, re-encrypt from the admin page, then remove the previous key. See Rotating the master key.

portona-keys init generates the wrapping key, encrypts it to each RSA public key in the deployment repository’s portona-recipients.pub on origin/main, commits and pushes that escrow to keys/, and only then sets the Worker secret. Any one recipient recovers it with their private key and portona-keys recover. See Wrapping key escrow, which has the diagram.

Turn maintenance on, restore a backup or roll back with D1 Time Travel, keep exchanges closed for 61 minutes, then turn maintenance off. See Backups and recovery.

How Portona protects secrets, layer by layer

Section titled “How Portona protects secrets, layer by layer”
Layer Protection Where
Edge HTTPS only. 30 exchanges a minute per IP, checked before any work. cloudflare.config.ts, src/http.ts
Routing Only /, /healthz and /v1/exchange are public. Every other path, in any spelling, goes through the admin check. Paths with % or // get 400, and /_serverFn gets 404. There are no TanStack server functions. src/server.ts
Identity Token checks for each built-in issuer (Amp, GitHub Actions): RS256 only, issuer, audience, expiry with 60 s skew, at most a one-hour lifetime, required claims. A JWKS outage fails closed. src/token.ts
Environment isolation Each deployment and environment has its own audience, D1, R2 and master key. A staging token is refused by production. Deployment repository
Environment ledger Secrets are stored by name, so the ledger pins each name to the first ID it was used with. A renamed or recreated environment is refused, and the daily cron fails naming it, until the name is retired. Retiring deletes the old secrets in one audited transaction, and restores skip them. src/ledger.ts
Replay Every verified token works once, whether or not its policy matches. claimJti in src/store.ts
Authorisation Exact matches on immutable ids, and a token never matches another issuer’s policy. An Amp policy names user_id, project_id and token_use: exchanged; a GitHub Actions policy names the repository and owner ids, ref, environment, workflow_ref, and an event of push or workflow_dispatch. Never email or workspace_id. Strict decoding at assembly and at runtime, and invalid policies fail every exchange closed. Policies change only through a deploy. src/config.ts, src/policies.ts
Disclosure Every refusal, including an unknown bundle, gets the identical 403. Errors return a 5xx without detail. Responses aren’t cached. src/http.ts
Storage AES-256-GCM with a fresh 12-byte IV per write. The bundle, name and version are bound in as authenticated data, so a ciphertext copied to another row won’t decrypt. Each row records its key id. Values are capped at 16 KB. D1 and R2 hold only ciphertext. With PORTONA_WRAPPING_KEY set, each value is also stored under its environment’s own data key, wrapped with authenticated data naming the deployment’s audience, the environment ID and the key’s version, and created only for an ID the ledger holds live. src/crypto.ts
Keys The master key is a Worker secret, which Cloudflare’s API never returns. Rotation runs two keys at most, and re-encryption can’t overwrite a newer write. portona-keys init pushes the wrapping key’s escrow, bound by an OAEP label to its account, Worker and key id, before the Worker gets it, and never replaces one; a wrapping key equal to a master key is refused. src/crypto.ts, src/store.ts, bin/portona-keys.ts
Admin Two locks: Access at the edge, then the Worker’s own check of Access’s token and the email allowlist. Admin is off without PORTONA_ADMIN, and fails closed if that is invalid. CSRF header and Origin checks on changes. Values are write-only. src/access.ts, src/admin.ts
Audit One row for each attempt with a validly signed token, and one for each admin change, with names and never values. Kept 90 days. Unsigned attempts are only logged, so a flood can’t fill D1. Bodies are capped while they’re read. src/exchange.ts, src/store.ts
Operations The maintenance switch closes exchanges, admin changes and the daily cron without writing to D1. The Worker never logs values. src/http.ts, src/admin.ts
Supply chain Exact pinned versions of Bun, cf and Effect. A deployment’s lockfile pins the release tarball’s hash. No deployment-specific values in the public repository. broker-run has no dependencies to compromise. portona/package.json, bun.lock
Verification Refusal cases were written first, and each safeguard was checked by breaking it and watching a test fail. portona/test/
  • Nothing to steal at rest. No long-lived secret is stored in Amp, the repository, a dotenv file or the orb. A value exists only in the command’s environment, for that run.
  • Narrow, short-lived access. A bundle serves one job. Its token lives 60 seconds and works once. A token from any other project or user gets nothing.
  • Only what was asked for. The command receives exactly the --env names. The broker can’t set PATH or anything else, and a missing name stops the run before the command starts.
  • One place to rotate and revoke. Replace a value on the admin page, and the next exchange gets it. Delete a secret, or remove a policy and deploy, to cut off a job. Maintenance cuts off everything.
  • A record of every use. Each exchange is audited with the actor and Amp thread id, and each change with the admin’s email.
  • The command itself. Once delivered, the command and anything it starts can read, print or send the values elsewhere. Run only trusted commands, and don’t log their environment.
  • The orb’s user and project. Anyone who can run code as the policy’s user in that project’s orbs can mint a token and fetch the bundle. Policies pin the user and project, not a specific thread or command. This is the main trust boundary.
  • The issuers as identity roots. If Amp’s or GitHub’s signing keys were compromised, forged tokens would pass. An outage of an issuer’s JWKS fails closed.
  • The Cloudflare account. Anyone who can deploy Workers there could ship a modified Worker that leaks values on their next use, even though the master key can’t be read back. Keep account membership small, with 2FA.
  • A lost master key. Without it, every secret and backup is unrecoverable. Save it, with its key id, before the first secret is stored.
  • The wrapping key’s recipients. Anyone holding a recipient’s private key and the escrow file can recover the wrapping key, and whoever can add a line to portona-recipients.pub on origin/main before a key is created becomes a recipient. Review that file as you review access configuration.
  • Metadata. Bundle names, secret names, versions, actors and thread ids are stored in plain text. Only values are encrypted.
  • Distributed floods. The rate limit is per IP. Without a signed token, though, an attacker can’t write to D1.