38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
"""Run inside API and worker containers; print only allow/deny outcomes."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import ssl
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
role = os.environ['BAO_SERVICE_ROLE']
|
|
other = {'api': 'worker', 'worker': 'api'}[role]
|
|
base = os.environ['BAO_ADDR'].rstrip('/')
|
|
context = ssl.create_default_context(cafile=os.environ['BAO_CACERT'])
|
|
context.load_cert_chain(os.environ['BAO_CLIENT_CERT'])
|
|
|
|
|
|
def request(path, token=None, body=None):
|
|
headers = {'Content-Type': 'application/json'}
|
|
if token:
|
|
headers['X-Vault-Token'] = token
|
|
data = json.dumps(body).encode() if body is not None else None
|
|
req = urllib.request.Request(base + '/v1/' + path, data=data, headers=headers,
|
|
method='POST' if body is not None else 'GET')
|
|
with urllib.request.urlopen(req, context=context, timeout=5) as response:
|
|
return json.load(response)
|
|
|
|
|
|
token = request('auth/cert/login', body={'name': role})['auth']['client_token']
|
|
assert request('lab/data/' + role, token)['data']['data']
|
|
for path, body in [('lab/data/' + other, None), ('sys/policies/acl/forbidden', {'policy': 'path "*" { capabilities = ["read"] }'})]:
|
|
try:
|
|
request(path, token, body)
|
|
except urllib.error.HTTPError as error:
|
|
assert error.code == 403, f'unexpected status {error.code}'
|
|
else:
|
|
raise AssertionError('cross-role read or policy write allowed')
|
|
assert not Path('/run/secrets/openbao-operator.json').exists()
|
|
print(f'PASS {role}: own path allowed; other path and policy write denied')
|