feat(api-keys): Scope catalogue, kinds, expiry and sha256 keys #70

Merged
sharang merged 1 commits from feat/api-keys-scoped into main 2026-10-13 21:21:17 +00:00
Owner

Step 1 of ADR 0001 (docs/adr/0001-tenant-scoped-api-keys.md): tenant-bound, scoped API keys. This is Werkmeister prerequisite 1 and step 1 of firmwerk#64.

The registry's api_keys were already bound to a tenant. This PR extends them; it doesn't replace them. Key format, verify request and 200 valid=false stay the same.

Changes

  • internal/scopes:
    • A scope catalogue in the form <product>:<resource>.<action>: firmwerk:licence, pruefwerk:findings.read and so on. werkbank:* stays as it is.
    • Create checks the scopes against the tenant's entitlements and answers 422 not_entitled when one is missing.
    • GET /v1/api-keys/scopes lists the catalogue for the portal's scope picker.
  • Migration 0015: adds kind (user|ci|integration), holder, expires_at and hash_sha256 (unique index), and makes hash nullable.
  • Hashing: new keys are stored as sha256, found with one index lookup instead of argon2id at 64 MB per verify. Older keys keep argon2. Every candidate with the same prefix is now checked, so a prefix collision no longer invalidates a valid key (that was a QueryRow bug).
  • Verify: the response also carries key_id, kind, holder, expires_at, tenant_status and legacy.
    • scopes is limited to the products the tenant has booked right now; archived or halted tenants get no scopes.
    • Expired keys are reported as valid=false.
  • Key kinds: ci keys need scopes and an expiry at most 365 days ahead. integration keys are refused on this path; they come in step 1b, created by their holder product.
  • Revoke: DELETE /v1/api-keys/{id}?tenant_id=… answers 404 for another tenant's key.
  • OpenAPI and the CI test list (./internal/scopes/...) are updated.

Compatibility

  • Portal: it sends no scopes, so its keys stay legacy (a full grant on product, as before). Revoke without tenant_id still works.
  • Firmwerk and the Werkbank runner: the new verify fields are additive (no deny_unknown_fields). werkbank:runner keeps working as long as the tenant has pruefwerk.

Tests

  • The CI test command passes locally with -race; coverage is 77.4%.
  • Migration up/down passes, and golangci-lint v2.12.2 reports 0 issues.
  • New tests cover:
    • scoped keys and filtering by entitlement;
    • create validation: unknown scope, scope from another product, not entitled, CI expiry, integration;
    • expired keys;
    • revoke with a tenant check;
    • legacy argon2 keys with a prefix collision;
    • the catalogue endpoint.

Next steps (not in this PR)

  • Step 1b: key auth for CI licences (idempotent per key/device/UTC day, 60/h) and the integration path with the AUTHZ group api-keys.integration.
  • Then licence-client BP_LICENCE_API_KEY, the portal scope picker, and Prüfwerk accepting keys.

🤖 Generated with Claude Code

Step 1 of ADR 0001 (`docs/adr/0001-tenant-scoped-api-keys.md`): tenant-bound, scoped API keys. This is Werkmeister prerequisite 1 and step 1 of firmwerk#64. The registry's `api_keys` were already bound to a tenant. This PR **extends** them; it doesn't replace them. Key format, verify request and `200 valid=false` stay the same. ## Changes - **`internal/scopes`:** - A scope catalogue in the form `<product>:<resource>.<action>`: `firmwerk:licence`, `pruefwerk:findings.read` and so on. `werkbank:*` stays as it is. - Create checks the scopes against the tenant's entitlements and answers `422 not_entitled` when one is missing. - `GET /v1/api-keys/scopes` lists the catalogue for the portal's scope picker. - **Migration 0015:** adds `kind` (user|ci|integration), `holder`, `expires_at` and `hash_sha256` (unique index), and makes `hash` nullable. - **Hashing:** new keys are stored as sha256, found with one index lookup instead of argon2id at 64 MB per verify. Older keys keep argon2. Every candidate with the same prefix is now checked, so a prefix collision no longer invalidates a valid key (that was a `QueryRow` bug). - **Verify:** the response also carries `key_id`, `kind`, `holder`, `expires_at`, `tenant_status` and `legacy`. - `scopes` is limited to the products the tenant has booked **right now**; archived or halted tenants get no scopes. - Expired keys are reported as `valid=false`. - **Key kinds:** `ci` keys need scopes and an expiry at most 365 days ahead. `integration` keys are refused on this path; they come in step 1b, created by their holder product. - **Revoke:** `DELETE /v1/api-keys/{id}?tenant_id=…` answers 404 for another tenant's key. - **OpenAPI** and the CI test list (`./internal/scopes/...`) are updated. ## Compatibility - **Portal:** it sends no scopes, so its keys stay **legacy** (a full grant on `product`, as before). Revoke without `tenant_id` still works. - **Firmwerk and the Werkbank runner:** the new verify fields are additive (no `deny_unknown_fields`). `werkbank:runner` keeps working as long as the tenant has pruefwerk. ## Tests - The CI test command passes locally with `-race`; coverage is 77.4%. - Migration up/down passes, and golangci-lint v2.12.2 reports 0 issues. - New tests cover: - scoped keys and filtering by entitlement; - create validation: unknown scope, scope from another product, not entitled, CI expiry, integration; - expired keys; - revoke with a tenant check; - legacy argon2 keys with a prefix collision; - the catalogue endpoint. ## Next steps (not in this PR) - Step 1b: key auth for CI licences (idempotent per key/device/UTC day, 60/h) and the `integration` path with the AUTHZ group `api-keys.integration`. - Then licence-client `BP_LICENCE_API_KEY`, the portal scope picker, and Prüfwerk accepting keys. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sharang added 1 commit 2026-10-13 20:56:00 +00:00
feat(api-keys): Scope catalogue, kinds, expiry and sha256 keys
ci / shared (pull_request) Successful in 14s
ci / test (pull_request) Successful in 24m39s
ci / image (pull_request) Skipped
06d94dcceb
Step 1 of ADR 0001 (tenant-bound, scoped API keys; Werkmeister
prerequisite 1, firmwerk#64 step 1):

- internal/scopes: catalogue <product>:<resource>.<action>, validated
  on create against the tenant's entitlements (422 not_entitled);
  GET /v1/api-keys/scopes for the portal's picker.
- Migration 0015: kind (user|ci|integration), holder, expires_at,
  hash_sha256. New keys are stored as sha256 (indexed lookup); older
  keys keep argon2id and every prefix candidate is checked, so a
  prefix collision no longer breaks a valid key.
- Verify returns key_id, kind, holder, expires_at, tenant_status and
  legacy, and limits scopes to products booked right now. Expired
  keys are invalid. Empty scopes stay a legacy full grant.
- ci keys need scopes and an expiry within 365 days; integration keys
  are refused here (created by their holder product, next step).
- DELETE /v1/api-keys/{id}?tenant_id= refuses another tenant's key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CODEOWNERS rules requested review from Benjamin_Boenisch 2026-10-13 20:56:00 +00:00
sharang merged commit 97284b5008 into main 2026-10-13 21:21:17 +00:00
sharang deleted branch feat/api-keys-scoped 2026-10-13 21:21:17 +00:00
Sign in to join this conversation.