"""Explicit operator init/unseal/provision. Shares never enter a service mount.""" import argparse import json from pathlib import Path import ssl import time import urllib.request from urllib.error import HTTPError, URLError ROOT=Path(__file__).resolve().parents[1] def request(path,body=None,token=None,method=None): ctx=ssl.create_default_context(cafile=str(ROOT/'secrets/trust/ca.crt')) ctx.load_cert_chain(ROOT/'secrets/identities/operator/cert.pem',ROOT/'secrets/identities/operator/key.pem') headers={'Content-Type':'application/json'} if token:headers['X-Vault-Token']=token req=urllib.request.Request('https://127.0.0.1:8200/v1/'+path,data=json.dumps(body).encode() if body is not None else None,headers=headers,method=method) with urllib.request.urlopen(req,context=ctx,timeout=15) as r: raw=r.read();return json.loads(raw) if raw else {} def main(): p=argparse.ArgumentParser();p.add_argument('action',choices=['init','unseal','provision','seal','publish-crl']);args=p.parse_args() file=ROOT/'secrets/operator/bao-init.json' if args.action=='init': if request('sys/init')['initialized']:raise SystemExit('Already initialized; preserve existing operator shares') result=request('sys/init',{'secret_shares':3,'secret_threshold':2}) with file.open('x',encoding='utf-8') as f:json.dump(result,f) file.chmod(0o600) print('Initialized; three shares stored in operator-only local file. Unseal explicitly.');return operator=json.loads(file.read_text(encoding='utf-8'));token=operator['root_token'] if args.action=='unseal': deadline=time.monotonic()+20 while True: try:request('sys/seal-status');break except (URLError,OSError): if time.monotonic()>=deadline:raise time.sleep(.5) for share in operator['keys'][:2]:request('sys/unseal',{'key':share}) elif args.action=='seal':request('sys/seal',{},token) elif args.action=='publish-crl':request('auth/cert/crls/operator',{'crl':(ROOT/'secrets/trust/revocations.pem').read_text()},token) elif args.action=='provision': mounts=request('sys/mounts',token=token) if 'secret/' not in mounts:request('sys/mounts/secret',{'type':'kv','options':{'version':'2'}},token) auth=request('sys/auth',token=token) if 'cert/' not in auth:request('sys/auth/cert',{'type':'cert'},token) config=json.loads((ROOT/'secrets/operator/config.json').read_text()) for role,data in config['service_credentials'].items(): policy=f'path "secret/data/{role}" {{ capabilities = ["read"] }}\n' request('sys/policies/acl/'+role,{'policy':policy},token) request('auth/cert/certs/'+role,{'certificate':(ROOT/'secrets/trust/ca.crt').read_text(), 'allowed_dns_sans':[role],'token_policies':[role],'token_ttl':120,'token_max_ttl':120,'token_no_default_policy':False},token) request('secret/data/'+role,{'data':data},token) # Audit is declarative in openbao.hcl; API audit creation remains disabled. assert 'operator/' in request('sys/audit',token=token), 'Audit device missing' print('Operator action completed; no root token/unseal shares printed') if __name__=='__main__':main()