feat(api-keys): CI licences by API key and integration keys
ci / shared (pull_request) Successful in 13s
ci / test (pull_request) Successful in 25m18s
ci / image (pull_request) Skipped

Step 1b of ADR 0001, with the user's decisions D1–D3 recorded:

- POST /v1/licences/ci (PUBLIC_EXPLICIT, the bearer is a tenant API key
  with scope <product>:licence): 24 h licence with kind "ci", no
  heartbeat, not a seat (D1; the registry records ci.licence usage once
  per key and day). Reused per key, device and UTC day; at most 60
  calls per key and hour. Unblocks firmwerk#64 step 1.
- Werkpilot CI licences carry the tenant's LiteLLM key (D2) with a
  1-day LiteLLM expiry; the secret is sealed on the licence row
  (AES-256-GCM, new SECRETS_ENCRYPTION_KEY, migration 0017) so the
  reused licence hands out the same key. At most 10 keys per API key
  and day. Without the encryption key a reuse comes without the key.
- Integration keys: a tenant admin's user token from a holder product's
  web client (werkmeister-<env>) creates, lists and revokes that
  holder's key for its own tenant only; scopes from a per-holder
  allowlist, the holder product must be booked. A user token on the key
  routes is classified as AUTHZ group api-keys.integration, so plain
  api-keys stays a service grant.
- Migration 0016: key names are unique among active keys only, so a
  revoked name can be reused (re-issue = revoke, then create).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sharang ParnerkarandClaude Opus 5.5 committed 2026-10-13 23:32:01 +02:00
1 parent 97284b5008
commit b89dd2ae4c
28 files changed
+1208 -39

No files matched your search

