37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
"""TLS settings shared by application roles. Every network client verifies its peer."""
|
|
import os
|
|
import ssl
|
|
from pathlib import Path
|
|
|
|
TLS = Path("/run/identity")
|
|
CA = str(TLS / "ca.crt")
|
|
CERT = str(TLS / "service.crt")
|
|
KEY = str(TLS / "service.key")
|
|
|
|
|
|
def client_context(identity=True):
|
|
context = ssl.create_default_context(cafile=CA)
|
|
context.minimum_version = ssl.TLSVersion.TLSv1_2
|
|
if identity:
|
|
context.load_cert_chain(CERT, KEY)
|
|
return context
|
|
|
|
|
|
def server_context():
|
|
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
|
context.minimum_version = ssl.TLSVersion.TLSv1_2
|
|
context.options |= ssl.OP_NO_TICKET
|
|
context.load_cert_chain(CERT, KEY)
|
|
context.load_verify_locations(CA)
|
|
context.verify_mode = ssl.CERT_REQUIRED
|
|
return context
|
|
|
|
|
|
def accept_ingress(peer):
|
|
allowed = (TLS / "ingress-serials").read_text().split()
|
|
return ("URI", "spiffe://lab7/ingress") in peer.get("subjectAltName", ()) and peer["serialNumber"] in allowed
|
|
|
|
|
|
def database_url():
|
|
return os.environ["DATABASE_URL"]
|