#!/usr/bin/env python3 """Run the LB-to-API mTLS positive/negative probe inside the API container.""" import subprocess SCRIPT = r""" import ssl, urllib.request, urllib.error ca = "/certs/internal-ca.crt" try: c = ssl.create_default_context(cafile=ca) urllib.request.urlopen("https://api:8443/health/ready", context=c, timeout=5) raise SystemExit("no-certificate accepted") except (urllib.error.URLError, ssl.SSLError): pass print("PASS:no-cert") try: c = ssl.create_default_context(cafile=ca) c.load_cert_chain("/certs/public-server.crt", "/certs/public-server.key") urllib.request.urlopen("https://api:8443/health/ready", context=c, timeout=5) raise SystemExit("wrong-CA accepted") except (urllib.error.URLError, ssl.SSLError): pass print("PASS:wrong-CA") try: c = ssl.create_default_context(cafile=ca) c.load_cert_chain("/certs/rogue.crt", "/certs/rogue.key") urllib.request.urlopen("https://api:8443/health/ready", context=c, timeout=5) raise SystemExit("wrong-identity accepted") except urllib.error.HTTPError as error: if error.code != 401: raise print("PASS:wrong-SAN") c = ssl.create_default_context(cafile=ca) c.load_cert_chain("/certs/lb.crt", "/certs/lb.key") response = urllib.request.urlopen("https://api:8443/health/ready", context=c, timeout=5) print("PASS:LB-identity", response.status) """ def main(): result = subprocess.run( [ "docker", "compose", "--env-file", ".env", "exec", "-T", "api", "python", "-c", SCRIPT, ], check=True, cwd=str(__import__("pathlib").Path(__file__).resolve().parents[1]), stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=30, ) print(result.stdout.strip()) if __name__ == "__main__": main()