+15 -12
View File
@@ -3,7 +3,7 @@
This is prerequisite 1 of the Werkmeister plan (platform/werkmeister ADR 0001), and it also settles the CI-licence gap (firmwerk#64, step 1).
- **Deciders:** Sharang (platform), reviewed by the platform team.
- **Status:** accepted 2026-10-13, with review corrections folded in (§9). Step 1 is implemented in this repo: migration 0015, `internal/scopes`, verify, revoke and SHA-256 keys. Open: the user decisions in §5 (D1, D2) and §2 (end of legacy keys).
- **Status:** accepted 2026-10-13, with review corrections folded in (§9) and the user's decisions D1–D3 (§2, §5). Implemented in this repo: step 1 (migration 0015, `internal/scopes`, verify, revoke, SHA-256 keys) and step 1b (`POST /v1/licences/ci`, integration keys, migrations 0016 and 0017). Open: the date for ending legacy keys.
Customers appear only as K-numbers.
@@ -65,6 +65,7 @@ The plan's placeholders (`findings:read`, `scan:trigger`) become `pruefwerk:find
**Proposal: no backfill. `scopes = []` stays a legacy full grant** on the key's `product` (or on every entitled product if `product` is NULL), exactly as today.
- Verify returns `"legacy": true` for such keys.
- The portal lists them as "Vollzugriff (alt)" with a hint to replace them.
- **D3 (decided):** legacy keys keep working as "Vollzugriff (alt)" and end after a 90-day notice; the user sets the date.
- New keys **must** carry at least one scope **once the portal's scope picker ships**. Until then the registry still accepts empty scopes for `kind=user`; `ci` keys always need scopes.
- After a 90-day notice, legacy keys stop being accepted. That is a separate decision for the user, with the date in the changelog.
@@ -118,7 +119,7 @@ What that means for each product:
## 5. API keys on registry routes: the CI licence (G5, firmwerk#64 step 1)
Narrow on purpose: **only `POST /v1/licences`** accepts a key.
Narrow on purpose: one dedicated route, **`POST /v1/licences/ci`**, accepts a key. It is registered outside the JWT chain (like the heartbeat) rather than mixing key auth into `requireAuth`; the interactive `POST /v1/licences` is unchanged.
- **Auth:** `Authorization: Bearer bp_…` with scope `<product>:licence`. The tenant comes from the key. `device = ci:<runner host or name>`, set by the CLI.
- **Licence:**
@@ -127,15 +128,15 @@ Narrow on purpose: **only `POST /v1/licences`** accepts a key.
- **idempotent per (key_id, device, UTC day):** a repeat request returns the existing unexpired licence instead of creating a new row per job;
- **rate limit per key** (60/h; above that `429`);
- the grace ladder applies unchanged.
- **bp-licence:** reads `BP_LICENCE_API_KEY`. If it's set, it skips the device flow and calls `Registry::issue` with the key. Store, verify and grace stay the same. Werkpilot and Firmwerk get CI mode by bumping the crate.
- **bp-licence:** reads `BP_LICENCE_API_KEY`. If it's set, it skips the device flow and calls `POST /v1/licences/ci` with the key and `device` = runner name. Store, verify and grace stay the same. Werkpilot and Firmwerk get CI mode by bumping the crate.
- **Key kind:** a CI key is created in the portal with `kind=ci` (picker preset "CI-Lizenz", scope `<product>:licence`, expiry required, at most 365 days).
**Two decisions for the user** (I don't decide these):
- **D1, seats:** does a CI licence count as a seat?
- Proposal: no. It records the usage event `ci.licence` (per key and day) instead of `seat.login`, and the plan's seat limit doesn't apply. CI usage is metered, not seat-billed.
- **D2, Werkpilot in CI:** does a Werkpilot CI licence get the per-tenant LiteLLM key, as interactive licences do?
- Proposal: yes. It's the same tenant key, metered the same way, with no billing for now.
- Without it, Werkpilot can't run headless in CI.
**Decided by the user (2026-10-13):**
- **D1, seats:** a CI licence is **not** a seat. The CLI records the usage event `ci.licence` (per key and day) instead of `seat.login`; CI usage is metered, not seat-billed.
- **D2, Werkpilot in CI:** a Werkpilot CI licence **gets the per-tenant LiteLLM key**, metered the same way, no billing for now.
- Werkpilot CI licences are reused per (key, device, UTC day) like the others. The LiteLLM key is generated with the licence (`duration: 1d`, so LiteLLM expires it itself) and its secret is stored **sealed** on the licence row (AES-256-GCM under the new registry secret `SECRETS_ENCRYPTION_KEY`, bound to the licence id; migration 0017). The reused licence hands out the same key again.
- Cost cap: at most 10 LiteLLM keys per API key and UTC day (distinct devices); beyond that the licence comes without a key (warning `llm_key_daily_cap`).
- Without `SECRETS_ENCRYPTION_KEY` a reused licence comes without the key (warning `llm_key_not_repeatable`).
## 6. Werkmeister's own use
@@ -148,6 +149,8 @@ Werkmeister acts for a tenant against Prüfwerk and Firmwerk, and later the regi
- **Storage:** encrypted in Werkmeister's database (AES-GCM, key `WERKMEISTER_KEK`). In the cloud it comes from the orca project secret. On-prem (W5) it must also work without orca: an env var or a file (`WERKMEISTER_KEK_FILE`).
- **Visibility:** shown in the portal list as "Werkmeister (Integration)", and the tenant can revoke it there.
**Name uniqueness:** migration 0016 makes `(tenant_id, name)` unique among **active** keys only, so a revoked key's name is free again (re-issue = revoke, then create; also lets portal users reuse a name).
**Who creates it.** Fork, with a decision proposal:
- **(a)** `werkmeister-svc-<env>` gets the AUTHZ group `api-keys`, and Werkmeister checks "caller is IT_ADMIN of that tenant" itself. This is rejected: it gives a product service client cross-tenant power to create keys, which is exactly what we want to avoid.
- **(b), decided:** the registry accepts **user tokens** on `POST /v1/api-keys`, but only:
@@ -181,16 +184,16 @@ Steps 1 and 2 unblock firmwerk#64. Steps 1 and 5 unblock Werkmeister's Prüfwerk
## 8. Open questions
- **D1 seats and D2 LLM key** (§5): the user decides.
- **End of legacy keys** (§2): date and notice, the user decides.
- **End of legacy keys** (§2, D3): the user sets the date; 90 days' notice.
- **Verify cost:** whether the registry also caches verify results itself, beyond the product caches. It's probably unnecessary with SHA-256.
- **LiteLLM budget per tenant before billing starts:** every LiteLLM key has its own MaxBudget, so a tenant's effective budget grows with its keys (devices, CI). Move the budget to the tenant (a LiteLLM team per tenant) before billing.
- **Registry data for Werkmeister** (documents, audit read, notifications): via the integration key, which means more registry routes accepting keys, or via `werkmeister-svc` AUTHZ groups with tenant scoping enforced in Werkmeister.
- Proposal: AUTHZ groups for read-only paths in W3, and keys once the registry route set is clear.
## 9. Review corrections (2026-10-13), folded in
1. **§6(b) AUTHZ:** a narrow group `api-keys.integration` instead of `api-keys`. Create, list and revoke for user principals are own tenant, IT_ADMIN and `kind=integration` only.
2. **§5:** CI licence issue is idempotent per (key_id, device, UTC day), with a rate limit of 60/h per key.
2. **§5:** CI licence issue is idempotent per (key_id, device, UTC day), with a rate limit of 60/h per key. Implemented as `POST /v1/licences/ci` (200 for the reused licence, 201 for a new one, 429 above the limit).
3. **§4:** revocation takes up to 60 s to take effect (product cache TTL). This is stated in the ADR and in the portal's revoke dialog.
4. **§7 step 5:** Prüfwerk implements it from a short spec of §4; its prod promotion is coordinated separately.
5. **§6:** the KEK also works without orca (env or file) for on-prem (W5).