38 lines
1.7 KiB
Python
38 lines
1.7 KiB
Python
"""Obtain scoped service configuration using a certificate identity in OpenBao."""
|
|
import json
|
|
import os
|
|
import ssl
|
|
import sys
|
|
import urllib.request
|
|
|
|
|
|
def main():
|
|
role = os.environ['BAO_SERVICE_ROLE']
|
|
if role not in {'api', 'worker'}:
|
|
raise ValueError('unsupported service identity')
|
|
base = os.environ.get('BAO_ADDR', 'https://openbao:8200').rstrip('/')
|
|
context = ssl.create_default_context(cafile=os.environ['BAO_CACERT'])
|
|
context.load_cert_chain(os.environ['BAO_CLIENT_CERT'])
|
|
login = urllib.request.Request(base + '/v1/auth/cert/login',
|
|
data=json.dumps({'name': role}).encode(),
|
|
headers={'Content-Type': 'application/json'}, method='POST')
|
|
with urllib.request.urlopen(login, context=context, timeout=5) as response:
|
|
token = json.load(response)['auth']['client_token']
|
|
request = urllib.request.Request(base + '/v1/lab/data/' + role,
|
|
headers={'X-Vault-Token': token})
|
|
with urllib.request.urlopen(request, context=context, timeout=5) as response:
|
|
values = json.load(response)['data']['data']
|
|
allowed = {'DATABASE_URL', 'CURSOR_SECRET', 'S3_ACCESS_KEY_ID', 'S3_SECRET_ACCESS_KEY', 'BROKER_URL'}
|
|
if set(values) != allowed or not all(isinstance(v, str) and v for v in values.values()):
|
|
raise ValueError('invalid service configuration from OpenBao')
|
|
for key, value in values.items():
|
|
os.environ.pop(key + '_FILE', None)
|
|
os.environ[key] = value
|
|
if len(sys.argv) < 2:
|
|
raise SystemExit('missing command')
|
|
os.execv(sys.argv[1], sys.argv[1:])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|