Skip to content

Seal runtime values with a versioned keyring - #428

Merged
tnkuehne merged 14 commits into
mainfrom
feat/runtime-keyring
Nov 27, 2026
Merged

tnkuehne merged 14 commits into
mainfrom
feat/runtime-keyring

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

The runtime encrypts what it stores itself (app secret snapshots, the keys behind WIDEFLEET_KEYS, the workload signing ring) and the managed OAuth values that clients hold. It used one raw key, WIDEFLEET_SECRETS_KEY, in a format that names no key version, so the key could not be rotated: replacing it made every stored value unreadable, including app keys that exist nowhere else.

Changes

  • Keyring. WIDEFLEET_SECRETS_KEYS is a JWK Set in the control plane's keyring format, active version first. The format helpers move from secret-encryption.ts to @platform/contracts/keyring, so both share one parser.
  • Format. Values are compact JWE, dir + A256GCM, with the key version as kid and the value's object path or purpose as a protected ctx. Opening requires the context the reader expects, so a value cannot be moved to another object or purpose.
  • Bearer values. Managed OAuth access placeholders, refresh grants and state tickets open only under the active version, so a new active version retires them.
  • App keys. A key's exact 32 bytes are sealed with its full path as context, so existing app ciphertexts and signatures stay valid.
  • Rewrite. The runtime management endpoint /.well-known/widefleet/rekey seals stored values again under the active version, one bounded page per request. It opens every value, also one that already names the active version, and writes only if the object is still the one it read; deleted objects stay deleted and no app key is created. Whatever deploys the runtime drives the pages in one pass once the runtime is live: deploy-hosted at the end of a deployment, after waiting for the runtime to report the keyring's active key version (a keyring change can keep the release), and the agent after each committed Runtime job, whose message reports the counts. deploy-hosted exits nonzero and the job message says so if a value was unreadable or changed during the pass. New values are always sealed under the active version, so a pass without either leaves every value under it. The earlier Ed25519 ring at workload/signing-keys.json is not read and is left as it is.
  • Earlier values. legacy-runtime-ciphertext.ts reads values written before the keyring with the active version's bytes until they are rewritten, including snapshots that still carry a fingerprint, which the rewrite drops. A later PR removes it.
  • Provisioning. deploy-hosted validates and deploys WIDEFLEET_SECRETS_KEYS. The agent generates a keyring per organization at organizations/<id>/app-secrets-keyring.json instead of the earlier hex key.
  • Docs are in Document the runtime keyring, rotation and upgrade #431, merged after the release.

Upgrade

  • Hosted: supply a keyring whose only key holds the bytes of the current WIDEFLEET_SECRETS_KEY in unpadded base64url, and deploy; the deployment rewrites runtime storage. Deployments keep secrets they do not name, so delete the earlier WIDEFLEET_SECRETS_KEY secret from the runtime Worker once with wrangler secret delete. Managed OAuth values still in the earlier format when that reader is removed need a new authorization.
  • Self-hosted: installations from before the keyring are not upgraded in place. Their runtime storage, including the workload signing ring and app keys, is encrypted with the earlier key, which the agent no longer provides to any runtime release. They are set up anew with matching agent and runtime releases.

The next PR in this stack derives one key per purpose from each version. Both ship in one release, so no deployment writes this PR's root-key format.

Testing

  • pnpm check
  • packages/app-runtime: secrets.test.ts (context binding, earlier versions for stored values, active version only for bearer values, values before the keyring), rekey.test.ts (rewrite of every stored kind with unchanged content and app key bytes, unreadable values, concurrent writes, deletions, paging), keys.test.ts (sealed record, same key bytes from a record before the keyring), oauth.test.ts (retired values after rotation; size limits), workload-keys.test.ts, oauth-metadata-document.test.ts, template-oauth.test.ts
  • apps/control-plane: runtime-rekey.test.ts (page loop); cloudflare/runtime.test.ts status checks with the key version; app-services-runtime.test.ts on workerd, including a stored snapshot from before the keyring rewritten through the management endpoint; managed-oauth-runtime.test.ts; cloudflare/runtime.test.ts -t secrets; cloudflare/deployment.test.ts -t "private secrets"
  • cargo test -p platform-agent --lib runtime::, cargo test -p platform-agent --bin platform-agent rekey
  • The rewrite step of deploy-hosted was not run against a Hosted installation.

