#!/usr/bin/env python3 """Initialize operator-controlled OpenBao configuration over verified HTTPS.""" import argparse import getpass import json import os from pathlib import Path import ssl import sys from urllib.error import HTTPError ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from app.credentials import bao_request os.environ.setdefault('BAO_ADDR', 'https://localhost:8200') CA = ROOT / 'secrets/ca/ca.crt' def unseal(context): while True: status = bao_request('sys/seal-status', context) if not status['initialized']: raise SystemExit('Initialize OpenBao before submitting unseal shares.') if not status['sealed']: print('OpenBao is unsealed') return key = getpass.getpass(f'OpenBao share ({status["progress"]}/{status["t"]}): ') bao_request('sys/unseal', context, {'key': key}) def configure(context): token = getpass.getpass('OpenBao operator token: ') def request(path, payload=None): return bao_request(path, context, payload, token) mounts = request('sys/mounts') if 'secret/' not in (mounts.get('data') or mounts): request('sys/mounts/secret', {'type': 'kv', 'options': {'version': '2'}}) methods = request('sys/auth') if 'cert/' not in (methods.get('data') or methods): request('sys/auth/cert', {'type': 'cert'}) for role in ('api', 'worker'): request('sys/policies/acl/' + role, {'policy': (ROOT / f'infra/openbao/{role}.hcl').read_text()}) request('auth/cert/certs/' + role, { 'certificate': (ROOT / f'secrets/identity/{role}/client.crt').read_text(), 'allowed_common_names': [role], 'token_policies': [role], 'token_no_default_policy': True, 'token_ttl': '120s', 'token_explicit_max_ttl': '300s', }) request('secret/data/' + role, {'data': json.loads((ROOT / f'secrets/operator/{role}.json').read_text())}) print('Configured two scoped certificate roles and KV paths') def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('action', choices=['unseal', 'configure']) args = parser.parse_args() context = ssl.create_default_context(cafile=str(CA)) try: {'unseal': unseal, 'configure': configure}[args.action](context) except HTTPError as exc: raise SystemExit(f'OpenBao rejected the operation with HTTP {exc.code}') from None if __name__ == '__main__': main()