Files
mtusi2026lab7-fork/app/core/tls.py
T

21 lines
838 B
Python

"""Проверка TLS на внутреннем входе каждой реплики."""
import asyncio
import ssl
from uvicorn.protocols.http.h11_impl import H11Protocol
class LoadBalancerProtocol(H11Protocol):
def connection_made(self, transport: asyncio.Transport) -> None:
# TLS уже проверил цепочку и clientAuth. SAN ограничивает роль клиента.
super().connection_made(transport)
tls = transport.get_extra_info("ssl_object")
certificate = tls.getpeercert() if tls else None
if not certificate or ("DNS", "load-balancer") not in certificate.get("subjectAltName", ()):
transport.abort()
def client_context(ca_file: str | None) -> ssl.SSLContext | None:
return ssl.create_default_context(cafile=ca_file) if ca_file else None