Plan: 009 — Extend API Authentication¶
Approach¶
Three small, surgical layers — each preserves spec 008's posture and re-uses its primitives:
- Generalize the auth helper —
app/core/security.pyaddsmake_api_key_dep(*setting_names)returning a freshDepends-able callable. The existingrequire_api_keycollapses to one line:require_api_key = make_api_key_dep("AGENT_API_KEY"). Behaviour for spec 008 callers is byte-identical. - Mount per-router —
app/main.pyadds three new dependencies, one per feature router. Each router resolves its own setting first and falls back toAGENT_API_KEYwhen its override is empty. - Frontend single-helper refactor —
frontend/lib/api.tsconsolidates the threefetch(...)styles (raw POST, raw GET,getJson,agentFetch) onto oneapiFetch(path, init?). The function attachesX-API-KeyfromNEXT_PUBLIC_AGENT_API_KEYwhenever it is set.agentFetchbecomes a one-line alias to keep call sites unchanged.
File Layout¶
backend/app/core/security.py (edit — add make_api_key_dep; require_api_key stays)
backend/app/core/config.py (edit — add three per-router settings)
backend/app/main.py (edit — three new include_router deps + expanded warning)
backend/tests/conftest.py (edit — autouse fixture neutralises *_API_KEY env)
backend/tests/core/test_security.py (edit — add factory cases incl. precedence + AC-8)
backend/tests/api/test_analysis.py (edit — +AC-1..AC-4 cases)
backend/tests/api/test_graph.py (edit — +AC-1..AC-4 cases)
backend/tests/api/test_lsat.py (edit — +AC-1..AC-6 cases incl. override + isolation)
frontend/lib/api.ts (edit — apiFetch consolidates fetch + getJson + agentFetch)
frontend/__tests__/components/AnalysisStudio.test.tsx (edit — assert X-API-Key when env set)
frontend/__tests__/components/KnowledgeGraph.test.tsx (edit — same)
frontend/__tests__/components/AgentDashboard.test.tsx (existing — unchanged behaviour, still asserts header)
.env.example (edit — add ANALYSIS/GRAPH/LSAT keys)
Why These Choices¶
- Factory over multiple bespoke dependencies: keeps the auth surface to one helper. Adding a future router is one new
include_router(...)line + (optionally) one new setting. - Override-then-fallback resolution (FR-1: first non-empty wins): operators get the dual model "set one key, get one gate" and "set per-router keys, get isolation" with no extra code. Matches spec 008 R-3 explicitly.
require_api_keypreserved as alias: every spec-008 test passes with no edits — protects NFR-COMPAT-1 and the audit gate.- Single
apiFetchon the frontend: today there are four flavours offetch(...)inlib/api.ts. Folding them into one removes accidental drift if a future feature wires a new endpoint and forgets the header. - Autouse
_reset_api_keysfixture inconftest.py: hermetic — protects every existing test from a developer's stray.envand from the new gates.
Provenance & Audit Hook-up¶
No new provenance events. The existing provenance_middleware (spec 003) already tees every request — including 401s — into audit_store. Operators continue to see denied requests in /api/agents/runs. The X-API-Key header is never read by the provenance layer (NFR-SEC-2 carried over from spec 008).
HITL Gates¶
None. This spec does not change any HITL path; the existing ingestion-quarantine gate (spec 004) and the analysis HITL flag (spec 007) remain untouched.
Test Strategy¶
- Unit (
test_security.py): extend with parametrized cases formake_api_key_dep: - Single name, all four spec-008 branches (proves FR-2 / AC-8).
- Two names with override set → expected key is the override (AC-6).
- Two names with only fallback set → expected key is the fallback (AC-2).
- All names empty → open (AC-1).
- API (
test_analysis.py,test_graph.py,test_lsat.py): add awith_shared_keyfixture usingmonkeypatch.setattr(settings, "AGENT_API_KEY", ...); reuse the spec-008_isolate_auditpattern. Cover AC-1..AC-4 in each module; AC-5 + AC-6 + isolation intest_lsat.py. - Health (
test_health.py): extend (or add) one test that proves AC-7 —GET /healthreturns 200 even when every key is set. - Startup warning (AC-9): extend the spec-008 warning test to assert the message lists all four router groups.
- Frontend (
AnalysisStudio.test.tsx,KnowledgeGraph.test.tsx): add MSW handlers that capturerequest.headers.get('x-api-key')and assert it equals's3cret'whenvi.stubEnv('NEXT_PUBLIC_AGENT_API_KEY', 's3cret')is active. Keep the env-unset case to prove no header is sent. - No LegalBench delta needed (no reasoning code touched).
Rollout¶
- Local dev: no action — keys unset → behaviour unchanged.
- Staging / tailnet: operator sets
AGENT_API_KEY(and optionally per-router overrides) in backend.env, setsNEXT_PUBLIC_AGENT_API_KEYin frontend.env, restarts.docker compose up --buildpicks both up. - No backfill, no migration, no flag beyond the keys themselves.
Risks & Mitigations¶
| Risk | Likelihood | Impact | Mitigation |
|---|---|---|---|
Stray dev .env leaks an AGENT_API_KEY into test runs |
med | high (suite goes red) | Autouse _reset_api_keys fixture in backend/tests/conftest.py. |
Operator confusion: "I only set AGENT_API_KEY for the dashboard, why is /api/analysis 401?" |
med | low | Explicit callout in .env.example + PR description; documented in spec §12 R-2. |
| Frontend page breaks because we forgot to thread the header through one call | low | med | Single apiFetch helper used by every outgoing call; vitest assertions on each gated suite. |
Estimated Effort¶
Small. ~250 LOC total across backend + frontend + tests. No human pairing required.