Files
mtusi2026lab7/app/serve_tls.py
T

36 lines
1.0 KiB
Python

"""Require a verified certificate with the ingress identity before parsing HTTP."""
import ssl
import uvicorn
from uvicorn.protocols.http.h11_impl import H11Protocol
class IngressOnlyProtocol(H11Protocol):
def connection_made(self, transport):
super().connection_made(transport)
tls = transport.get_extra_info("ssl_object")
if tls is None or ("URI", "spiffe://lab7/ingress") not in tls.getpeercert().get(
"subjectAltName", ()
):
transport.close()
return
if __name__ == "__main__":
config = uvicorn.Config(
"app.main:app",
host="0.0.0.0",
port=8443,
http=IngressOnlyProtocol,
proxy_headers=False,
access_log=False,
ssl_certfile="/run/identity/server.crt",
ssl_keyfile="/run/identity/server.key",
ssl_ca_certs="/run/ca/ca.crt",
ssl_cert_reqs=ssl.CERT_REQUIRED,
timeout_graceful_shutdown=5,
)
config.load()
config.ssl.minimum_version = ssl.TLSVersion.TLSv1_2
uvicorn.Server(config).run()