Files
mtusi2026lab7/checks/sealed_bao.py
T

47 lines
2.2 KiB
Python

"""Finite supervisor lease under real sealed OpenBao, then explicit recovery."""
import json
from pathlib import Path
import ssl
import subprocess
import sys
import time
import urllib.request
from urllib.error import HTTPError,URLError
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT/'scripts'))
from bao_operator import request
ctx=ssl.create_default_context(cafile=str(ROOT/'secrets/trust/ca.crt'))
def status():
try:
with urllib.request.urlopen('https://127.0.0.1:8443/health/ready',context=ctx,timeout=3) as r:return r.status
except HTTPError as e:return e.code
assert status()==200
root=json.loads((ROOT/'secrets/operator/bao-init.json').read_text())['root_token']
start=time.monotonic();observations=[]
try:
request('sys/seal',{},root)
while time.monotonic()-start<150:
current=status();observations.append({'seconds':round(time.monotonic()-start,1),'HTTP':current})
if current==503:
# Both API children must actually have stopped, not just one LB check.
code="import socket; s=socket.socket();s.settimeout(1);assert s.connect_ex(('127.0.0.1',8080))!=0"
for role in ('api1','api2'):
subprocess.run(['docker','compose','exec','-T',role,'python','-c',code],cwd=ROOT,check=True,capture_output=True)
break
time.sleep(2)
else:raise AssertionError('Application exceeded finite secret availability budget')
stopped=time.monotonic()-start
finally:
subprocess.run([sys.executable,str(ROOT/'scripts/bao_operator.py'),'unseal'],cwd=ROOT,check=True)
recovery=time.monotonic()
while time.monotonic()-recovery<60:
if status()==200:break
time.sleep(1)
else:raise AssertionError('Unsealed system failed to recover')
result={'passed':True,'secret_refresh_seconds':20,'token_max_TTL_seconds':120,'both_API_children_stopped_seconds':round(stopped,2),'recovery_seconds':round(time.monotonic()-recovery,2),'observations':observations,'scope':'Supervisor bounds use during Bao outage; static backend passwords themselves require explicit rotation.'}
(ROOT/'evidence/sealed-bao.json').write_text(json.dumps(result,indent=2)+'\n')
print('PASS: sealed Bao -> finite lease -> both API children stopped -> unseal -> recovery')