wip: apply pki-ingress-mtls groups 1-4

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 06:42:44 +03:00
co-authored by Claude Opus 5.5
parent 44fe7110a2
commit 5c514b09e8
28 changed files with 2067 additions and 60 deletions
+40
View File
@@ -1,3 +1,4 @@
import sys
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
@@ -5,6 +6,8 @@ import uvicorn
from fastapi import FastAPI
from src.api import errors
from src.api.dependencies import database_ready
from src.api.health_listener import HealthListener
from src.api.middlewares import RequestContextMiddleware
from src.api.routers import health_router, v1_router
from src.config import settings
@@ -14,6 +17,11 @@ from src.infra.db.session import engine
from src.infra.logger import AbstractLogger, get_logger
from src.infra.password import prepare_dummy_hash
from src.infra.s3.instance import object_storage
from src.infra.tls import (
PROTOCOL_IMPORT_PATH,
api_tls_problems,
ssl_context_factory,
)
from src.schemas.enums import RunModeEnum
from src.utils.constants import (
SERVICE_VERSION,
@@ -55,6 +63,17 @@ async def lifespan(_: FastAPI) -> AsyncIterator[None]:
await prepare_dummy_hash()
# модели botocore грузятся при старте, а не в первом запросе; сети нет
await object_storage.start()
# healthcheck контейнера без сертификата клиента — только loopback
# (change pki-ingress-mtls, design, решение 6); в консоли его нет
health_listener = None
if settings.app.run_mode == RunModeEnum.DOCKER:
health_listener = HealthListener(
ready=database_ready,
instance_id=settings.app.instance_id,
port=settings.app.health_port,
)
await health_listener.start()
tls = settings.api_tls
logger.info(
"Started API",
host=settings.app.host,
@@ -73,11 +92,17 @@ async def lifespan(_: FastAPI) -> AsyncIterator[None]:
uploads_wait_timeout_seconds=settings.uploads.wait_timeout_seconds,
uploads_body_timeout_seconds=settings.uploads.body_timeout_seconds,
uploads_max_unfinished_jobs=settings.uploads.max_unfinished_jobs,
# режим TLS без путей к файлам
tls_client_auth=tls.client_auth,
tls_allowed_clients=len(tls.allowed_clients) if tls.client_auth else 0,
health_port=health_listener.port if health_listener else None,
**telemetry.startup_fields(telemetry_enabled),
)
try:
yield
finally:
if health_listener is not None:
await health_listener.close()
await engine.dispose()
await cache.aclose()
await object_storage.aclose()
@@ -106,11 +131,26 @@ app = create_app()
def main() -> None:
# TLS обязателен: HTTP без TLS API не слушает ни в одном режиме; в
# docker — ещё CA, CRL, проверка клиента и allowlist (change
# pki-ingress-mtls, design, решение 10). Имена полей без значений, код 2
problems = api_tls_problems(settings.api_tls, settings.app.run_mode)
if problems:
logger.error(
"Invalid API TLS settings",
fields=[f"api_tls.{name}" for name in problems],
run_mode=settings.app.run_mode,
)
sys.exit(2)
reload = settings.app.run_mode == RunModeEnum.CONSOLE
uvicorn.run(
"src.main:app",
host=settings.app.host,
port=settings.app.port,
# свой SSLContext (CA, CRL, TLS 1.3 при проверке клиента) и протокол
# с allowlist identity клиента после рукопожатия
ssl_context_factory=ssl_context_factory,
http=PROTOCOL_IMPORT_PATH,
reload=reload,
# без reload uvicorn пишет warning на reload_dirs при каждом старте
reload_dirs=["src"] if reload else None,