"""Операторские действия с OpenBao (только stdlib). Запускает ОПЕРАТОР на хосте, OpenBao — https://127.0.0.1:8200. python scripts/bao.py status # sealed? initialized? (без токена) python scripts/bao.py init # один раз: 3 доли ключа (порог 2) + root token python scripts/bao.py unseal [--prompt] # после КАЖДОГО старта OpenBao: 2 доли (из файла или ввод вручную) python scripts/bao.py configure # KV v2, cert auth, политики, роли, CRL, секреты приложений python scripts/bao.py sync-crl # загрузить свежий CRL (после pki.py revoke) python scripts/bao.py kv-rotate api redis_password --value-file F # новая версия одного ключа (cas) python scripts/bao.py role-ttl api 15m 1h # TTL токенов роли (для теста истечения — короче) python scripts/bao.py revoke-service-tokens # отозвать все токены cert auth: сервисы перелогинятся сертификатом, # отозванный сертификат войти уже не сможет python scripts/bao.py seal # запечатать (демонстрация sealed) Файл оператора secrets/operator/openbao-init.json (0600, вне Git, ни в один контейнер не монтируется) хранит доли unseal и root token учебного стенда. В реальной эксплуатации доли раздаются разным людям (`init --print-shares-once` выводит их один раз и НЕ сохраняет, затем `unseal --prompt`), root token отзывается после настройки. Ни одна команда не печатает значения секретов, долей или токенов (кроме явного --print-shares-once). """ import argparse import getpass import json import os import ssl import sys import time import urllib.error import urllib.request from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SECRETS = Path(os.environ.get("SECRETS_DIR", ROOT / "secrets")) OPERATOR = SECRETS / "operator" INIT_FILE = OPERATOR / "openbao-init.json" APP_FILE = OPERATOR / "app-secrets.json" LOADED_MARKER = OPERATOR / "app-secrets.loaded" CA_FILE = Path(os.environ.get("BAO_CACERT", SECRETS / "pki" / "public" / "ca.crt")) CRL_FILE = SECRETS / "pki" / "public" / "crl.pem" POLICIES = ROOT / "infra" / "openbao" / "policies" ADDR = os.environ.get("BAO_ADDR", "https://127.0.0.1:8200").rstrip("/") KV_MOUNT = "messenger" # Роли cert auth = identity (CN) сервиса. Токен: TTL 15 мин, renewable, не дольше 1 ч; дальше — новый вход. SERVICES = ["api", "worker", "dispatcher", "janitor", "migrate", "s3-init"] TOKEN_TTL, TOKEN_MAX_TTL = "15m", "1h" class BaoError(Exception): pass def request(method: str, path: str, body=None, token: str | None = None, ok=(200, 204)) -> dict: context = ssl.create_default_context(cafile=str(CA_FILE)) data = json.dumps(body).encode("utf-8") if body is not None else None req = urllib.request.Request(ADDR + path, data=data, method=method, headers={"Content-Type": "application/json"}) if token: req.add_header("X-Vault-Token", token) opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), urllib.request.HTTPSHandler(context=context)) try: with opener.open(req, timeout=10) as reply: status, raw = reply.status, reply.read() except urllib.error.HTTPError as exc: status, raw = exc.code, exc.read() except (urllib.error.URLError, OSError) as exc: raise BaoError(f"OpenBao at {ADDR} is unreachable: {getattr(exc, 'reason', exc)}") from None if status not in ok: try: errors = json.loads(raw).get("errors") except ValueError: errors = None raise BaoError(f"{method} {path} -> HTTP {status} {errors or ''}") return json.loads(raw) if raw else {} def init_data() -> dict: if not INIT_FILE.exists(): raise BaoError(f"{INIT_FILE} not found: run `python scripts/bao.py init` first (or use --prompt)") return json.loads(INIT_FILE.read_text(encoding="utf-8")) def root_token() -> str: token = os.environ.get("BAO_TOKEN") or init_data().get("root_token") if not token: raise BaoError("no operator token: set BAO_TOKEN or keep it in the init file") return token def seal_status() -> dict: return request("GET", "/v1/sys/seal-status") def cmd_status(_args) -> int: status = seal_status() print(json.dumps({k: status.get(k) for k in ("initialized", "sealed", "t", "n", "progress", "version", "storage_type")})) return 0 def cmd_init(args) -> int: if request("GET", "/v1/sys/init").get("initialized"): print("openbao: already initialized, nothing to do") return 0 reply = request("PUT", "/v1/sys/init", {"secret_shares": 3, "secret_threshold": 2}) if args.print_shares_once: print("Unseal shares (hand each to a different operator, they are NOT saved):") for number, share in enumerate(reply["keys_base64"], 1): print(f" share {number}: {share}") print("Root token (revoke after configure in real life): " + reply["root_token"]) return 0 OPERATOR.mkdir(parents=True, exist_ok=True) OPERATOR.chmod(0o700) with open(INIT_FILE, "w", encoding="utf-8") as file: json.dump({"unseal_keys_b64": reply["keys_base64"], "root_token": reply["root_token"], "threshold": 2, "created_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())}, file) INIT_FILE.chmod(0o600) print(f"openbao: initialized (3 shares, threshold 2); shares and root token saved to {INIT_FILE} (not printed)") return 0 def wait_reachable(seconds: float = 30) -> dict: """После (ре)старта контейнера listener поднимается не сразу: ждём ответа /sys/seal-status.""" deadline = time.monotonic() + seconds while True: try: return seal_status() except BaoError: if time.monotonic() > deadline: raise time.sleep(0.5) def cmd_unseal(args) -> int: status = wait_reachable() if not status.get("sealed"): print("openbao: already unsealed") return 0 if args.prompt: shares = [getpass.getpass(f"unseal share {i + 1}: ") for i in range(status.get("t", 2))] else: shares = init_data()["unseal_keys_b64"] for share in shares: status = request("PUT", "/v1/sys/unseal", {"key": share}) if not status.get("sealed"): break if status.get("sealed"): print("openbao: still sealed, progress " + str(status.get("progress"))) return 1 print("openbao: unsealed") return 0 def cmd_seal(_args) -> int: request("PUT", "/v1/sys/seal", {}, token=root_token()) print("openbao: sealed (services keep cached secrets; new logins fail until unseal)") return 0 def ensure_mounts(token: str) -> None: mounts = request("GET", "/v1/sys/mounts", token=token) mounts = mounts.get("data", mounts) if f"{KV_MOUNT}/" not in mounts: request("POST", f"/v1/sys/mounts/{KV_MOUNT}", {"type": "kv", "options": {"version": "2"}, "description": "messenger application secrets"}, token=token) print(f"openbao: KV v2 mounted at {KV_MOUNT}/") auths = request("GET", "/v1/sys/auth", token=token) auths = auths.get("data", auths) if "cert/" not in auths: request("POST", "/v1/sys/auth/cert", {"type": "cert", "description": "service identities (mTLS leaf)"}, token=token) print("openbao: cert auth enabled at cert/") def write_policies(token: str) -> None: for path in sorted(POLICIES.glob("*.hcl")): request("PUT", f"/v1/sys/policies/acl/{path.stem}", {"policy": path.read_text(encoding="utf-8")}, token=token) print("openbao: policies " + ", ".join(p.stem for p in sorted(POLICIES.glob("*.hcl")))) def write_roles(token: str, ttl: str = TOKEN_TTL, max_ttl: str = TOKEN_MAX_TTL, only: list | None = None) -> None: ca = CA_FILE.read_text(encoding="utf-8") for name in only or SERVICES: request("POST", f"/v1/auth/cert/certs/{name}", { "display_name": name, "certificate": ca, # доверяем только учебному CA ... "allowed_common_names": [name], # ... и только сертификату с CN = имя роли "token_policies": [name], "token_ttl": ttl, "token_max_ttl": max_ttl, "token_type": "service", }, token=token) print(f"openbao: cert roles {', '.join(only or SERVICES)} (ttl {ttl}, max {max_ttl}, CN must equal role)") def sync_crl(token: str) -> None: request("POST", "/v1/auth/cert/crls/messenger-ca", {"crl": CRL_FILE.read_text(encoding="utf-8")}, token=token) print("openbao: CRL uploaded to cert auth (revoked certificates cannot log in)") def kv_read(token: str, path: str) -> tuple[dict | None, int]: try: reply = request("GET", f"/v1/{KV_MOUNT}/data/{path}", token=token) except BaoError as exc: if "HTTP 404" in str(exc): return None, 0 raise return reply["data"]["data"], reply["data"]["metadata"]["version"] def load_app_secrets(token: str, force: bool) -> None: if not APP_FILE.exists(): if LOADED_MARKER.exists(): print("openbao: application secrets already loaded earlier (source of truth is OpenBao)") return raise BaoError(f"{APP_FILE} not found: run python scripts/init_secrets.py") app = json.loads(APP_FILE.read_text(encoding="utf-8")) for path, values in app.items(): existing, _version = kv_read(token, path) if existing is not None and not force: continue request("POST", f"/v1/{KV_MOUNT}/data/{path}", {"data": values}, token=token) LOADED_MARKER.write_text(time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) + "\n", encoding="utf-8") APP_FILE.unlink() print(f"openbao: application secrets written to {KV_MOUNT}/{{{','.join(app)}}}; {APP_FILE.name} deleted") def cmd_configure(args) -> int: token = root_token() ensure_mounts(token) write_policies(token) write_roles(token) sync_crl(token) load_app_secrets(token, args.force) return 0 def cmd_sync_crl(_args) -> int: sync_crl(root_token()) return 0 def cmd_kv_rotate(args) -> int: """Новая версия ОДНОГО ключа в пути сервиса (check-and-set: не затираем чужую параллельную запись).""" token = root_token() values, version = kv_read(token, args.path) if values is None: raise BaoError(f"{KV_MOUNT}/{args.path} does not exist") value = Path(args.value_file).read_text(encoding="utf-8").strip() values[args.key] = value request("POST", f"/v1/{KV_MOUNT}/data/{args.path}", {"options": {"cas": version}, "data": values}, token=token) print(f"openbao: {KV_MOUNT}/{args.path} key {args.key} -> version {version + 1} (value not printed)") return 0 def cmd_kv_version(args) -> int: _values, version = kv_read(root_token(), args.path) print(json.dumps({"path": f"{KV_MOUNT}/{args.path}", "version": version})) return 0 def cmd_role_ttl(args) -> int: write_roles(root_token(), args.ttl, args.max_ttl, only=[args.role]) return 0 def cmd_revoke_tokens(_args) -> int: request("PUT", "/v1/sys/leases/revoke-prefix/auth/cert/login", {}, token=root_token()) print("openbao: all tokens issued by cert auth revoked; services re-login with their certificates") return 0 def main() -> int: parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) sub = parser.add_subparsers(dest="command", required=True) sub.add_parser("status").set_defaults(func=cmd_status) p = sub.add_parser("init") p.add_argument("--print-shares-once", action="store_true", help="print shares/root token instead of saving") p.set_defaults(func=cmd_init) p = sub.add_parser("unseal") p.add_argument("--prompt", action="store_true", help="type the shares instead of reading the operator file") p.set_defaults(func=cmd_unseal) sub.add_parser("seal").set_defaults(func=cmd_seal) p = sub.add_parser("configure") p.add_argument("--force", action="store_true", help="overwrite existing KV values from app-secrets.json") p.set_defaults(func=cmd_configure) sub.add_parser("sync-crl").set_defaults(func=cmd_sync_crl) p = sub.add_parser("kv-rotate") p.add_argument("path", choices=SERVICES) p.add_argument("key") p.add_argument("--value-file", required=True, help="file with the new value (not echoed)") p.set_defaults(func=cmd_kv_rotate) p = sub.add_parser("kv-version") p.add_argument("path", choices=SERVICES) p.set_defaults(func=cmd_kv_version) p = sub.add_parser("role-ttl") p.add_argument("role", choices=SERVICES) p.add_argument("ttl") p.add_argument("max_ttl") p.set_defaults(func=cmd_role_ttl) sub.add_parser("revoke-service-tokens").set_defaults(func=cmd_revoke_tokens) args = parser.parse_args() try: return args.func(args) except BaoError as exc: print(f"bao.py: {exc}", file=sys.stderr) return 1 if __name__ == "__main__": sys.exit(main())