Check API keys in our own database instead of the auth service
Database · PostgreSQL · beginner · modification
Drops the round trip to the legacy auth service on every API request that carries a key. Since the key data moved into our database last month, `app/public_api.py` can check a key itself: `api_key_workspace` hashes the bearer key with SHA-256, looks it up in `api_keys` (skipping revoked keys) and returns the key's workspace, with the same 401 body as before. Migration `0061_api_keys_lookup_index.sql` adds the index for that lookup, built `CONCURRENTLY` in a no-transaction migration so key management keeps working during the deploy. `tests/test_api_keys.py` (new) tests the dependency through `GET /api/v1/projects`.
Projects API: FastAPI 0.115 on Python 3.12 with psycopg 3 (plain SQL, a `psycopg_pool.ConnectionPool` in `app/db.py`, at most 10 connections per pod), PostgreSQL 16, one primary. `api_keys` (`id bigint` identity primary key, `workspace_id bigint NOT NULL`, `name text NOT NULL`, `key_prefix text NOT NULL` (the first 8 characters of the key, shown on the settings page), `key_hash bytea NOT NULL` (SHA-256 of the key), `created_at timestamptz NOT NULL DEFAULT now()`, `revoked_at timestamptz` (NULL while the key is active)) was copied from the auth service last month by migration `0058` and holds about 400,000 keys; a few hundred are created or revoked a day. Its only indexes are the primary key and `api_keys_workspace_idx` on `workspace_id` (the settings page lists a workspace's keys). Keys are `sk_live_` plus 32 random bytes from `secrets.token_urlsafe`, so no two keys share a hash. `projects` (`id bigint` identity primary key, `workspace_id bigint NOT NULL`, `name text NOT NULL`, `created_at timestamptz NOT NULL DEFAULT now()`) is indexed on `workspace_id`. Schema changes are plain SQL files in `migrations/`, applied by the existing runner `app/migrate.py` at the start of every deploy: files run in name order and are recorded in `schema_migrations`; each file runs as one transaction unless its first line is `-- migrate: no-transaction`, in which case each statement runs on its own in autocommit and the file is recorded after its last statement succeeds. A failed migration fails the deploy, which is then retried. `app/auth_client.py` (existing) is the legacy auth-service client; the service is switched off after this release. The test suite (`tests/conftest.py`, existing) builds a fresh, empty test database from all migrations, empties `projects` and `api_keys` before each test, and provides `client` (a FastAPI `TestClient`), `insert_project(workspace_id, name)` and `insert_api_key(workspace_id, key, revoked=False)`, which stores the key's prefix and SHA-256 hash.
Requirements
- Requests to `/api/v1/*` keep sending `Authorization: Bearer <key>`, parsed by the existing `_bearer_key`. The app now checks the key itself: it hashes the key with SHA-256 and looks the hash up in `api_keys`. A missing or malformed header, an unknown key or a revoked key (`revoked_at` set) gets 401 `{"detail": "invalid API key"}`; a valid key's `workspace_id` scopes the request.
- No call to the legacy auth service (`app.auth_client`) remains on the request path.
- The key lookup runs on every API request that carries a key (up to about 300 requests a second at peak). It must be served by an index on `api_keys`, not a scan of the table; migration `0061` adds that index.
- Keys are created and revoked while the migration runs, so it must not block writes to `api_keys`.
- Unchanged: `GET /api/v1/projects` returns the key's workspace's projects as `[{"id", "name", "created_at"}]`, ordered by `id`.
Files touched
- migrations/0061_api_keys_lookup_index.sql
- app/public_api.py
- tests/test_api_keys.py