This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
"""Operator-only init/unseal/configure. Never prints or mounts operator credentials."""
|
||||
from pathlib import Path
|
||||
import json,ssl,urllib.request,urllib.error,os,sys,time
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
os.chdir(ROOT)
|
||||
def context(role=None):
|
||||
ctx=ssl.create_default_context(cafile='certs/ca.crt')
|
||||
if role:ctx.load_cert_chain('certs/'+role+'.crt','certs/'+role+'.key')
|
||||
return ctx
|
||||
def call(path,data=None,token=None,role=None):
|
||||
req=urllib.request.Request('https://localhost:8207/v1/'+path,data=None if data is None else json.dumps(data).encode(),headers={'Content-Type':'application/json',**({'X-Vault-Token':token} if token else {})})
|
||||
with urllib.request.urlopen(req,context=context(role),timeout=10) as res:
|
||||
body=res.read();return json.loads(body) if body else {}
|
||||
def main():
|
||||
state=Path('operator/bao-init.json')
|
||||
if not call('sys/init')['initialized']:
|
||||
if state.exists():raise RuntimeError('Existing operator backup but Bao storage is empty; restore storage or explicitly start a new lab.')
|
||||
result=call('sys/init',{'secret_shares':1,'secret_threshold':1})
|
||||
state.write_text(json.dumps(result),encoding='utf8');state.chmod(0o600)
|
||||
if not state.exists():raise RuntimeError('Operator unseal backup required')
|
||||
saved=json.loads(state.read_text())
|
||||
if call('sys/seal-status')['sealed']:call('sys/unseal',{'key':saved['keys_base64'][0]})
|
||||
if '--unseal-only' in sys.argv:
|
||||
print('PASS: OpenBao unsealed by operator; no keys sent to application containers.');return
|
||||
root=saved['root_token']
|
||||
mounts=call('sys/mounts',token=root)
|
||||
if 'secret/' not in mounts:call('sys/mounts/secret',{'type':'kv','options':{'version':'2'}},root)
|
||||
auth=call('sys/auth',token=root)
|
||||
if 'cert/' not in auth:call('sys/auth/cert',{'type':'cert'},root)
|
||||
c=json.loads(Path('operator/credentials.json').read_text())
|
||||
for role in ['api','worker']:
|
||||
call('sys/policies/acl/'+role,{'policy':Path('infra/openbao/'+role+'.hcl').read_text()},root)
|
||||
call('auth/cert/certs/'+role,{'certificate':Path('certs/ca.crt').read_text(),'allowed_dns_sans':[role],'token_policies':[role],'token_ttl':300,'token_max_ttl':600,'token_no_default_policy':True},root)
|
||||
call('secret/data/'+role,{'data':{'database_url':'postgresql://'+role+':'+c['db_'+role]+'@db:5432/messenger','broker_url':'amqps://'+role+':'+c['rabbit_'+role]+'@broker:5671/messenger?heartbeat=5','s3_access_key':role,'s3_secret_key':c['s3_'+role]}},root)
|
||||
print('PASS: persistent OpenBao initialized/unsealed; cert-auth roles and scoped KV policies configured.')
|
||||
if __name__=='__main__':
|
||||
try:main()
|
||||
except urllib.error.HTTPError as e:raise SystemExit('OpenBao operation rejected: HTTP '+str(e.code))
|
||||
Reference in New Issue
Block a user