Skip to content

Worker: issue and renew engine license keys (#474, W1) - #481

Draft
teaspoonai wants to merge 23 commits into
mainfrom
feat/license-issue-474
Draft

teaspoonai wants to merge 23 commits into
mainfrom
feat/license-issue-474

Conversation

@teaspoonai

Copy link
Copy Markdown
Contributor

Part of #474 (Engine License Key), step W1. The engine side is teaport-engine#107, merged as 9122977. The design is in #474.

Draft: no deploy. Deploying runs migration 0004 and the Worker through worker-deploy, and needs the owner's OK. Until the owner makes the online key (LICENSE_SIGNING_KEY as a Worker secret, LICENSE_PUBKEY in wrangler.toml), POST /api/license answers 503 license_unavailable and records nothing. The live gate is unchanged either way.

What it adds

  • POST /api/license issues and renews engine license keys. The response is a license.json the engine verifies: payload v2, signed as an SSH signature (Ed25519, namespace teaport-license) over the JCS payload, with WebCrypto.

    • Personal: the request needs the box's acceptance id, as /api/token does. The key lasts a year and renews silently.
      • A renewal needs an acceptance of the current license version; otherwise it gets 409 eula_changed.
      • At most 3 devices per normalized email, enforced in one atomic INSERT … SELECT.
      • licensed acceptances get their commercial keys by hand.
    • Appliance: issued at the factory against the maker acceptance from Download gate: per-box maker acceptances for the factory (#464) #470, one key per maker record. The key is perpetual.
      • A box provisioned again keeps its key, which moves only to a newer record for the same serial.
      • A maker record that ever held a key never gets another (license_makers, filled by triggers).
    • Revocation sticks: a revoked key of any tier bars its device on every path, whatever the email, until staff release the row (released_at). Revoked keys keep counting toward the cap until they expire.
    • Requests carry ids only, never audio or transcripts. The new route shares the existing rate limiter, and no email address reaches Worker code, a response or a log.
  • Migration 0004, additive only:

    • a licenses table (with first_maker_id, released_at and signed_at);
    • a license_makers table and its two triggers;
    • indexes, including lower(trim(email)) on acceptances.

    The live Worker runs unchanged on the new schema. The queries are pinned to their indexes with INDEXED BY, because D1 collects no planner statistics, and tests check that the exact SQL never scans a table.

  • Docs:

    • packaging/RELEASING.md:
      • "Worker secrets" gains LICENSE_SIGNING_KEY. The owner generates it in their own terminal (openssl genpkey), saves it to Bitwarden, and runs wrangler secret put. The doc gives the exact line for the engine's prod.pub.
      • The FACTORY_TOKEN leak section now covers license keys, with a tested cleanup runbook.
    • docs/engine-license-data.md: what key issuance records and for how long, the revocation rules, and erasure. The "PROPOSAL to confirm with counsel" note is gone: 7 years was confirmed.

Tests

node --test worker/test/*.test.mjs: 82 pass (40 existing, 39 license, 3 runbook).

  • Interop with OpenSSH: ssh-keygen -Y verify accepts the Worker's signature, and a signature from ssh-keygen -Y sign parses and rebuilds byte for byte.
  • Interop with the engine: in review, the engine's own teaport-license verify accepted personal, appliance and renewed keys the Worker issued.
  • Concurrency: the device cap, same-device requests, renewals, appliance races and key moves.
  • Query plans: checked on the exported SQL.
  • The leak runbook: run as RELEASING.md writes it, between the leak-runbook markers, against a leak scenario. The tests also confirm it fails when the doc's SQL is wrong.
  • Workers runtime: the signer was run in the workerd that ships with wrangler. The new statements and triggers were run in wrangler's local D1.

Review: an independent review agent went through 10 passes and cleared it. The issues it found along the way are all fixed in this branch: revocations that didn't stick, the cap under concurrency, query plans, the key move, a maker record minting a second key, and the leak runbook.

After the first deploy: check in sqlite_master that both license_makers triggers exist on remote D1.

Still to come in #474: I1 (the installer requests the key, built on #475), O1 (#465: the owner accepts at setup, and the box is linked by a per-box secret), and E2/W2 (the revocation list, and the grace period after a personal key expires).

🤖 Generated with Claude Code

teaspoonai and others added 20 commits November 28, 2026 20:07
Moves bucket(), d1() and manifest() out of index.test.mjs into
worker/test/fakes.mjs (not a *.test.mjs, so the test glob skips it), for the
license tests that follow. No test changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
POST /api/license signs license.json ({payload, signature}) per the design's
wire format: payload v=2, an SSHSIG blob (namespace teaport-license, sha512)
over JCS(payload), made with WebCrypto Ed25519 (src/sshsig.js).

- {acceptance_id, device}: a personal key for a year, with the acceptance id
  the installer's receipt holds, checked as /api/token checks it. Licensed
  acceptances are refused (licensed_use). At most 3 devices per normalized
  email hold an unexpired, unrevoked personal key (device_cap); a device
  asking again gets the same license_id, so it never counts twice.
- {maker_id, device}: the appliance key of a live maker acceptance, no
  expiry; one per maker acceptance (a unique index), the same device gets it
  signed again, another is refused (maker_device).
- {license_id, device}: a renewal keeping the license_id. Personal: another
  year (keeping its engine_major), the cap again. Appliance: the same terms
  under the current key, past MAKER_MAX_DAYS too.

LICENSE_SIGNING_KEY (secret, PKCS#8 PEM) signs only if it is the key that
LICENSE_PUBKEY (var, OpenSSH line labelled LICENSE_KID) names; otherwise 503
license_unavailable and nothing is read or written. LICENSE_PUBKEY ships
empty, so this deploys before the key exists.

Migration 0004 is additive: the licenses table, and an index on
acceptances' normalized email for the cap. The retention cron deletes a
personal key's row RETENTION_DAYS after it expired; appliance rows stay.

Tests cover each path, the cap, the 503 cases, the migration, and interop
with ssh-keygen -Y verify / -Y sign.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
packaging/RELEASING.md, "Worker secrets": LICENSE_SIGNING_KEY. The owner
makes it with openssl in their own terminal and keeps it in Bitwarden; the
OpenSSH public line for LICENSE_PUBKEY and engine/license_keys/prod.pub is
built from the raw key (ssh-keygen -y cannot read openssl's PKCS#8 file).
The commands were run with a throwaway key, in zsh and bash, and the line
verifies the Worker's signatures under ssh-keygen -Y verify. Then secret
put, shred; what a leak exposes; rotation; rollbacks.

docs/engine-license-data.md: the licenses table (ids only, no email; the
link to the acceptance or maker record; tpdev2), the key file on the box,
retention (a personal key's row 7 years after it expired; appliance rows
kept), access and erasure. The retention period is no longer a proposal:
the owner confirmed 7 years.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review fixes for POST /api/license.

- A revoked personal key stays revoked: its device gets 403 revoked on the
  acceptance path and on renewal, under any acceptance of the same email.
  Revoking an acceptance revokes the personal keys last issued under it:
  no renewal, no cap slot, and accepting again does not bring them back.
- Freeing a slot is a new column, released_at (0004 is not deployed yet):
  a released key no longer counts or renews (403 released), and its device
  may get a new key.
- The cap and the one-key-per-device rule are one INSERT ... SELECT ...
  WHERE NOT EXISTS (...) AND (count) < 3, and a renewal one conditional
  UPDATE: written first, signed after, checked by meta.changes. Requests at
  the same moment no longer pass the cap or give a device two keys.
- The cap's statements start from acceptances_email_norm, then
  licenses_acceptance, by INDEXED BY and CROSS JOIN: D1 keeps no planner
  statistics, and without them SQLite picked licenses_expires (every live
  personal key). They are exported as LICENSE_SQL, and the tests check
  EXPLAIN QUERY PLAN on those strings over 500 keys without ANALYZE.
- The rate-limit test now checks that neither the body, D1 nor R2 is read.
- The D1 fake's run() returns meta.changes, as D1's does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eptance (#474)

docs/engine-license-data.md: released_at; a revocation sticks, and an
acceptance's revocation reaches its keys; an appliance key goes to the
first device asking with a live maker id (design §6). A key's row can
outlive its acceptance by up to 8 years, so access and erasure also find
rows by device, and erasure deletes a person's license rows with their
acceptances.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Second review fixes for POST /api/license.

- R1: revoking a key is the device's. A device holding a revoked,
  unreleased personal key gets none under any email address: the new-key
  INSERT and the renewal UPDATE refuse it in the same statement, by
  licenses_device (INDEXED BY, as the others; without it SQLite picked
  licenses_expires). Releasing the row lifts it.
- R2: revoked keys, and keys of a revoked acceptance, count toward the
  email's 3-device cap until they expire; only released keys stop counting.
  Accepting again starts no new cap.
- A write refused for a reason gone by the re-read (a row released or
  deleted meanwhile) is tried again, up to 3 times, then 409 conflict; a
  renewal whose row is gone is 404 unknown_license, not device_cap.
- A comment: an index named by INDEXED BY that is missing fails to
  prepare, a 500 with nothing written (fail closed).

Tests: the revoked-device and revoked-acceptance tests follow the new
rules, a retry/conflict test, and the plan test covers the device check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/engine-license-data.md: a revoked key bars its device under any
email address until released; revoking an acceptance blocks the person
under that address; revoked keys count toward the cap until they expire.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
R3: the device check looked at personal keys only, so a box whose
appliance (or commercial) key was revoked got a personal key under a new
acceptance: a stolen appliance could switch tiers. The check now takes
any revoked, unreleased row on the device, whatever its tier; the plan
stays on licenses_device (the plan test unchanged). Releasing the row
lifts the bar.

Tests: a revoked appliance key and a revoked commercial row bar a
personal key on their device, under any email, until released; an
unrevoked appliance key bars nothing. Docs and the migration's comments
say "revoking a key bars the device", any tier.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A device holding any revoked, unreleased row now gets no new key and no
renewal of any tier. The appliance path checks it in its own statements:
the INSERT OR IGNORE of a new appliance key becomes INSERT ... SELECT ...
WHERE NOT EXISTS (revoked row on the device), and signing an appliance key
again (asked again, or renewed) is a conditional UPDATE, written first and
signed after, as the personal keys are. Both are in LICENSE_SQL and the
plan test (licenses_device, by INDEXED BY). A box back at the factory
after a revocation is released by staff first.

Tests: a device with a revoked personal key gets no new appliance key, no
repeat and no renewal of the one it has; releasing the row lifts it.
Docs and the migration's comments say so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… its key (#474)

The owner's two decisions.

(a) A personal renewal ({license_id, device}) whose acceptance is not of
the channel's current license gets 409 eula_changed, the same body as the
acceptance path (one helper, eulaRefusal, for both), and nothing is
written. The box keeps its key to expiry plus the engine's grace while
teaport has the license accepted again; that acceptance renews the same
key. Appliance renewals are not tied to a license version.

(b) {maker_id, device} for a live maker acceptance with no key yet, from
a device holding a live appliance key under an older maker acceptance,
moves that key: same license_id, maker_id updated, signed again. One
UPDATE OR IGNORE (applianceMove, by licenses_device): of records racing,
the newest ends up with it, a key never moves back to an older record
(409 maker_superseded), the unique index on maker_id holds, and the
revoked-device bar applies. A new appliance key is inserted only for a
device with no live one. A maker record past its retention counts as
older.

Tests: the 409 on renewal (same body, nothing written, appliance
unaffected, re-acceptance renews); the move, its limits (released,
revoked, the device bar, a deleted old record); a race of five newer
records both ways round; the plan test covers applianceMove.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…474)

docs/engine-license-data.md and packaging/RELEASING.md: a personal
renewal answers 409 eula_changed after a license change (the box keeps
its key to expiry plus the 30-day grace, teaport asks again, and the new
acceptance renews the same key; appliance keys unaffected); a box
provisioned again keeps its appliance key, which moves to the newest
maker acceptance; staff release a revoked row before re-provisioning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
R5: applianceMove checked only that the new maker record was newer. With
a leaked FACTORY_TOKEN and a box's tpdev2, a made-up record for another
serial could take the box's key, and revoking that record in the leak's
cleanup would then stop the real box's renewals.

The move now also needs the new record's device_id (the serial's hash)
to equal the old record's, while that one exists (an old record deleted
by retention still counts as the box's). Otherwise nothing moves and
nothing is written: 403 maker_serial (a new applianceHeld statement says
why, so a newer record of the same box stays 409 maker_superseded). A
swapped module or serial needs staff to release the old key; documented
in docs/engine-license-data.md and packaging/RELEASING.md.

Tests: another serial's newer record is refused, the key stays, the real
box still renews after that record is revoked, a same-serial record still
moves it, and a swapped serial gets a new key once the old one is
released; one new record asked from two boxes moves only its own box's
key, in order and at once. The plan test covers applianceHeld.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…up (#474)

packaging/RELEASING.md, FACTORY_TOKEN: a leak also mints appliance
license keys (up to FACTORY_DAILY_MAX a day, for any tpdev2 with no live
appliance key and no revoked row, never expiring), a made-up record for a
box's own (guessable) serial can take its key, and any record can once
the box's old record is gone. The cleanup is now ordered, with the exact
D1 commands for the owner (--remote; not run here): list the suspect
records, then their keys; revoke the made-up boxes' keys (they reach the
field only through the W2 revocation list); give real boxes' keys back to
their own records, or unlink or release them; check none is left; only
then revoke the maker acceptances. The SQL was run in local D1 against a
leak scenario. docs/engine-license-data.md points to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eys by evidence (#474)

The review ran 08aa2ef's runbook against a leak: it revoked a real box's
key and left made-up keys live. Now, in packaging/RELEASING.md:

- Order: rotate the secret, then revoke the suspect maker acceptances
  before touching any key. The Worker already refuses mint, move and
  renewal under a revoked one, so that stops the leak's records at once.
  Then sort their keys, revoke the made-up ones, give the real ones back,
  and check none is left live (not 0: go again from step 2).
- Real vs made-up: real only if the key predates the leak (a key the
  record took) or its device is the tpdev2 the same serial's box held a
  key for before the leak. A sold serial is no evidence. Revoking and
  giving back are each one statement on that rule, with no hand-made
  lists; a real key whose own record is gone or holds another key is
  unlinked (maker_id NULL, it keeps renewing), never released.
- IP: COALESCE(ip, '') NOT IN (<every factory address>, IPv6 too),
  checked against the station's provisioning log.
- Time: <LEAK_START> in the stored form with milliseconds
  (2026-12-01T00:00:00.000Z); the shorter form misses the first second.

The SQL sits between leak-runbook markers, read by the test that follows;
it was also run in local D1. docs/engine-license-data.md states the order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
worker/test/leak-runbook.test.mjs reads the runbook's SQL from between
its markers in packaging/RELEASING.md and runs it, in order, on the fake
D1 after a leak whose keys the Worker issued: real boxes A (key taken by
a same-serial record), B, C (built during the leak, IPv6 factory
address), D (released, then re-keyed on its own hardware), E (record past
retention, key taken); made-up ones in the leak's first second, on
attacker hardware, and under a sold serial. Between the record
revocation and the rest, the attacker's mint, move and renewal are
refused. Every real key ends live, under its own record (or unlinked) and
renewing; every made-up key is revoked; the check prints 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
licenses.first_maker_id (edited into 0004, not yet deployed): the maker
acceptance an appliance key was first issued under, set by the insert
and never changed by a move (maker_id is where it is now). A leak's
cleanup sorts keys by it: a real box built during the leak, whose key a
same-serial record took, was otherwise sorted made-up.

Tests: set on issue, kept across a move. docs/engine-license-data.md
lists the column.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…_START (#474)

Eighth review of the leak runbook (packaging/RELEASING.md):

- A key under a suspect record is real if made before <LEAK_START>, or
  first issued under a record that is not suspect (first_maker_id), or
  bound to the tpdev2 that serial's box held a key for before the leak.
  A key minted under one made-up record and moved to another stays
  made-up. Steps 3, 4 and 5 use that rule.
- Step 1 also lists every record the leaked secret (its mark, from
  <KNOWN_RECORD>) made before <LEAK_START>, with whether it came from a
  factory address: one missing from the station's log means the secret
  was out earlier; move <LEAK_START> back and start again (a late one
  finds nothing, and the check would still print 0).

worker/test/leak-runbook.test.mjs, now three tests: the check run before
the cleanup prints more than 0; the full run, with a real box built
during the leak whose key was taken (G) and a chain mint-and-move, ends
with every real key live under its own record and every made-up one
revoked; a run without step 4 gives no made-up key a record; a late
LEAK_START shows in step 1's second list and an earlier one cleans up.
Mutants killed: an always-0 check, step 5's move and unlink without the
real-keys filter, the sort without first_maker_id. The SQL also ran in
local D1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ninth review: since the key move (813fe05), a box provisioned again left
its old record R1 holding nothing, so applianceInsert let R1 mint a
second, perpetual appliance key for any hardware, with no FACTORY_TOKEN:
R1's id is in the box's old receipt. The leak runbook never sees it, R1
being a real factory record.

Migration 0004 (not deployed) adds license_makers (maker_id PRIMARY KEY,
license_id, at): every maker acceptance that ever held an appliance key,
written by triggers on licenses (AFTER INSERT, and AFTER UPDATE OF
maker_id to a new non-NULL one) in the same statement as the key's
write; insert-only, kept for good. applianceInsert and applianceMove
refuse a record found there (NOT EXISTS, by its primary key), and the
refusal is 403 maker_spent. A record that got its key by a move and lost
it again (R2 -> R3) is spent too; a released box's next key comes under
its next record. D1 applies the triggers and runs them (checked in local
D1).

Tests: a superseded record gets no key for other hardware, after one
move and after two; the history follows the moves and outlives the maker
acceptance; the plan test checks the lookup uses license_makers' primary
key. The leak runbook tests pass unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/engine-license-data.md: the license_makers table, kept for good
(ids only; it must outlive the maker acceptances it names), and why a
maker acceptance that ever held a key gets no other. packaging/
RELEASING.md: provisioning a box again spends its old record.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cloudflare-workers-and-pages

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

Copy link
Copy Markdown

Deploying teaport-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1d2fcaa
Status: ✅  Deploy successful!
Preview URL: https://d0faddb0.teaport-docs.pages.dev
Branch Preview URL: https://feat-license-issue-474.teaport-docs.pages.dev

View logs

…#474)

The cited acceptance lines are unchanged (hash 290d30f9); only their
line numbers moved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the license (#474)

As teaport#482 does for acceptances and downloads, for the v2.0 privacy
policy: the licenses rows are kept on an erasure request (Art. 6(1)(b)
while the license is in force, then Art. 17(3)(e) until their retention),
and used for nothing else; the request changes nothing about the license.
Ending the license is a separate request: revoked_at on the acceptances,
released_at on the personal keys, an appliance's owner unlinked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	docs/engine-license-data.md
#	docs/src/content/docs/reference/licenses.md

This branch has not been deployed

No deployments
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