47 lines
2.0 KiB
Python
47 lines
2.0 KiB
Python
"""Scoped role self-test executed by operator-only one-shot probe.
|
|
Reads role IDs but never displays secret values/tokens.
|
|
"""
|
|
import json
|
|
import ssl
|
|
from pathlib import Path
|
|
from urllib.request import Request,urlopen
|
|
from urllib.error import HTTPError
|
|
|
|
ROOT='https://openbao:8200/v1/'
|
|
TLS=ssl.create_default_context(cafile='/run/pki/ca.crt')
|
|
|
|
def call(method,path,payload=None,token=None):
|
|
headers={'Content-Type':'application/json'}
|
|
if token: headers['X-Vault-Token']=token
|
|
req=Request(ROOT+path,headers=headers,
|
|
data=json.dumps(payload).encode() if payload is not None else None,
|
|
method=method)
|
|
try:
|
|
with urlopen(req,context=TLS,timeout=8) as resp:
|
|
raw=resp.read()
|
|
return resp.status,json.loads(raw) if raw else {}
|
|
except HTTPError as e:
|
|
return e.code,{}
|
|
|
|
def main():
|
|
for role in ('api','worker','dispatcher'):
|
|
root=Path('/role-files')/role
|
|
id=(root/'role_id').read_text().strip()
|
|
secret=(root/'secret_id').read_text().strip()
|
|
status,res=call('POST','auth/approle/login',{'role_id':id,'secret_id':secret})
|
|
assert status==200,role+' AppRole login failed'
|
|
token=res['auth']['client_token']
|
|
status,_=call('GET','kv/data/'+role,token=token)
|
|
assert status==200,role+' failed own read'
|
|
for other in ('api','worker','dispatcher'):
|
|
if other!=role:
|
|
status,_=call('GET','kv/data/'+other,token=token)
|
|
assert status==403,role+' unexpectedly read '+other
|
|
status,_=call('PUT','sys/policies/acl/api',{'policy':'path "*" { capabilities = ["sudo"] }'},token=token)
|
|
assert status==403,role+' unexpectedly modified policies'
|
|
print('PASS:',role,'own KV read, cross-role read denied, write policy denied')
|
|
status,_=call('POST','auth/approle/login',{'role_id':'bad-role','secret_id':'bad-secret'})
|
|
assert status in (400,403), 'Invalid AppRole was accepted'
|
|
print('PASS: untrusted AppRole rejected')
|
|
if __name__=='__main__':main()
|