Files
tenant-registry/migrations/0015_api_keys_scoped.up.sql
T
Sharang ParnerkarandClaude Opus 5.5 06d94dcceb
ci / shared (pull_request) Successful in 14s
ci / test (pull_request) Successful in 24m39s
ci / image (pull_request) Skipped
feat(api-keys): Scope catalogue, kinds, expiry and sha256 keys
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>
2026-10-13 22:55:58 +02:00

19 lines
953 B
SQL

-- Tenant-bound, scoped API keys (ADR: prerequisite 1 of the Werkmeister plan).
-- kind: user (portal, a person's key), ci (CLI licences in CI), integration
-- (held by a product for a tenant, e.g. Werkmeister). New keys are stored as
-- sha256 of the 192-bit random key (indexed lookup); rows created before this
-- migration keep their argon2id hash.
ALTER TABLE api_keys
ADD COLUMN kind TEXT NOT NULL DEFAULT 'user' CHECK (kind IN ('user', 'ci', 'integration')),
ADD COLUMN holder TEXT,
ADD COLUMN expires_at TIMESTAMPTZ,
ADD COLUMN hash_sha256 BYTEA;
ALTER TABLE api_keys ALTER COLUMN hash DROP NOT NULL;
ALTER TABLE api_keys
ADD CONSTRAINT api_keys_hash_present CHECK (hash IS NOT NULL OR hash_sha256 IS NOT NULL),
ADD CONSTRAINT api_keys_holder_kind CHECK ((kind = 'integration') = (holder IS NOT NULL));
CREATE UNIQUE INDEX api_keys_hash_sha256_idx ON api_keys (hash_sha256) WHERE hash_sha256 IS NOT NULL;