Выполнение лабораторной работы номер 7
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
"""Check scoped, short-lived OpenBao access without printing credentials."""
|
||||
import argparse
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
CHECK='''
|
||||
import json,ssl,sys,urllib.request,urllib.error
|
||||
role,other,mode=sys.argv[1:]
|
||||
ctx=ssl.create_default_context(cafile='/ca/ca.crt')
|
||||
ctx.load_cert_chain('/identity/tls.crt','/identity/tls.key')
|
||||
base='https://openbao:8200/v1/'
|
||||
def req(path,data=None,token=None):
|
||||
headers={'Content-Type':'application/json'}
|
||||
if token:headers['X-Vault-Token']=token
|
||||
r=urllib.request.Request(base+path,data=None if data is None else json.dumps(data).encode(),headers=headers)
|
||||
with urllib.request.urlopen(r,context=ctx,timeout=5) as response:return json.loads(response.read())
|
||||
if mode=='sealed':
|
||||
assert req('sys/seal-status')['sealed']
|
||||
try:req('auth/cert/login',{'name':role})
|
||||
except urllib.error.HTTPError as e:assert e.code==503
|
||||
else:raise AssertionError('sealed OpenBao issued a token')
|
||||
print('PASS: sealed OpenBao refuses service authentication')
|
||||
raise SystemExit(0)
|
||||
auth=req('auth/cert/login',{'name':role})['auth']
|
||||
assert 0 < auth['lease_duration'] <= 900
|
||||
token=auth['client_token']
|
||||
assert 'DATABASE_URL' in req('kv/data/'+role,token=token)['data']['data']
|
||||
for path in ('kv/data/'+other,'sys/policies/acl/'+role):
|
||||
try:req(path,token=token)
|
||||
except urllib.error.HTTPError as e:assert e.code==403
|
||||
else:raise AssertionError('unscoped access')
|
||||
print('PASS: '+role+' can read only its own secrets, with a limited token TTL')
|
||||
'''
|
||||
|
||||
|
||||
def compose(*args):
|
||||
subprocess.run(['docker','compose',*args],cwd=ROOT,check=True)
|
||||
|
||||
|
||||
def probe(role,other,mode='unsealed'):
|
||||
compose('exec','-T','--index','1',role,'python','-c',CHECK,role,other,mode)
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--check-sealed',action='store_true',help='Restart the lab OpenBao, then unseal it')
|
||||
args=parser.parse_args()
|
||||
if args.check_sealed:
|
||||
compose('restart','openbao')
|
||||
try:
|
||||
probe('api','worker','sealed')
|
||||
finally:
|
||||
subprocess.run([sys.executable,'scripts/bootstrap_bao.py'],cwd=ROOT,check=True)
|
||||
probe('api','worker')
|
||||
probe('worker','api')
|
||||
(ROOT/'evidence/bao-policy.json').write_text(json.dumps({
|
||||
'scoped_roles': ['api','worker'],
|
||||
'maximum_token_ttl_seconds': 900,
|
||||
'sealed_authentication_rejected': True if args.check_sealed else None,
|
||||
},indent=2)+'\n',encoding='utf-8')
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user