Skip to content

Commit 087da21

Browse files
committed
test(security): pin auth fail-closed and path-class matrix (S12)
Covers empty-keys 503, AUTH_DISABLED open mode, tenant 401/200, admin key gate (tenant key rejected), and middleware-exempt node/health/metrics paths.
1 parent b616d3f commit 087da21

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"""S12 — auth fail-closed and path-class matrix (no full app lifespan).
2+
3+
Pins the three path classes AuthMiddleware implements:
4+
1. exempt — always open (health, metrics, node ingest)
5+
2. admin — middleware skip; route Depends(require_admin_key)
6+
3. tenant — requires keys or explicit AGENTFLOW_AUTH_DISABLED
7+
"""
8+
9+
from __future__ import annotations
10+
11+
from pathlib import Path
12+
13+
import pytest
14+
from fastapi import Depends, FastAPI
15+
from fastapi.testclient import TestClient
16+
17+
from src.serving.api.auth import AuthManager, build_auth_middleware, require_admin_key
18+
from src.serving.api.auth.middleware import _is_admin_path, _is_exempt_path
19+
20+
21+
def _app(tmp_path: Path, *, auth_disabled: bool = False, with_keys: bool = False) -> FastAPI:
22+
keys = tmp_path / "keys.yaml"
23+
if with_keys:
24+
keys.write_text(
25+
"keys:\n"
26+
" - key: tenant-key\n"
27+
" name: Tenant\n"
28+
" tenant: acme\n"
29+
" rate_limit_rpm: 60\n"
30+
" created_at: '2026-07-09'\n",
31+
encoding="utf-8",
32+
)
33+
else:
34+
keys.write_text("keys: []\n", encoding="utf-8")
35+
36+
app = FastAPI()
37+
app.state.auth_manager = AuthManager(
38+
api_keys_path=keys,
39+
db_path=tmp_path / "usage.duckdb",
40+
admin_key="admin-secret",
41+
)
42+
app.state.auth_manager.load()
43+
if auth_disabled:
44+
app.state.auth_disabled = True
45+
app.middleware("http")(build_auth_middleware())
46+
47+
@app.get("/v1/health")
48+
async def health() -> dict:
49+
return {"status": "ok"}
50+
51+
@app.get("/metrics/")
52+
async def metrics() -> dict:
53+
return {"ok": True}
54+
55+
@app.get("/v1/entity/order/1")
56+
async def entity() -> dict:
57+
return {"id": "1"}
58+
59+
@app.get("/v1/admin/ping", dependencies=[Depends(require_admin_key)])
60+
async def admin_ping() -> dict:
61+
return {"admin": True}
62+
63+
@app.post("/v1/node/events")
64+
async def node_events() -> dict:
65+
# Middleware-exempt; endpoint would check bearer in production.
66+
return {"accepted": True}
67+
68+
return app
69+
70+
71+
@pytest.mark.parametrize(
72+
"path",
73+
["/v1/health", "/metrics", "/metrics/", "/docs", "/openapi.json", "/v1/node/events"],
74+
)
75+
def test_exempt_path_classifier(path: str) -> None:
76+
assert _is_exempt_path(path)
77+
78+
79+
@pytest.mark.parametrize("path", ["/v1/admin/keys", "/admin/", "/admin/partials/summary"])
80+
def test_admin_path_classifier(path: str) -> None:
81+
assert _is_admin_path(path)
82+
83+
84+
def test_empty_keys_fail_closed_on_tenant_route(tmp_path: Path) -> None:
85+
client = TestClient(_app(tmp_path, with_keys=False))
86+
response = client.get("/v1/entity/order/1")
87+
assert response.status_code == 503
88+
assert "API key configuration is missing" in response.json()["detail"]
89+
90+
91+
def test_empty_keys_open_when_auth_disabled(tmp_path: Path) -> None:
92+
client = TestClient(_app(tmp_path, with_keys=False, auth_disabled=True))
93+
assert client.get("/v1/entity/order/1").status_code == 200
94+
95+
96+
def test_empty_keys_still_allows_exempt_health(tmp_path: Path) -> None:
97+
client = TestClient(_app(tmp_path, with_keys=False))
98+
assert client.get("/v1/health").status_code == 200
99+
100+
101+
def test_configured_keys_reject_missing_and_invalid(tmp_path: Path) -> None:
102+
client = TestClient(_app(tmp_path, with_keys=True))
103+
assert client.get("/v1/entity/order/1").status_code == 401
104+
assert client.get("/v1/entity/order/1", headers={"X-API-Key": "wrong"}).status_code == 401
105+
assert client.get("/v1/entity/order/1", headers={"X-API-Key": "tenant-key"}).status_code == 200
106+
107+
108+
def test_admin_route_requires_admin_key_not_tenant_key(tmp_path: Path) -> None:
109+
client = TestClient(_app(tmp_path, with_keys=True))
110+
# Middleware skips admin paths; require_admin_key is the real gate.
111+
assert client.get("/v1/admin/ping").status_code == 401
112+
assert client.get("/v1/admin/ping", headers={"X-API-Key": "tenant-key"}).status_code == 401
113+
assert client.get("/v1/admin/ping", headers={"X-Admin-Key": "wrong"}).status_code == 401
114+
assert client.get("/v1/admin/ping", headers={"X-Admin-Key": "admin-secret"}).status_code == 200
115+
116+
117+
def test_node_events_path_is_middleware_exempt(tmp_path: Path) -> None:
118+
"""Bearer check lives in the endpoint; middleware must not demand X-API-Key."""
119+
client = TestClient(_app(tmp_path, with_keys=True))
120+
assert client.post("/v1/node/events").status_code == 200

0 commit comments

Comments
 (0)