88 lines
3.2 KiB
Python
88 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Internal mTLS audit: LB -> API identity checks (lab7, grade 3/4).
|
|
|
|
The API replica has no published port: it is reachable only from the compose
|
|
network on 8443, where a per-replica nginx terminates mTLS. This script runs a
|
|
probe inside the network (using the api service image) and asserts the four
|
|
required outcomes from the trust matrix:
|
|
|
|
no client certificate -> rejected
|
|
certificate from a rogue CA -> rejected
|
|
project CA, forbidden CN -> rejected (HTTP 403)
|
|
project CA, identity CN=lb -> accepted (HTTP 200)
|
|
|
|
The probe never disables certificate verification.
|
|
|
|
Run: python scripts/check-internal-mtls.py
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
os.chdir(ROOT)
|
|
|
|
PROBE = r'''
|
|
import http.client, ssl
|
|
|
|
def probe(name, certfile, keyfile, expect):
|
|
ctx = ssl.create_default_context(cafile="/pki/ca.crt")
|
|
ctx.check_hostname = True
|
|
ctx.verify_mode = ssl.CERT_REQUIRED
|
|
if certfile:
|
|
ctx.load_cert_chain(certfile, keyfile)
|
|
try:
|
|
conn = http.client.HTTPSConnection("api", 8443, context=ctx, timeout=5)
|
|
conn.request("GET", "/health/live")
|
|
resp = conn.getresponse(); resp.read()
|
|
print("%s: HTTP %d expect=%s" % (name, resp.status, expect))
|
|
except ssl.SSLError as exc:
|
|
print("%s: TLS-REJECT %s expect=%s" % (name, type(exc).__name__, expect))
|
|
except Exception as exc:
|
|
print("%s: ERROR %s expect=%s" % (name, type(exc).__name__, expect))
|
|
|
|
probe("no_client", None, None, "reject")
|
|
probe("rogue_ca", "/pki/rogue-client.crt", "/pki/rogue-client.key", "reject")
|
|
probe("forbidden_cn", "/pki/evil-client.crt", "/pki/evil-client.key", "reject")
|
|
probe("lb_identity", "/pki/lb-client.crt", "/pki/lb-client.key", "accept")
|
|
'''
|
|
|
|
# nginx reports a missing or unverifiable client certificate as HTTP 400 and a
|
|
# valid certificate with a forbidden identity as HTTP 403; both are rejections.
|
|
EXPECT = {
|
|
"no_client": lambda out: "HTTP 400" in out or "TLS-REJECT" in out,
|
|
"rogue_ca": lambda out: "HTTP 400" in out or "TLS-REJECT" in out,
|
|
"forbidden_cn": lambda out: "HTTP 403" in out,
|
|
"lb_identity": lambda out: "HTTP 200" in out,
|
|
}
|
|
|
|
|
|
def main() -> int:
|
|
cmd = [
|
|
"docker", "compose", "--env-file", ".env", "run", "--rm", "--no-deps", "-T",
|
|
"-v", os.path.join(ROOT, "secrets", "pki") + ":/pki:ro",
|
|
"api", "python", "-c", PROBE,
|
|
]
|
|
proc = subprocess.run(cmd, capture_output=True, text=True)
|
|
output = (proc.stdout + proc.stderr).strip()
|
|
print(output)
|
|
failures = 0
|
|
for line in output.splitlines():
|
|
for name, check in EXPECT.items():
|
|
if line.startswith(name + ":"):
|
|
if not check(line):
|
|
print(" MISMATCH:", line)
|
|
failures += 1
|
|
found = sum(1 for line in output.splitlines() if line.split(":")[0] in EXPECT)
|
|
if found != 4:
|
|
print(" expected 4 probe results, saw", found)
|
|
failures += 1
|
|
print("PASS: internal mTLS identity checks" if failures == 0 else "FAIL: %d mismatch(es)" % failures)
|
|
return 1 if failures else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|