#!/usr/bin/env python3 """Test certificate-based OpenBao entrypoint in Docker.""" import json import subprocess import sys from pathlib import Path PROJECT = Path(__file__).resolve().parent.parent PKI = Path.home() / ".local/share/mtusi2026lab7/pki" SERVICES = { "api1": ( "lab7/data/api/runtime", ("DATABASE_URL", "S3_ACCESS_KEY", "S3_SECRET_KEY"), ), "api2": ( "lab7/data/api/runtime", ("DATABASE_URL", "S3_ACCESS_KEY", "S3_SECRET_KEY"), ), "worker": ( "lab7/data/worker/runtime", ( "DATABASE_URL", "RABBITMQ_USER", "RABBITMQ_PASSWORD", "S3_ACCESS_KEY", "S3_SECRET_KEY", ), ), } def project_network(): container_id = subprocess.check_output( ["docker", "compose", "ps", "-q", "openbao"], cwd=PROJECT, text=True, ).strip() if not container_id: raise RuntimeError("OpenBao is not running") details = json.loads( subprocess.check_output( ["docker", "inspect", container_id], text=True, ) )[0] for name in details["NetworkSettings"]["Networks"]: if name.endswith("_default"): return name raise RuntimeError("Project Docker network not found") def mount(source, target): if not source.is_file(): raise RuntimeError( "Required file missing: " + source.name ) return ( f"type=bind,source={source}," f"target={target},readonly" ) def verify(service, secret_path, required, network): print(f"=== {service} ===", flush=True) cert = PKI / "openbao-clients" / f"{service}.crt" key = PKI / "openbao-clients" / f"{service}.key" child_code = ( "import os\n" f"required = {required!r}\n" "assert all(" "isinstance(os.environ.get(k), str) " "and os.environ[k] for k in required" ")\n" "print('PASS: child process received runtime credentials')\n" ) command = [ "docker", "run", "--rm", "--network", network, "--mount", mount( PKI / "ca.crt", "/etc/ssl/lab7/ca.crt", ), "--mount", mount( cert, "/run/lab7/bao/client.crt", ), "--mount", mount( key, "/run/lab7/bao/client.key", ), "--mount", mount( PROJECT / "src/openbao_cert_client.py", "/app/src/openbao_cert_client.py", ), "--mount", mount( PROJECT / "src/openbao_entrypoint.py", "/app/src/openbao_entrypoint.py", ), "-e", "BAO_ADDR=https://openbao:8200", "-e", "BAO_CACERT=/etc/ssl/lab7/ca.crt", "-e", "BAO_CERT_FILE=/run/lab7/bao/client.crt", "-e", "BAO_KEY_FILE=/run/lab7/bao/client.key", "-e", f"BAO_CERT_NAME=lab7-{service}", "-e", f"BAO_SECRET_PATH={secret_path}", "--entrypoint", "python", f"mtusi2026lab7-{service}", "-m", "src.openbao_entrypoint", "--", "python", "-c", child_code, ] result = subprocess.run( command, capture_output=True, text=True, timeout=90, ) if result.returncode != 0: raise RuntimeError( f"{service}: container exit code {result.returncode}" ) expected = ( "PASS: runtime credentials loaded from OpenBao", "PASS: child process received runtime credentials", ) for line in expected: if line not in result.stdout: raise RuntimeError( f"{service}: expected result missing" ) print(line, flush=True) print( f"PASS: {service} certificate entrypoint verified", flush=True, ) def main(): network = project_network() for service, (secret_path, required) in SERVICES.items(): verify(service, secret_path, required, network) print("PASS: three certificate entrypoint tests completed") print("PASS: running containers unchanged") print("PASS: no secret values or tokens printed") if __name__ == "__main__": try: main() except Exception as exc: print( "FAIL: certificate entrypoint test:", type(exc).__name__, file=sys.stderr, ) raise SystemExit(1)