lab7: обязательное mTLS LB->API с allowlist identity (SAN lb), health только на loopback, скрипт отрицательных проверок mtls_check
This commit is contained in:
+29
-11
@@ -22,7 +22,7 @@ from starlette.types import ASGIApp, Message as AsgiMessage, Receive, Scope, Sen
|
||||
from opentelemetry import context as otel_context
|
||||
from opentelemetry import trace
|
||||
|
||||
from . import attachments, authhdr, cursor, docs, passwords, repo, telemetry, validation
|
||||
from . import attachments, authhdr, cursor, docs, passwords, repo, telemetry, tls, validation
|
||||
from .config import Settings
|
||||
from .db import Database
|
||||
from .errors import BASIC_CHALLENGE, ApiError, Conflict, InvalidRequest, NotFound, PayloadTooLarge, Unauthorized, Unavailable
|
||||
@@ -275,19 +275,28 @@ async def ready(request: Request) -> JSONResponse:
|
||||
"""200 only if the WHOLE mandatory synchronous path can be served: PostgreSQL, Valkey
|
||||
(SESSION_STORE=valkey) and, since lab 4, the S3 bucket (upload/download of attachments)
|
||||
answer. 503 while draining after SIGTERM. HAProxy routes to such replicas first."""
|
||||
if request.app.state.draining:
|
||||
raise Unavailable("Сервер завершает работу")
|
||||
db_ok, store_ok, s3_ok = await asyncio.gather(db_of(request).ping(), sessions_of(request).ping(),
|
||||
storage_of(request).ping())
|
||||
if not db_ok:
|
||||
raise Unavailable("PostgreSQL недоступен")
|
||||
if not store_ok:
|
||||
raise Unavailable("Хранилище сессий недоступно")
|
||||
if not s3_ok:
|
||||
raise Unavailable("Хранилище файлов (S3) недоступно")
|
||||
problem = await readiness_problem(request.app)
|
||||
if problem is not None:
|
||||
raise Unavailable(problem)
|
||||
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)."""
|
||||
if app.state.draining:
|
||||
return "Сервер завершает работу"
|
||||
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:
|
||||
return "PostgreSQL недоступен"
|
||||
if not store_ok:
|
||||
return "Хранилище сессий недоступно"
|
||||
if not s3_ok:
|
||||
return "Хранилище файлов (S3) недоступно"
|
||||
return None
|
||||
|
||||
|
||||
async def ready_core(request: Request) -> JSONResponse:
|
||||
"""Not in the contract: readiness WITHOUT S3, for the load balancer's degraded mode only.
|
||||
If S3 is down for every replica, /health/ready is 503 everywhere; HAProxy then routes to
|
||||
@@ -633,9 +642,18 @@ def create_app(settings: Settings | None = None) -> Starlette:
|
||||
tasks.append(asyncio.create_task(janitor_loop(settings.janitor_interval_seconds)))
|
||||
if settings.stats_log_seconds > 0:
|
||||
tasks.append(asyncio.create_task(stats_loop(settings.stats_log_seconds)))
|
||||
health_server = None
|
||||
if settings.health_port:
|
||||
async def probe(path: str) -> int:
|
||||
if path == "/health/live":
|
||||
return 200
|
||||
return 200 if await readiness_problem(app) is None else 503
|
||||
health_server = await tls.health_listener(settings.health_port, probe)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
if health_server is not None:
|
||||
health_server.close()
|
||||
for task in tasks:
|
||||
task.cancel()
|
||||
await sessions.close()
|
||||
|
||||
Reference in New Issue
Block a user