Comment thread crates/platform-agent/src/runtime.rs Resolved
Comment thread crates/platform-agent/src/runtime.rs Resolved
Comment thread packages/app-runtime/src/rekey.ts Resolved
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs 5dc744f Nov 27 2026, 08:31 AM

The runtime encrypted app secret snapshots, app keys, the workload signing
ring and managed OAuth values with one raw key, WIDEFLEET_SECRETS_KEY, whose
ciphertexts named no key version. The key could not be rotated.

The runtime now takes WIDEFLEET_SECRETS_KEYS, a JWK Set in the control plane's
keyring format, and seals values as compact JWE with the key version as `kid`
and the value's object or purpose as a protected `ctx`. Stored values open under
any version; OAuth bearer values open only under the active one. The rekey
management endpoint, `POST /runtime/rekey` and `widefleet runtime rekey` seal
stored values again under the active version, page by page with conditional
writes. Values written before the keyring stay readable until rewritten.
…g alone

New values are always sealed under the active version, so one pass without
unreadable values or conflicts already leaves every value under it. The
earlier secret binding is deleted once by the operator instead of being
overwritten with an empty value on every deployment.
@tnkuehne
tnkuehne force-pushed the feat/runtime-keyring branch from 1bb36b2 to cbb7243 Compare November 26, 2026 20:35
… rewrite

The keyring reads and writes key material with jose's base64url, and the
agent draws its key and key ID from getrandom. A rewrite that loses its
conditional write counts a conflict instead of reading again; the operator
runs the rewrite again.
…nd CLI command

Rewriting runtime storage is a rare maintenance task, like re-encrypting the
control plane's secrets with tools/secret-encryption.ts. tools/runtime-rekey.ts
calls the runtime's private management route page by page: through the
RuntimeManagement binding for a Hosted installation, and through the private
listener with the control token inside a self-hosted control-plane container.
The control-plane route POST /runtime/rekey and widefleet runtime rekey are
removed.
The deployer already knows when the runtime with the current keyring is live,
so it drives the rewrite instead of an operator script: deploy-hosted after the
whole installation is deployed, and the agent after each committed Runtime job,
whose message reports the counts. The runtime's status reports its active key
version, so deploy-hosted also waits for a keyring change that keeps the
release.
… rekey message

During a rollout an earlier Worker version can still answer without a key
version; the wait now treats it as not yet live instead of failing the
deployment. A failed rewrite's message is cut to the job message limit.
The control plane keeps the keyring helpers only it uses, and modules import
the shared parser from contracts directly. deploy-hosted reads the runtime's
status once and waits until both its release and active key match, retrying a
read that an earlier version answers differently. The rewrite lists only the
RS256 ring instead of skipping other workload objects. Tests share one keyring
fixture per package, one fixture for values from before the keyring, and the
real page loop.
@tnkuehne
tnkuehne merged commit 9bc4ec8 into main Nov 27, 2026
15 of 16 checks passed
@tnkuehne
tnkuehne deleted the feat/runtime-keyring branch November 27, 2026 08:48
tnkuehne added a commit that referenced this pull request Nov 27, 2026
Stacked on #428. Each runtime keyring version encrypted every kind of
value with its own 32 bytes: app secret snapshots, the keys behind
`WIDEFLEET_KEYS`, the workload signing ring and the managed OAuth
access, refresh and state values. A weakness in how one of them uses the
key, for example a nonce reuse, would then reach all of them.

## Changes

- **Purpose keys (`secrets.ts`).** Each version derives one AES-256-GCM
key per purpose with HKDF-SHA256 (RFC 5869): a fixed 32-byte zero salt
and the label `widefleet runtime <purpose> v1`. The purposes are a
closed list: `app-secrets`, `app-keys`, `workload-keys`, `oauth-access`,
`oauth-refresh` and `oauth-state`.
- **Key IDs.** `kid` is `<version>:<purpose>`. Opening accepts only the
purpose the caller expects, and never the root key itself. Version IDs
cannot contain a colon, so the scheme is unambiguous.
- **Callers.** Snapshot preparation and reads, app keys, the workload
ring, managed OAuth and the rewrite pass their purpose. The rewrite
counts a value as current only under the active version's key for its
purpose.
- **Docs** are in #431.

This PR ships in the same release as #428, so no deployment writes the
root-key format of #428 and the rewrite runs once.

## Testing

