"""Explicit host operator actions; unseal material never enters a container.""" import argparse import json from pathlib import Path import ssl import urllib.request import urllib.error import time ROOT=Path(__file__).resolve().parents[1] def request(path, data=None, token=None, context=None): env=dict(line.split('=',1) for line in (ROOT/'.env').read_text().splitlines() if '=' in line) ctx=context or ssl.create_default_context(cafile=str(ROOT/'secrets/ca.crt')) headers={'Content-Type':'application/json'} if token: headers['X-Vault-Token']=token req=urllib.request.Request('https://localhost:'+env['BAO_PORT']+'/v1/'+path, data=json.dumps(data).encode() if data is not None else None,headers=headers) with urllib.request.urlopen(req,context=ctx,timeout=5) as response: body=response.read() return json.loads(body) if body else {} def main(): p=argparse.ArgumentParser(); p.add_argument('action',choices=['init','unseal','configure']); a=p.parse_args() private=ROOT/'secrets/operator/bao-init.json' if a.action=='init': if private.exists(): print('Operator init material already exists; refusing reinitialization.'); return result=request('sys/init',{'secret_shares':1,'secret_threshold':1}) from bootstrap import write write(private,json.dumps(result)) print('Initialized persistent OpenBao; operator material saved privately on host.'); return state=json.loads(private.read_text()) if a.action=='unseal': result=request('sys/unseal',{'key':state['keys_base64'][0]}) assert not result['sealed'] print('OpenBao unsealed by host operator.'); return root=state['root_token'] # Idempotent enable, without silently replacing keys or credentials. mounts=request('sys/mounts',token=root) if 'secret/' not in mounts: request('sys/mounts/secret',{'type':'kv','options':{'version':'2'}},root) auth=request('sys/auth',token=root) if 'cert/' not in auth: request('sys/auth/cert',{'type':'cert'},root) for role in ('api','worker','dispatcher','cleaner'): policy=(ROOT/'infra/openbao'/f'{role}.hcl').read_text() request('sys/policies/acl/'+role,{'policy':policy},root) # Pin the exact leaf certificate: another trusted CA leaf has no auth role. name='api1' if role=='api' else role certificate=(ROOT/'secrets/tls'/name/'cert.crt').read_text() request('auth/cert/certs/'+role,{'certificate':certificate,'allowed_common_names':name, 'token_policies':[role],'token_ttl':300,'token_max_ttl':300,'token_no_default_policy':True},root) if role=='api': request('auth/cert/certs/api2',{'certificate':(ROOT/'secrets/tls/api2/cert.crt').read_text(), 'allowed_common_names':'api2','token_policies':['api'],'token_ttl':300,'token_max_ttl':300, 'token_no_default_policy':True},root) path='secret/data/'+role try: request(path,token=root) except urllib.error.HTTPError as error: if error.code!=404: raise request(path,{'data':json.loads((ROOT/'secrets'/f'bao_{role}.json').read_text())},root) print('Certificate auth, 300s tokens, scoped policies and KV paths configured.') if __name__=='__main__': main()