#!/usr/bin/env python3 """Verify OpenBao certificate auth and least-privilege ACLs.""" import json import ssl import sys import urllib.error import urllib.request from pathlib import Path BASE = Path.home() / ".local/share/mtusi2026lab7/pki" URL = "https://localhost:8200/v1" SERVICES = { "api1": { "policy": "messenger-api", "own": "api", "other": "worker", }, "api2": { "policy": "messenger-api", "own": "api", "other": "worker", }, "worker": { "policy": "messenger-worker", "own": "worker", "other": "api", }, } def request(method, path, tls, token=None, payload=None): headers = {} body = None if token is not None: headers["X-Vault-Token"] = token if payload is not None: body = json.dumps(payload).encode("utf-8") headers["Content-Type"] = "application/json" req = urllib.request.Request( URL + path, headers=headers, data=body, method=method, ) try: with urllib.request.urlopen( req, context=tls, timeout=15, ) as response: raw = response.read() result = json.loads(raw) if raw else {} return response.status, result except urllib.error.HTTPError as exc: # Do not print HTTP response bodies: they may contain # security-sensitive information. return exc.code, {} def verify_service(name, config): cert = BASE / "openbao-clients" / f"{name}.crt" key = BASE / "openbao-clients" / f"{name}.key" tls = ssl.create_default_context( cafile=str(BASE / "ca.crt") ) tls.load_cert_chain( certfile=str(cert), keyfile=str(key), ) print(f"=== {name} ===", flush=True) status, result = request( "POST", "/auth/cert/login", tls, payload={"name": f"lab7-{name}"}, ) if status != 200: raise RuntimeError( f"{name}: certificate login HTTP {status}" ) auth = result["auth"] token = auth["client_token"] print("PASS: certificate authentication successful") try: policies = set(auth.get("policies", [])) expected = {config["policy"]} if policies != expected: raise RuntimeError( f"{name}: unexpected token policies" ) print("PASS: only scoped service policy assigned") own_path = ( f"/lab7/data/{config['own']}/runtime" ) other_path = ( f"/lab7/data/{config['other']}/runtime" ) status, result = request( "GET", own_path, tls, token=token, ) if status != 200: raise RuntimeError( f"{name}: own secret read HTTP {status}" ) secret = result.get("data", {}).get("data", {}) if not isinstance(secret, dict) or not secret: raise RuntimeError( f"{name}: own secret data missing" ) print("PASS: own KV v2 secret readable") status, _ = request( "GET", other_path, tls, token=token, ) if status != 403: raise RuntimeError( f"{name}: cross-service read HTTP {status}" ) print("PASS: cross-service KV read denied") status, _ = request( "POST", own_path, tls, token=token, payload={ "data": { "acl_probe": "should-not-be-written" } }, ) if status != 403: raise RuntimeError( f"{name}: unauthorized write HTTP {status}" ) print("PASS: KV modification denied") finally: status, _ = request( "POST", "/auth/token/revoke-self", tls, token=token, payload={}, ) if status not in (200, 204): raise RuntimeError( f"{name}: token revocation HTTP {status}" ) print("PASS: temporary token revoked") def main(): for name, config in SERVICES.items(): verify_service(name, config) print("PASS: three certificate identities verified") print("PASS: scoped OpenBao certificate ACL tests completed") print("PASS: no secret values or tokens printed") if __name__ == "__main__": try: main() except Exception as exc: print( "FAIL: certificate ACL test:", type(exc).__name__, str(exc) if isinstance(exc, RuntimeError) else "", file=sys.stderr, ) raise SystemExit(1)