Files
mtusi2026lab7/scripts/test-openbao-policy.py
2026-11-26 23:34:55 +03:00

28 lines
1.5 KiB
Python

#!/usr/bin/env python3
import json,ssl,urllib.request,urllib.error
from pathlib import Path
root=Path(__file__).resolve().parent.parent
ctx=ssl.create_default_context(cafile=str(root/'certs/ca.crt'))
def call(path,body=None,token=None,expected=200):
h={'Content-Type':'application/json'}
if token:h['X-Vault-Token']=token
r=urllib.request.Request('https://localhost:8200/v1/'+path,headers=h,data=None if body is None else json.dumps(body).encode())
try:
with urllib.request.urlopen(r,context=ctx,timeout=10) as response:
assert response.status==expected,(path,response.status)
raw=response.read();return json.loads(raw) if raw else {}
except urllib.error.HTTPError as e:
assert e.code==expected,(path,e.code)
for role,other in [('api','worker'),('worker','api')]:
creds=root/'secrets/approle'/role
token=call('auth/approle/login',{'role_id':(creds/'role-id').read_text(),'secret_id':(creds/'secret-id').read_text()})['auth']['client_token']
try:
call('secret/data/'+role,token=token)
call('database/creds/'+role+'-role',token=token)
call('secret/data/'+other,token=token,expected=403)
call('database/creds/'+other+'-role',token=token,expected=403)
call('sys/policies/acl/root',token=token,expected=403)
call('sys/policies/acl/'+role,{'policy':''},token,403)
finally:call('auth/token/revoke-self',{},token,204)
print('PASS: both AppRoles read own KV/DB path; cross-role and policy read/write denied; test tokens revoked')