feat(api): the customer API accepts portal-issued API keys #564

Merged
sharang merged 2 commits from feat/api-key-acceptance into main 2026-10-13 22:22:22 +00:00
Owner

Implements step 5 of tenant-registry ADR 0001 (#70 there): the customer API accepts portal-issued API keys (Authorization: Bearer bp_…) next to Keycloak JWTs. Spec: research/2026-10-13_pruefwerk-api-key-acceptance-spec.md (decision D4).

How it works

  • Middleware: a new api_key_gate::require_api_key runs just outside the JWT middleware.
    • A bp_ bearer is verified with the registry (POST /v1/internal/api-keys/verify, using the existing pruefwerk-svc token) and becomes a TenantContext carrying ApiKeyAccess.
    • Every other bearer goes on to the JWT path unchanged.
    • Health, admin, Werkbank and platform paths are skipped, so runner keys still reach require_runner_token untouched.
  • Cache:
    • Valid and invalid verdicts are both cached for 60 s, keyed by sha256(key); the key itself is never stored, cached or logged.
    • At most 10,000 entries: when full, expired entries are dropped first; if none have expired, the new verdict is answered but not cached.
    • The registry being down with no fresh entry gives 503. An expired entry counts as no entry, so an outage never extends a revoked key's life.
    • The Werkbank runner path shares the same cached verify.
  • Principal:
    • Taken only from the verify answer: tenant, scopes, key_id, kind, holder, tenant_status and legacy.
    • The support tenant header is never read for a key, and a key holds no org role (product_access() is None, so any handler-level role check refuses it).
    • user_id is apikey:<key_id> and the display name is the holder, so the finding history attributes changes to the key.
  • Scopes (role_gate::KEY_SCOPES):
    • The table matches on method plus route template, exactly as the spec lists it.
    • Every other route answers 403 {"error":"scope_missing","required":…}, even for a key holding every scope.
    • x.write implies x.read; scans.run implies nothing.
    • Legacy keys (no scopes, product pruefwerk or empty) get only the four read scopes.
  • Entitlement:
    • require_entitlement skips keys, because verify already intersects the key's scopes with the tenant's entitlements and returns the ladder status.
    • restricted/frozen → reads work and writes get 402, through the existing status middleware.
    • A registry answer without tenant_status (one from before #70) is treated as read-only.
  • Audit:
    • Key attribution everywhere (actor_type: "apikey").
    • In addition, every key write is audited like support writes, as pruefwerk.api_key_write, with X-BP-Actor recorded as metadata.on_behalf_of. That header is cleaned and capped at 120 characters and never used to authorize.
    • This is the stricter reading of spec §7; say so if attribution alone was meant.
  • Guide page: docs/guide/api-keys.md (in German).

Tests

  • Router tests (role_gate/router_tests.rs):
    • every table route exists in the real router and needs its own scope;
    • write implies read;
    • nothing outside the table is reachable with a key;
    • legacy keys can read but not write or run.
  • Registry (registry.rs):
    • valid and invalid verdicts are reused within the TTL;
    • an unreachable registry is answered from a warm cache and fails on a cold or expired one;
    • the cache holds digests only, and its cap holds.
  • Middleware (api_key_gate.rs): the context a key gets, legacy handling, a registry answer from before #70, and the X-BP-Actor cleanup.
  • tests/api_key_access.rs (needs Mongo, as CI provides; run locally against a temporary mongo:7), through the server's middleware order:
    • the tenant comes only from verify;
    • no second verify call and no entitlement call within the TTL;
    • unknown key → 401, JWT path untouched, cold cache with the registry down → 503;
    • restricted tenant → read 200, write 402;
    • the write audit entry has the expected shape.

Not done here

  • End-to-end on stage waits for tenant-registry #70 to be merged and deployed.
  • Prod promotion needs your go, timed around the K-00001 pilot.
  • No new endpoints; the MCP server does not accept keys.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p

Implements step 5 of tenant-registry ADR 0001 (#70 there): the customer API accepts portal-issued API keys (`Authorization: Bearer bp_…`) next to Keycloak JWTs. Spec: `research/2026-10-13_pruefwerk-api-key-acceptance-spec.md` (decision D4). ## How it works - **Middleware:** a new `api_key_gate::require_api_key` runs just outside the JWT middleware. - A `bp_` bearer is verified with the registry (`POST /v1/internal/api-keys/verify`, using the existing `pruefwerk-svc` token) and becomes a `TenantContext` carrying `ApiKeyAccess`. - Every other bearer goes on to the JWT path unchanged. - Health, admin, Werkbank and platform paths are skipped, so runner keys still reach `require_runner_token` untouched. - **Cache:** - Valid and invalid verdicts are both cached for **60 s**, keyed by `sha256(key)`; the key itself is never stored, cached or logged. - At most 10,000 entries: when full, expired entries are dropped first; if none have expired, the new verdict is answered but not cached. - The registry being down with no fresh entry gives `503`. An expired entry counts as no entry, so an outage never extends a revoked key's life. - The Werkbank runner path shares the same cached verify. - **Principal:** - Taken only from the verify answer: tenant, scopes, `key_id`, `kind`, `holder`, `tenant_status` and `legacy`. - The support tenant header is never read for a key, and a key holds no org role (`product_access()` is `None`, so any handler-level role check refuses it). - `user_id` is `apikey:<key_id>` and the display name is the holder, so the finding history attributes changes to the key. - **Scopes (`role_gate::KEY_SCOPES`):** - The table matches on method plus route template, exactly as the spec lists it. - Every other route answers `403 {"error":"scope_missing","required":…}`, even for a key holding every scope. - `x.write` implies `x.read`; `scans.run` implies nothing. - **Legacy keys** (no scopes, product `pruefwerk` or empty) get only the four read scopes. - **Entitlement:** - `require_entitlement` skips keys, because verify already intersects the key's scopes with the tenant's entitlements and returns the ladder status. - `restricted`/`frozen` → reads work and writes get `402`, through the existing status middleware. - A registry answer without `tenant_status` (one from before #70) is treated as read-only. - **Audit:** - Key attribution everywhere (`actor_type: "apikey"`). - In addition, every key write is audited like support writes, as `pruefwerk.api_key_write`, with `X-BP-Actor` recorded as `metadata.on_behalf_of`. That header is cleaned and capped at 120 characters and never used to authorize. - This is the stricter reading of spec §7; say so if attribution alone was meant. - **Guide page:** `docs/guide/api-keys.md` (in German). ## Tests - **Router tests (`role_gate/router_tests.rs`):** - every table route exists in the real router and needs its own scope; - write implies read; - nothing outside the table is reachable with a key; - legacy keys can read but not write or run. - **Registry (`registry.rs`):** - valid and invalid verdicts are reused within the TTL; - an unreachable registry is answered from a warm cache and fails on a cold or expired one; - the cache holds digests only, and its cap holds. - **Middleware (`api_key_gate.rs`):** the context a key gets, legacy handling, a registry answer from before #70, and the `X-BP-Actor` cleanup. - **`tests/api_key_access.rs`** (needs Mongo, as CI provides; run locally against a temporary `mongo:7`), through the server's middleware order: - the tenant comes only from verify; - no second verify call and no entitlement call within the TTL; - unknown key → 401, JWT path untouched, cold cache with the registry down → 503; - restricted tenant → read 200, write 402; - the write audit entry has the expected shape. ## Not done here - End-to-end on stage waits for tenant-registry #70 to be merged and deployed. - Prod promotion needs your go, timed around the K-00001 pilot. - No new endpoints; the MCP server does not accept keys. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang added 2 commits 2026-10-13 22:07:11 +00:00
Step 5 of tenant-registry ADR 0001. A `Bearer bp_…` token is verified
with the registry (pruefwerk-svc) and becomes a tenant context carrying
the key; every other bearer goes through the JWT path unchanged.

- Verdicts, valid and invalid, are cached for 60 s by the key's SHA-256;
  an unreachable registry with no fresh verdict means 503. The Werkbank
  runner shares the same cached verify.
- Tenant, scopes and ladder status come only from the verify answer; the
  support header is never read for a key, and a key holds no org role.
- role_gate gets a method + route -> scope table; every other route
  answers 403 scope_missing. x.write implies x.read. Legacy keys (no
  scopes) get the read scopes only.
- The entitlement check is skipped for keys (verify already intersects
  scopes with entitlements). Key writes are audited like support writes,
  as apikey:<key_id> with the holder and X-BP-Actor as on_behalf_of.
- Guide page docs/guide/api-keys.md.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
fix(api): cap the API-key verdict cache at 10,000 entries
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 10m7s
2254e1c8ff
Callers choose the keys, so cached invalid verdicts must not grow without
end: at the cap expired verdicts are dropped first, and if none are, the
new verdict is answered but not kept.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang force-pushed feat/api-key-acceptance from f7f56ab1cc to 2254e1c8ff 2026-10-13 22:07:11 +00:00 Compare
sharang merged commit 07ba0d6665 into main 2026-10-13 22:22:22 +00:00
Sign in to join this conversation.