#!/usr/bin/env python3 import argparse import json import ssl import urllib.error import urllib.request from pathlib import Path SERVICES = ("api", "worker", "publisher", "migrate", "reconciler") class BaoClient: def __init__(self, address: str, ca_file: str): self.address = address.rstrip("/") self.context = ssl.create_default_context(cafile=ca_file) def request(self, method: str, path: str, payload=None, token: str = ""): body = json.dumps(payload).encode("utf-8") if payload is not None else None headers = {"Content-Type": "application/json"} if token: headers["X-Vault-Token"] = token request = urllib.request.Request( self.address + "/v1/" + path, data=body, headers=headers, method=method, ) try: with urllib.request.urlopen(request, context=self.context, timeout=5) as response: return response.status, json.loads(response.read(1024 * 1024)) except urllib.error.HTTPError as error: return error.code, {} except (urllib.error.URLError, TimeoutError, OSError, ValueError) as error: raise RuntimeError(f"OpenBao request failed ({type(error).__name__})") from None def main() -> None: parser = argparse.ArgumentParser(description="Check service AppRole path isolation.") parser.add_argument("--address", default="https://localhost:8200") parser.add_argument("--ca-file", required=True) parser.add_argument("--credential-dir", default="secrets/openbao") args = parser.parse_args() credential_dir = Path(args.credential_dir) client = BaoClient(args.address, args.ca_file) for service in SERVICES: role_id = (credential_dir / f"{service}_role_id").read_text(encoding="utf-8").strip() secret_id = (credential_dir / f"{service}_secret_id").read_text(encoding="utf-8").strip() status, response = client.request( "POST", "auth/approle/login", {"role_id": role_id, "secret_id": secret_id}, ) token = response.get("auth", {}).get("client_token") if status != 200 or not token: raise RuntimeError(f"{service} AppRole authentication failed with HTTP {status}") lease_duration = response.get("auth", {}).get("lease_duration") if not isinstance(lease_duration, int) or not 1 <= lease_duration <= 3600: raise RuntimeError(f"{service} AppRole returned an invalid token TTL") own_status, _ = client.request( "GET", f"secret/data/messenger/{service}", token=token, ) if own_status != 200: raise RuntimeError(f"{service} cannot read its own scoped KV path (HTTP {own_status})") for other in SERVICES: if other == service: continue denied_status, _ = client.request( "GET", f"secret/data/messenger/{other}", token=token, ) if denied_status != 403: raise RuntimeError( f"{service} AppRole accessed {other} KV path (HTTP {denied_status})" ) lookup_status, lookup_response = client.request( "GET", "auth/token/lookup-self", token=token, ) token_data = lookup_response.get("data", {}) if lookup_status != 200 or not 1 <= token_data.get("ttl", 0) <= 3600: raise RuntimeError( f"{service} AppRole token TTL is not bounded (HTTP {lookup_status})" ) print("PASS: each AppRole reads its own KV path and is denied all other service paths.") if __name__ == "__main__": main()