#!/usr/bin/env python3 """OpenBao allow/deny check, run INSIDE a service container with that service's own credentials: docker compose exec -T api python - < scripts/bao_check.py (wrapper: scripts/bao_check.sh) Uses exactly what the service has: BAO_ADDR, BAO_CACERT (server verified), BAO_ROLE_ID_FILE, BAO_SECRET_ID_FILE, BAO_SECRET_PATH. Prints status codes and key NAMES only, never values or tokens. Expected for service S (api | relay | worker): allow AppRole login; read messenger/data/S; token lookup-self; revoke-self deny read messenger/data/; write/patch messenger/data/S; list metadata; write a policy; read the policy list; create a token; enable an auth method; read sys/mounts deny the revoked token afterwards; a login with a wrong secret_id """ import json import os import ssl import sys import urllib.error import urllib.request ADDR = os.environ["BAO_ADDR"].rstrip("/") CTX = ssl.create_default_context(cafile=os.environ["BAO_CACERT"]) PATH = os.environ["BAO_SECRET_PATH"] SERVICE = PATH.rsplit("/", 1)[-1] OTHERS = [s for s in ("api", "relay", "worker") if s != SERVICE] def call(method, path, token=None, body=None): data = json.dumps(body).encode() if body is not None else (b"{}" if method in ("POST", "PUT") else None) req = urllib.request.Request(f"{ADDR}/v1/{path}", data=data, method=method, headers={"Content-Type": "application/json", **({"X-Vault-Token": token} if token else {})}) try: with urllib.request.urlopen(req, timeout=5, context=CTX) as r: raw = r.read() return r.status, (json.loads(raw) if raw else {}) except urllib.error.HTTPError as exc: return exc.code, {} def read(name): with open(os.environ[name], encoding="utf-8") as f: return f.read().strip() failures = 0 def expect(label, status, allowed): global failures ok = (200 <= status < 300) if allowed else status in (400, 403) failures += not ok print(f" {'PASS' if ok else 'FAIL'} {'allow' if allowed else 'deny '} {label:58s} -> HTTP {status}") print(f"service {SERVICE}: OpenBao {ADDR} (TLS verified against {os.environ['BAO_CACERT']})") status, body = call("POST", "auth/approle/login", body={"role_id": read("BAO_ROLE_ID_FILE"), "secret_id": read("BAO_SECRET_ID_FILE")}) expect("AppRole login with the service's role_id/secret_id", status, True) auth = body.get("auth") or {} token = auth.get("client_token", "") print(f" token policies={auth.get('token_policies')} ttl={auth.get('lease_duration')}s renewable={auth.get('renewable')}") status, body = call("GET", PATH, token) expect(f"read own path {PATH}", status, True) print(f" keys: {sorted((body.get('data') or {}).get('data') or {})}") status, _ = call("GET", "auth/token/lookup-self", token) expect("token lookup-self", status, True) for other in OTHERS: expect(f"read other service's path messenger/data/{other}", call("GET", f"messenger/data/{other}", token)[0], False) expect(f"write own path {PATH}", call("POST", PATH, token, {"data": {"x": "y"}})[0], False) expect("list messenger/metadata", call("LIST", "messenger/metadata", token)[0], False) expect(f"write policy sys/policies/acl/{SERVICE}", call("PUT", f"sys/policies/acl/{SERVICE}", token, {"policy": 'path "*" {capabilities=["sudo"]}'})[0], False) expect("read policy list sys/policies/acl", call("LIST", "sys/policies/acl", token)[0], False) expect("create a child token auth/token/create", call("POST", "auth/token/create", token, {"policies": ["api"]})[0], False) expect("enable an auth method sys/auth/userpass", call("POST", "sys/auth/userpass", token, {"type": "userpass"})[0], False) expect("read sys/mounts", call("GET", "sys/mounts", token)[0], False) expect("revoke-self", call("POST", "auth/token/revoke-self", token)[0], True) expect("use the token after revoke-self (lookup-self)", call("GET", "auth/token/lookup-self", token)[0], False) expect("read own path with the revoked token", call("GET", PATH, token)[0], False) expect("AppRole login with a wrong secret_id", call("POST", "auth/approle/login", body={"role_id": read("BAO_ROLE_ID_FILE"), "secret_id": "00000000-0000-0000-0000-000000000000"})[0], False) print(f"{'PASS' if failures == 0 else 'FAIL'}: OpenBao policy of {SERVICE}, {failures} failure(s)") sys.exit(1 if failures else 0)