"""Unseal a restarted OpenBao from the operator's local init file. Prints only seal state.""" import json import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] INIT = ROOT / "secrets" / "operator" / "init.json" def bao(*args): command = [ "docker", "compose", "--env-file", ".env", "exec", "-T", "-e", "BAO_ADDR=https://127.0.0.1:8200", "-e", "BAO_CACERT=/openbao/tls/ca.crt", "-e", "BAO_CLIENT_CERT=/openbao/tls/operator.crt", "-e", "BAO_CLIENT_KEY=/openbao/tls/operator.key", "openbao", "bao", *args, ] return subprocess.run(command, cwd=ROOT, capture_output=True, text=True) def status(): result = bao("status", "-format=json") text = result.stdout or "" start = text.find("{") end = text.rfind("}") if start < 0 or end < start: raise SystemExit("OpenBao status is not available") return json.loads(text[start : end + 1]) def main(): current = status() if not current.get("initialized"): raise SystemExit("OpenBao is not initialized") if not current.get("sealed"): print("sealed=false") return if not INIT.is_file(): raise SystemExit("operator init file is missing") keys = json.loads(INIT.read_text(encoding="utf-8")).get("unseal_keys_b64") or [] for key in keys[:2]: result = bao("operator", "unseal", key) if result.returncode != 0: raise SystemExit("OpenBao unseal failed") print("sealed=" + str(bool(status().get("sealed"))).lower()) if __name__ == "__main__": sys.exit(main())