Files
mtusi2026lab7/scripts/configure-bao.py
2026-11-22 20:57:19 +03:00

29 lines
1.5 KiB
Python

"""Operator-only, repeatable configuration. Reads root token from a hidden prompt."""
import getpass
import json
import ssl
import urllib.request
from pathlib import Path
root = Path(__file__).resolve().parents[1]
ctx = ssl.create_default_context(cafile=root/'.local/ca.crt')
token = getpass.getpass('OpenBao operator token: ')
def request(path, body=None):
req = urllib.request.Request('https://localhost:8200/v1/'+path,
data=json.dumps(body).encode() if body is not None else None,
headers={'X-Vault-Token':token,'Content-Type':'application/json'})
with urllib.request.urlopen(req,context=ctx,timeout=10) as res:
data = res.read()
return json.loads(data) if data else {}
if 'messenger/' not in request('sys/mounts')['data']:
request('sys/mounts/messenger',{'type':'kv','options':{'version':'2'}})
if 'cert/' not in request('sys/auth')['data']:
request('sys/auth/cert',{'type':'cert'})
for role in ['api','worker']:
policy = 'messenger-'+role
request('sys/policies/acl/'+policy,{'policy':'path "messenger/data/'+role+'" { capabilities = ["read"] }'})
request('auth/cert/certs/'+role,{'certificate':(root/'.local/id'/role/'cert.pem').read_text(),
'allowed_dns_sans':[role],'token_policies':[policy],'token_ttl':'2m','token_max_ttl':'5m'})
request('messenger/data/'+role,{'data':json.loads((root/'.local'/(role+'.json')).read_text())})
print('Configured API and worker certificate roles, policies and KV records.')