Files
mtusi2026lab7/checks/bao_policy.py
T

40 lines
1.9 KiB
Python

import json
import ssl
import urllib.request
from urllib.error import HTTPError
from tls_matrix import context
def request(role,path,body=None,token=None):
headers={'Content-Type':'application/json'}
if token:headers['X-Vault-Token']=token
req=urllib.request.Request('https://openbao:8200/v1/'+path,headers=headers,
data=json.dumps(body).encode() if body is not None else None)
try:
with urllib.request.urlopen(req,context=context(role),timeout=5) as r:
raw=r.read();return r.status,json.loads(raw) if raw else {}
except HTTPError as e:return e.code,{}
results=[]
for role in ('api1','api2','worker','dispatcher','reconciler'):
code,data=request(role,'auth/cert/login',{'name':role})
assert code==200,(role,code)
auth=data['auth'];token=auth['client_token']
assert 0<auth['lease_duration']<=120 and 'root' not in auth['policies']
try:
assert request(role,'secret/data/'+role,token=token)[0]==200
other='worker' if role!='worker' else 'api1'
assert request(role,'secret/data/'+other,token=token)[0]==403
assert request(role,'sys/mounts',token=token)[0]==403
assert request(role,'auth/cert/login',{'name':other})[0] in (400,403)
results.append({'role':role,'own_secret':200,'other_secret':403,'admin':403,'TTL':auth['lease_duration'],'wrong_role_denied':True})
finally:request(role,'auth/token/revoke-self',{},token)
for role in (None,'outsider','revoked','expired','alien'):
try:assert request(role,'auth/cert/login',{'name':'api1'})[0] in (400,403)
except (ssl.SSLError,urllib.error.URLError) as exc:
# TLS handshake rejection also denies auth; network must already work above.
if not isinstance(exc,ssl.SSLError) and not isinstance(getattr(exc,'reason',None),ssl.SSLError):raise
results.append({'identity':role,'login_denied':True})
print(json.dumps({'passed':True,'results':results},indent=2))