"""Run inside the Compose network using temporary test identities.""" import socket import ssl def call(host, cert=None): context = ssl.create_default_context(cafile="/certs/ca.crt") if cert: context.load_cert_chain(cert) with socket.create_connection((host, 8444), timeout=5) as raw: with context.wrap_socket(raw, server_hostname=host) as conn: conn.sendall(b"GET /health/ready HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n") return conn.recv(4096).split(b"\r\n", 1)[0] for host in ("api-a", "api-b"): assert b" 200 " in call(host, "/certs/lb.pem"), f"{host}: valid LB certificate rejected" for certificate in (None, "/certs/foreign.pem"): try: result = call(host, certificate) except (ssl.SSLError, ConnectionResetError, BrokenPipeError): continue raise AssertionError(f"{host}: bad certificate unexpectedly received {result!r}") assert b" 403 " in call(host, "/certs/wrong-role.pem"), f"{host}: wrong role not denied" try: socket.create_connection((host, 8080), timeout=3).close() except ConnectionRefusedError: pass else: raise AssertionError(f"{host}: application port reachable without mTLS") print(f"PASS {host}: valid identity accepted; no cert, foreign CA and wrong role rejected; direct API closed")