37 lines
1.7 KiB
Python
37 lines
1.7 KiB
Python
"""Operator-only init/unseal and cert-auth provisioning. No secrets printed."""
|
|
import json
|
|
from pathlib import Path
|
|
import ssl
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
root=Path(__file__).resolve().parents[1];private=root/'.secrets'
|
|
context=ssl.create_default_context(cafile=str(private/'ca.crt'))
|
|
base='https://localhost:8207/v1/'
|
|
operator=private/'bao-operator.json'
|
|
def call(path,data=None,token=None,method=None):
|
|
body=json.dumps(data).encode() if data is not None else None
|
|
headers={'Content-Type':'application/json'}
|
|
if token:headers['X-Vault-Token']=token
|
|
with urllib.request.urlopen(urllib.request.Request(base+path,data=body,headers=headers,method=method),context=context,timeout=30 if path in ("sys/init","sys/unseal") else 10) as r:
|
|
raw=r.read();return json.loads(raw) if raw else {}
|
|
for _ in range(60):
|
|
try:state=call('sys/seal-status');break
|
|
except (OSError,ValueError):time.sleep(1)
|
|
else:raise SystemExit('OpenBao TLS endpoint unavailable')
|
|
if not state['initialized']:
|
|
import os
|
|
os.umask(0o077)
|
|
operator.write_text(json.dumps(call('sys/init',{'secret_shares':1,'secret_threshold':1})))
|
|
if not operator.exists():raise SystemExit('Operator shares required; do not reinitialize existing Bao data')
|
|
credentials=json.loads(operator.read_text())
|
|
if state['sealed']:call('sys/unseal',{'key':credentials['keys'][0]})
|
|
token=credentials['root_token']
|
|
mounts=call('sys/mounts',token=token)
|
|
if 'secret/' not in mounts:call('sys/mounts/secret',{'type':'kv','options':{'version':'2'}},token)
|
|
auth=call('sys/auth',token=token)
|
|
if 'cert/' not in auth:call('sys/auth/cert',{'type':'cert'},token)
|
|
from bao_operator import provision
|
|
provision()
|
|
print('OpenBao initialized/unsealed with short scoped cert auth')
|