- `pnpm check`
- `packages/app-runtime`: `secrets.test.ts` (a value of one purpose does
not open for another, also with the purpose renamed in its header; the
root key opens nothing), `rekey.test.ts`, `keys.test.ts`,
`oauth.test.ts`, `workload-keys.test.ts`,
`oauth-metadata-document.test.ts`, `template-oauth.test.ts`
- `apps/control-plane`: `app-services-runtime.test.ts` on workerd,
`managed-oauth-runtime.test.ts`, `cloudflare/runtime.test.ts -t secrets`








<!-- greptile_comment -->

<!-- greptile_summary -->

<h2><ruby><rp>Confidence Score: 5/5 · The PR appears safe to merge; no
blocking issue remains.</rp></ruby><a
href="https://app.greptile.com/api/retrigger?id=120995216&amp;sig=fc88eb82f480fd83d61f191706434ff82974403bb58fb65eedcc940c10e842ed"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img
alt="Retrigger"
src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"
align="right"></picture></a><a
href="https://app.greptile.com/-/switch-tenant?tenant=mendra&amp;next=%2Fwidefleet%2Fwidefleet%2Fpull%2F429&amp;utm_source=greptile_review&amp;utm_medium=github&amp;utm_campaign=view_in_greptile"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img
alt="View in Greptile"
src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"
align="right"></picture></a>Confidence Score: 5/5 <sup><img alt="Tier:
plus" title="Review tier: plus"
src="https://greptile-static-assets.s3.amazonaws.com/badges/effort-plus.svg?v=3"
height="12"></sup></h2>

<!-- greptile-impact-verdict --> The PR appears safe to merge; no
blocking issue remains.

<details open><summary><h3>Summary</h3></summary>

Derives a separate encryption key for each runtime purpose and includes
that purpose in `kid`.

- Runtime values use separate encryption keys for each purpose.
- The storage rewrite checks each value against its purpose key.
</details>

<details open><summary><h3>Diagram</h3></summary>

```mermaid
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  Root["Runtime key version"] --> HKDF["HKDF-SHA256 with purpose label"]
  Purpose["Caller supplies purpose"] --> HKDF
  HKDF --> Key["Purpose-specific AES-GCM key"]
  Key --> Seal["Encrypt value"]
  Context["Expected context"] --> Seal
  Seal --> Value["JWE with version:purpose and context"]
  Value --> Open["Open with expected purpose and context"]
  Key --> Open
```
</details>

<!-- greptile_confidence_score:5 -->

<sub>Reviews (8) · Last reviewed commit: ["Drop an import the removed
refresh test
..."](b963da6)
· [Reviewed by
Greptile](https://www.greptile.com/?utm_source=greptile_review&utm_medium=github&utm_campaign=review_public&utm_content=widefleet/widefleet)</sub>

<!-- /greptile_comment -->
tnkuehne added a commit that referenced this pull request Nov 27, 2026
Values stored before the runtime keyring (#428) and before the control
plane's keys per kind (#435) stayed readable through temporary readers
until they were rewritten. This removes those readers.

## Changes

- **Runtime.** `legacy-runtime-ciphertext.ts` goes: values sealed with
the raw key and app keys wrapped with AES-KW no longer open. Stored
values and app keys accept only the keyring format, so the rewrite no
longer special-cases app key records.
- **Control plane.** The reader for JWEs whose `kid` names only a key
version goes, together with `earlierFormat` in `secret-encryption
status`. A value still in that format counts as `failed`.
- **Tests.** The fixtures and cases for the earlier formats go. The
workerd test of the rewrite route now moves a value from a read-only key
version to the active one. Rejection and missing-key cases use values in
the current format, so each fails only for the property it tests.

## Upgrade

Rewrite both stores on the previous release before upgrading:

- A runtime deployment reports `0 rewritten, 0 unreadable, 0 conflicts`.
- `node tools/secret-encryption.ts status` reports `earlierFormat: 0`
and `failed: 0`.

Managed OAuth values that clients still hold from before the runtime
keyring are no longer accepted; those connections need a new
authorization.

## Testing

- `svelte-check` (control plane), `tsc` (app runtime), `oxlint`, `oxfmt`
- `packages/app-runtime`: `secrets.test.ts`, `keys.test.ts`,
`rekey.test.ts`, `oauth.test.ts`, `workload-keys.test.ts`
- `apps/control-plane`: `secret-encryption.test.ts`,
`secret-reencryption.integration.test.ts`, `runtime-rekey.test.ts`,
`app-services-runtime.test.ts` with `RUN_SERVICE_TESTS=1` (workerd and
celld), `cloudflare/deployment.test.ts -t "removes an undeployed app"`
with `RUN_CLOUDFLARE_TESTS=1`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant