#!/usr/bin/env python3 """Build pki/identities.json: which mTLS client identities are allowed and where. The file is consumed by: * app/relay.py — TLS relay in front of each API replica (LB -> API mTLS); * the trust-matrix audit script — prints the effective allowlist. Format: {"": {"cn": "", "serials": ["", ...], "scope": "/"|"/health/", "leaf": ""}} `scope` is the URL prefix the identity may reach on the business listener. """ import json import pathlib import ssl import sys ALLOWED = { "ingress-client": "/", "health-client": "/health/", } def cert_info(path): decoded = ssl._ssl._test_decode_cert(str(path)) # noqa: SLF001 - stdlib helper cn = dict(x[0] for x in decoded["subject"]).get("commonName", "") serial = decoded["serialNumber"] # Python versions disagree: int on some builds, hex string on others. serial = format(serial, "X") if isinstance(serial, int) else str(serial).upper().replace("0X", "", 1) return cn, serial def main(): pki = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else "pki") out = {} for leaf, scope in ALLOWED.items(): path = pki / "leaf" / (leaf + ".crt") if not path.exists(): continue cn, serial = cert_info(path) out[cn] = {"cn": cn, "serials": [serial], "scope": scope, "leaf": leaf} json.dump(out, sys.stdout, indent=2, sort_keys=True) sys.stdout.write("\n") if __name__ == "__main__": main()