forked from mtusi2026/mtusi2026lab7
Wire API and worker secrets through OpenBao AppRole
This commit is contained in:
@@ -30,6 +30,8 @@ for name in ('s3_root_user','s3_root_password','s3_access_key','s3_secret_key'):
|
||||
if not path.exists():
|
||||
with path.open('x', encoding='utf-8') as file:
|
||||
file.write(secrets.token_hex(16))
|
||||
for name in ('bao_api_role_id', 'bao_api_secret_id', 'bao_worker_role_id', 'bao_worker_secret_id'):
|
||||
(directory / name).touch(exist_ok=True)
|
||||
certs = root / 'certs'
|
||||
certs.mkdir(exist_ok=True)
|
||||
ca = certs / 'ca.crt'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# OpenBao operator bootstrap
|
||||
|
||||
The service runs in server mode with file-backed persistent storage, never in dev mode. The operator initializes and unseals it interactively; root token and unseal keys must not be written to the repository:
|
||||
The service runs in server mode with file-backed persistent storage, never in dev mode. `openbao-init` performs the reproducible local initialization and AppRole issuance; root token and unseal keys are written only to ignored local storage and must not be committed:
|
||||
|
||||
```sh
|
||||
docker compose exec openbao bao operator init -key-shares=3 -key-threshold=2
|
||||
@@ -11,4 +11,4 @@ docker compose exec openbao bao policy write messenger-api /bao/policies/api.hcl
|
||||
docker compose exec openbao bao policy write messenger-worker /bao/policies/worker.hcl
|
||||
```
|
||||
|
||||
The application is deliberately not given a root token. Production deployment must add AppRole or workload identity, short TTLs, audit logging, and TLS on the OpenBao listener.
|
||||
The application is never given a root token. API and worker use separate AppRole role/secret-id files, exchange them for short-lived tokens, and can read only their own KV path. Production deployment should add wrapped secret IDs, audit logging, and TLS on the OpenBao listener.
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Idempotent local operator bootstrap for OpenBao + AppRole.
|
||||
|
||||
Root/unseal material is generated only into the ignored secrets/ directory.
|
||||
"""
|
||||
import json, os, time, urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
root = Path('/run/bao-bootstrap')
|
||||
root.mkdir(exist_ok=True)
|
||||
addr = 'http://openbao:8200'
|
||||
def call(path, method='GET', payload=None, token=None):
|
||||
req = urllib.request.Request(addr + path, method=method,
|
||||
data=None if payload is None else json.dumps(payload).encode(),
|
||||
headers={'Content-Type':'application/json', **({'X-Vault-Token':token} if token else {})})
|
||||
with urllib.request.urlopen(req, timeout=5) as response: return json.loads(response.read() or b'{}')
|
||||
for _ in range(60):
|
||||
try: status=call('/v1/sys/seal-status'); break
|
||||
except Exception: time.sleep(1)
|
||||
else: raise SystemExit('OpenBao did not become reachable')
|
||||
if not status.get('initialized'):
|
||||
result=call('/v1/sys/init','PUT',{'secret_shares':3,'secret_threshold':2})
|
||||
(root/'bao_root_token').write_text(result['root_token'])
|
||||
(root/'bao_unseal_keys').write_text('\n'.join(result['keys']))
|
||||
status=call('/v1/sys/seal-status')
|
||||
else:
|
||||
if not (root/'bao_root_token').exists() or not (root/'bao_unseal_keys').exists(): raise SystemExit('existing OpenBao needs operator recovery material')
|
||||
if status.get('sealed'):
|
||||
for key in (root/'bao_unseal_keys').read_text().splitlines()[:2]: call('/v1/sys/unseal','PUT',{'key':key})
|
||||
token=(root/'bao_root_token').read_text().strip()
|
||||
try: call('/v1/sys/mounts/secret','POST',{'type':'kv','options':{'version':'2'}},token)
|
||||
except Exception: pass
|
||||
policy='path "secret/data/messenger/api" { capabilities = ["read"] }'
|
||||
call('/v1/sys/policies/acl/messenger-api','PUT',{'policy':policy},token)
|
||||
try: call('/v1/sys/auth/approle','PUT',{'type':'approle','description':'short-lived service identities'},token)
|
||||
except Exception: pass
|
||||
try: call('/v1/auth/approle/role/messenger-api','POST',{'token_policies':['messenger-api'],'token_ttl':'5m','token_max_ttl':'15m'},token)
|
||||
except Exception: pass
|
||||
secrets={name:Path('/run/local-secrets/'+name).read_text().strip() for name in ('database_url','s3_access_key','s3_secret_key')}
|
||||
call('/v1/secret/data/messenger/api','POST',{'data':{'DATABASE_URL':secrets['database_url'],'S3_ACCESS_KEY':secrets['s3_access_key'],'S3_SECRET_KEY':secrets['s3_secret_key']}},token)
|
||||
role=call('/v1/auth/approle/role/messenger-api/role-id',token=token)['data']['role_id']
|
||||
sid=call('/v1/auth/approle/role/messenger-api/secret-id','POST',{},token)['data']['secret_id']
|
||||
(root/'bao_api_role_id').write_text(role); (root/'bao_api_secret_id').write_text(sid)
|
||||
worker_policy='path "secret/data/messenger/worker" { capabilities = ["read"] }'
|
||||
call('/v1/sys/policies/acl/messenger-worker','PUT',{'policy':worker_policy},token)
|
||||
try: call('/v1/auth/approle/role/messenger-worker','POST',{'token_policies':['messenger-worker'],'token_ttl':'5m','token_max_ttl':'15m'},token)
|
||||
except Exception: pass
|
||||
call('/v1/secret/data/messenger/worker','POST',{'data':{'DATABASE_URL':secrets['database_url'],'S3_ACCESS_KEY':secrets['s3_access_key'],'S3_SECRET_KEY':secrets['s3_secret_key']}},token)
|
||||
wrole=call('/v1/auth/approle/role/messenger-worker/role-id',token=token)['data']['role_id']
|
||||
wsid=call('/v1/auth/approle/role/messenger-worker/secret-id','POST',{},token)['data']['secret_id']
|
||||
(root/'bao_worker_role_id').write_text(wrole); (root/'bao_worker_secret_id').write_text(wsid)
|
||||
print('OpenBao initialized/unsealed; AppRole credentials issued with a bounded TTL.')
|
||||
Reference in New Issue
Block a user