lab7: тесты безопасности и перевод своих тестов на HTTPS, документы (trust-matrix, THREAT_MODEL, OPERATOR, DEFENSE, WINDOWS, PR), сборщик доказательств, чистка legacy bench

This commit is contained in:
Alexey Bodanyuk
2026-11-27 17:43:14 +03:00
parent 05df1695fe
commit 84dbdb4735
44 changed files with 1644 additions and 2686 deletions
+12 -9
View File
@@ -281,11 +281,15 @@ async def ready(request: Request) -> JSONResponse:
return ok({"status": "ok"})
async def readiness_problem(app: Starlette) -> str | None:
"""None if ready, otherwise the (public, secret-free) reason. Shared by GET /health/ready and the
loopback health listener of the Docker healthcheck (tls.health_listener, lab 7)."""
async def readiness_problem(app: Starlette, core: bool = False) -> str | None:
"""None if ready, otherwise the (public, secret-free) reason. Shared by GET /health/ready(/core)
and the loopback health listener of the Docker healthcheck (tls.health_listener, lab 7).
core=True: without S3 (the LB's degraded mode, see ready_core)."""
if app.state.draining:
return "Сервер завершает работу"
if core:
db_ok, store_ok = await asyncio.gather(app.state.db.ping(), app.state.sessions.ping())
return None if db_ok and store_ok else "PostgreSQL или хранилище сессий недоступно"
db_ok, store_ok, s3_ok = await asyncio.gather(app.state.db.ping(), app.state.sessions.ping(),
app.state.storage.ping())
if not db_ok:
@@ -302,11 +306,9 @@ async def ready_core(request: Request) -> JSONResponse:
If S3 is down for every replica, /health/ready is 503 everywhere; HAProxy then routes to
replicas that are at least core-ready, so chats and messages keep working while every
attachment request answers 503 itself (see infra/haproxy/haproxy.cfg)."""
if request.app.state.draining:
raise Unavailable("Сервер завершает работу")
db_ok, store_ok = await asyncio.gather(db_of(request).ping(), sessions_of(request).ping())
if not (db_ok and store_ok):
raise Unavailable("PostgreSQL или хранилище сессий недоступно")
problem = await readiness_problem(request.app, core=True)
if problem is not None:
raise Unavailable(problem)
return ok({"status": "ok"})
@@ -672,7 +674,8 @@ def create_app(settings: Settings | None = None) -> Starlette:
async def probe(path: str) -> int:
if path == "/health/live":
return 200
return 200 if await readiness_problem(app) is None else 503
problem = await readiness_problem(app, core=path == "/health/ready/core")
return 200 if problem is None else 503
health_server = await tls.health_listener(settings.health_port, probe)
try:
yield
+6 -3
View File
@@ -96,11 +96,14 @@ def protocol_class() -> type[MtlsHttpProtocol]:
# ------------------------------------------------------------------ loopback health listener
HEALTH_PATHS = ("/health/live", "/health/ready", "/health/ready/core")
async def health_listener(port: int, probe: Callable[[str], Awaitable[int]]) -> asyncio.base_events.Server:
"""Minimal HTTP/1.0 server on 127.0.0.1:<port> for the Docker healthcheck only.
`probe(path)` returns the status code of /health/live or /health/ready (the same checks as the
API endpoints). Any other path gets 404 and nothing else of the API is reachable here.
`probe(path)` returns the status code of /health/live, /health/ready or /health/ready/core (the
same checks as the API endpoints). Any other path gets 404: nothing else of the API is here.
"""
async def handle(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
@@ -109,7 +112,7 @@ async def health_listener(port: int, probe: Callable[[str], Awaitable[int]]) ->
line = await asyncio.wait_for(reader.readline(), 2)
parts = line.decode("latin-1").split()
if len(parts) >= 2 and parts[0] == "GET":
status = await probe(parts[1]) if parts[1] in ("/health/live", "/health/ready") else 404
status = await probe(parts[1]) if parts[1] in HEALTH_PATHS else 404
except Exception: # noqa: BLE001 - a broken probe connection is the prober's problem
status = 503
body = b'{"status":"ok"}' if status == 200 else b'{"status":"unavailable"}'