-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
293 lines (256 loc) · 10.3 KB
/
Copy pathmain.py
File metadata and controls
293 lines (256 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import asyncio
import re
import secrets
import statistics
import time
import uuid
from collections import deque
from collections.abc import AsyncGenerator
import psutil
from contextlib import asynccontextmanager
from pathlib import Path
import structlog
from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.responses import JSONResponse, RedirectResponse, Response
from fastapi.staticfiles import StaticFiles
from slowapi.errors import RateLimitExceeded
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.middleware.sessions import SessionMiddleware
from structlog.contextvars import bind_contextvars, clear_contextvars
from src.db import get_pool
from src.deps import (
HTTP_ERROR_CODES,
NeedsLogin,
_rate_limit_exceeded_handler,
error_envelope,
limiter,
require_user,
settings,
)
from src.logging_config import _release, configure_logging, configure_sentry
from src.routes import account, api as api_routes
from src.routes import auth, garmin, libre, pages
configure_logging()
logger = structlog.get_logger(__name__)
class SecurityHeadersMiddleware(BaseHTTPMiddleware):
async def dispatch(
self, request: Request, call_next: RequestResponseEndpoint
) -> Response:
nonce = secrets.token_urlsafe(16)
request.state.nonce = nonce
response = await call_next(request)
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["X-Frame-Options"] = "DENY"
response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
response.headers["Permissions-Policy"] = (
"camera=(), microphone=(), geolocation=(), payment=()"
)
response.headers["X-Permitted-Cross-Domain-Policies"] = "none"
if settings.https_only:
response.headers["Strict-Transport-Security"] = (
"max-age=31536000; includeSubDomains"
)
response.headers["Content-Security-Policy"] = (
"default-src 'self'; "
f"script-src 'nonce-{nonce}' 'strict-dynamic'; "
f"style-src 'self' 'nonce-{nonce}'; "
"font-src 'self'; "
"img-src 'self' data: https:; "
"media-src 'self' data:; "
"connect-src 'self'; "
"worker-src 'none'; "
"manifest-src 'self'; "
"frame-ancestors 'none'; "
"base-uri 'self'; "
"form-action 'self'"
)
return response
_TOKEN_PATH_RE = re.compile(r"^/(auth/(reset|verify)|account/delete/confirm)/[^/]+")
def _safe_path(path: str) -> str:
"""Replace token segments in sensitive URL paths to avoid leaking them in logs."""
return _TOKEN_PATH_RE.sub(lambda m: m.group(0).rsplit("/", 1)[0] + "/<token>", path)
_active_requests: int = 0
_error_requests: int = 0
_metrics_lock = asyncio.Lock()
_start_time: float = time.monotonic()
_duration_deque: deque[float] = deque(maxlen=1000)
# Module-level Process so cpu_percent() keeps state between scrapes. A fresh
# psutil.Process() per request would always report 0.0 on its first call.
_proc = psutil.Process()
class RequestIDMiddleware(BaseHTTPMiddleware):
async def dispatch(
self, request: Request, call_next: RequestResponseEndpoint
) -> Response:
global _active_requests, _error_requests
async with _metrics_lock:
_active_requests += 1
request_id = str(uuid.uuid4())
clear_contextvars()
bind_contextvars(request_id=request_id)
start = time.perf_counter()
try:
response = await call_next(request)
except Exception:
async with _metrics_lock:
_active_requests -= 1
_error_requests += 1
raise
else:
async with _metrics_lock:
_active_requests -= 1
if response.status_code >= 400:
async with _metrics_lock:
_error_requests += 1
duration_ms = round((time.perf_counter() - start) * 1000, 1)
_duration_deque.append(duration_ms)
logger.info(
"http.request",
method=request.method,
path=_safe_path(request.url.path),
status=response.status_code,
duration_ms=duration_ms,
active_requests=_active_requests,
)
response.headers["X-Request-ID"] = request_id
return response
class CacheControlMiddleware(BaseHTTPMiddleware):
async def dispatch(
self, request: Request, call_next: RequestResponseEndpoint
) -> Response:
response = await call_next(request)
path = request.url.path
if path.startswith("/static/") and path.endswith(".js"):
response.headers["Cache-Control"] = "no-cache"
elif path.startswith("/static/"):
response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
elif path.startswith("/api/") and request.method == "GET":
response.headers.setdefault("Cache-Control", "private, no-cache")
response.headers.setdefault("Vary", "Cookie")
elif request.method in ("POST", "PUT", "PATCH", "DELETE"):
response.headers.setdefault("Cache-Control", "no-store")
return response
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: # pragma: no cover
pool = await get_pool()
logger.info("db.pool_initialized")
# Prime cpu_percent() so the first /api/metrics scrape reports a real value.
_proc.cpu_percent(interval=None)
try:
from cryptography.fernet import Fernet
Fernet(settings.fernet_key.encode())
except ValueError as e:
raise ValueError("FERNET_KEY invalid — must be 32-byte URL-safe base64") from e
from sentry_sdk.integrations.fastapi import FastApiIntegration
from sentry_sdk.integrations.starlette import StarletteIntegration
configure_sentry(
settings, integrations=[StarletteIntegration(), FastApiIntegration()]
)
yield
await pool.close()
logger.info("db.pool_closed")
app = FastAPI(title="PulseBase API", version=_release(), lifespan=lifespan)
app.mount(
"/static",
StaticFiles(directory=str(Path(__file__).parent / "static")),
name="static",
)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) # type: ignore[arg-type]
# Middleware-Reihenfolge: last added = outermost (executes first)
app.add_middleware(GZipMiddleware, minimum_size=1000)
app.add_middleware(CacheControlMiddleware)
app.add_middleware(SecurityHeadersMiddleware)
app.add_middleware(RequestIDMiddleware)
app.add_middleware(
SessionMiddleware,
secret_key=settings.session_secret,
https_only=settings.https_only,
same_site="strict",
max_age=3600,
)
@app.exception_handler(NeedsLogin)
async def needs_login_handler(request: Request, exc: NeedsLogin) -> RedirectResponse:
return RedirectResponse("/login", status_code=303)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
request: Request, exc: RequestValidationError
) -> JSONResponse:
# Normalise Pydantic/FastAPI 422s into the unified error envelope. We map
# only `loc` + `msg` and deliberately DROP `input`/`ctx`: Pydantic echoes the
# offending value, which on auth POSTs (/login, /register, /auth/reset/*) is
# the client's plaintext password. Keeping it would leak it into both the
# response body and the Sentry event. Security invariant: no client-submitted
# value ever leaves the server via an error response.
details = [
{"field": ".".join(str(p) for p in err["loc"]), "msg": err["msg"]}
for err in exc.errors()
]
return JSONResponse(
status_code=422,
content=error_envelope("VALIDATION_ERROR", "Eingabe ungültig", details),
)
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(
request: Request, exc: StarletteHTTPException
) -> JSONResponse:
# Unify raised HTTPExceptions (e.g. 403 CSRF, 404, 405) into the envelope.
# Only exc.detail (a developer-controlled string) becomes the message — never
# internal state. Preserve headers so 401 challenges / 405 Allow survive.
code = HTTP_ERROR_CODES.get(exc.status_code, "ERROR")
message = exc.detail if isinstance(exc.detail, str) else "Request failed"
return JSONResponse(
status_code=exc.status_code,
content=error_envelope(code, message),
headers=getattr(exc, "headers", None),
)
@app.get("/health")
async def health() -> dict[str, str]:
return {"status": "ok"}
@app.get("/api/metrics")
async def app_metrics(request: Request) -> dict[str, float | int | None]:
await require_user(request)
async with _metrics_lock:
active = _active_requests
errors = _error_requests
pool = await get_pool()
pool_size = pool.get_size()
pool_idle = pool.get_idle_size()
samples = list(_duration_deque)
p95 = (
round(statistics.quantiles(samples, n=20)[18], 1)
if len(samples) >= 20
else None
)
return {
"active_requests": active,
"error_requests_total": errors,
"uptime_seconds": round(time.monotonic() - _start_time),
"memory_mb": round(_proc.memory_info().rss / 1024 / 1024, 1),
"cpu_percent": _proc.cpu_percent(interval=None),
"db_pool_used": pool_size - pool_idle,
"db_pool_max": pool_size,
"p95_duration_ms": p95,
}
@app.get("/ready", response_model=None)
async def ready() -> JSONResponse | dict[str, str]:
try:
pool = await get_pool()
await pool.fetchval("SELECT 1")
migrations_ok = await pool.fetchval(
"SELECT COUNT(*) FROM flyway_schema_history WHERE success = TRUE"
)
if not migrations_ok:
return JSONResponse(status_code=503, content={"status": "no_migrations"})
return {"status": "ready"}
except Exception:
logger.exception("readiness.check_failed")
return JSONResponse(status_code=503, content={"status": "unavailable"})
app.include_router(auth.router)
app.include_router(account.router)
app.include_router(garmin.router)
app.include_router(libre.router)
app.include_router(pages.router)
app.include_router(api_routes.router)