"""Seal OpenBao, prove AppRole login fails, then unseal. Prints no key material.""" from __future__ import annotations import json import os import ssl import urllib.error import urllib.request from pathlib import Path ADDR = os.environ["BAO_ADDR"].rstrip("/") CAFILE = os.environ["TLS_CA_FILE"] INIT = json.loads(Path(os.environ["BAO_INIT_FILE"]).read_text(encoding="utf-8")) ROLE = Path("/secrets/worker.role_id").read_text(encoding="utf-8").strip() SECRET = Path("/secrets/worker.secret_id").read_text(encoding="utf-8").strip() def ctx() -> ssl.SSLContext: context = ssl.create_default_context(cafile=CAFILE) context.check_hostname = True context.verify_mode = ssl.CERT_REQUIRED context.minimum_version = ssl.TLSVersion.TLSv1_2 return context def call(method: str, path: str, body: dict | None = None, token: str = "") -> int: data = None if body is None else json.dumps(body).encode("utf-8") request = urllib.request.Request(ADDR + path, data=data, method=method) request.add_header("Content-Type", "application/json") if token: request.add_header("X-Vault-Token", token) try: with urllib.request.urlopen(request, context=ctx(), timeout=5) as response: response.read() return response.status except urllib.error.HTTPError as exc: exc.read() return exc.code def main() -> None: token = INIT.get("root_token") or "" key = (INIT.get("keys") or [""])[0] if not token or not key: raise SystemExit("operator init file is incomplete") sealed = False try: code = call("PUT", "/v1/sys/seal", token=token) if code not in (200, 204): raise SystemExit("FAIL seal returned HTTP %s" % code) sealed = True login = call( "POST", "/v1/auth/approle/login", {"role_id": ROLE, "secret_id": SECRET}, ) if login == 200: raise SystemExit("FAIL sealed OpenBao accepted AppRole") print("PASS sealed OpenBao rejects AppRole", login) finally: if sealed: status = call("POST", "/v1/sys/unseal", {"key": key}) if status != 200: raise SystemExit("FAIL unseal returned HTTP %s" % status) print("PASS OpenBao unsealed") login = call( "POST", "/v1/auth/approle/login", {"role_id": ROLE, "secret_id": SECRET}, ) if login != 200: raise SystemExit("FAIL AppRole login after unseal HTTP %s" % login) print("PASS AppRole login after unseal") if __name__ == "__main__": main()