Implement Lab 7 security and infrastructure
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import logging
|
||||
|
||||
import uvicorn.protocols.http.h11_impl as h11_impl
|
||||
|
||||
|
||||
logger = logging.getLogger("app.mtls")
|
||||
|
||||
|
||||
_original_cycle = h11_impl.RequestResponseCycle
|
||||
|
||||
|
||||
class MtlsRequestResponseCycle(_original_cycle):
|
||||
def __init__(self, *args, **kwargs):
|
||||
scope = kwargs.get("scope")
|
||||
transport = kwargs.get("transport")
|
||||
|
||||
if scope is None and args:
|
||||
scope = args[0]
|
||||
|
||||
if transport is None and len(args) > 2:
|
||||
transport = args[2]
|
||||
|
||||
ssl_object = (
|
||||
transport.get_extra_info("ssl_object")
|
||||
if transport is not None
|
||||
else None
|
||||
)
|
||||
|
||||
peer_certificate = (
|
||||
ssl_object.getpeercert()
|
||||
if ssl_object is not None
|
||||
else None
|
||||
)
|
||||
|
||||
if scope is not None:
|
||||
scope.setdefault("state", {})
|
||||
scope["state"]["tls_client_cert"] = peer_certificate
|
||||
|
||||
sans = (peer_certificate or {}).get("subjectAltName", ())
|
||||
|
||||
logger.warning(
|
||||
"mTLS diagnostic: transport=%s ssl_object=%s certificate=%s uri_sans=%s",
|
||||
transport is not None,
|
||||
ssl_object is not None,
|
||||
peer_certificate is not None,
|
||||
[value for kind, value in sans if kind == "URI"],
|
||||
)
|
||||
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
|
||||
h11_impl.RequestResponseCycle = MtlsRequestResponseCycle
|
||||
Reference in New Issue
Block a user