forked from mtusi2026/mtusi2026lab7
11 lines
972 B
Python
11 lines
972 B
Python
"""Prove that an API replica rejects unauthenticated internal TLS clients."""
|
|
import subprocess
|
|
|
|
base = ["docker", "compose", "exec", "-T", "api1", "python", "-c"]
|
|
no_cert = "import ssl,urllib.request;c=ssl.create_default_context(cafile='/run/certs/ca.crt');c.check_hostname=False;urllib.request.urlopen('https://api1:8080/health/live',context=c,timeout=3)"
|
|
with_cert = "import ssl,urllib.request;c=ssl.create_default_context(cafile='/run/certs/ca.crt');c.check_hostname=False;c.load_cert_chain('/run/certs/lb-client.crt','/run/certs/lb-client.key');assert urllib.request.urlopen('https://api1:8080/health/live',context=c,timeout=3).status==200"
|
|
rejected = subprocess.run(base + [no_cert], capture_output=True).returncode != 0
|
|
accepted = subprocess.run(base + [with_cert], capture_output=True).returncode == 0
|
|
assert rejected and accepted, 'internal mTLS negative/positive check failed'
|
|
print('PASS: API rejects a client without certificate and accepts the LB identity.')
|