31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
"""Uvicorn TLS server that exposes the verified peer certificate to ASGI."""
|
|
import ssl
|
|
|
|
import uvicorn
|
|
from uvicorn.protocols.http.h11_impl import H11Protocol
|
|
|
|
|
|
class CertificateAwareH11Protocol(H11Protocol):
|
|
def handle_events(self):
|
|
previous_cycle=getattr(self,'cycle',None)
|
|
super().handle_events()
|
|
cycle=getattr(self,'cycle',None)
|
|
if cycle is None or cycle is previous_cycle or cycle.scope.get('type')!='http':
|
|
return
|
|
ssl_object=self.transport.get_extra_info('ssl_object')
|
|
cert=ssl_object.getpeercert(binary_form=True) if ssl_object is not None else None
|
|
cycle.scope.setdefault('extensions',{})['tls']={
|
|
'client_cert_chain':[ssl.DER_cert_to_PEM_cert(cert)] if cert else []
|
|
}
|
|
|
|
|
|
def main():
|
|
uvicorn.run('app.main:app',host='0.0.0.0',port=8443,http=CertificateAwareH11Protocol,
|
|
ssl_certfile='/run/certs/api.crt',ssl_keyfile='/run/certs/api.key',
|
|
ssl_ca_certs='/run/certs/lb-client-ca.crt',ssl_cert_reqs=ssl.CERT_REQUIRED,
|
|
timeout_graceful_shutdown=5,proxy_headers=False,forwarded_allow_ips='')
|
|
|
|
|
|
if __name__=='__main__':
|
|
main()
|