Files
mtusi2026lab7/research/sealed.py
T

72 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Fail closed after the service-auth lease expires, then bounded recovery."""
import json
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, check # noqa: E402
def operator(action):
subprocess.run(
[sys.executable, "scripts/bao_operator.py", action],
cwd=ROOT,
check=True,
capture_output=True,
)
def main():
c = Client("https://localhost:8447", str(ROOT / "secrets/ca.pem"))
actor = c.user("sealed")
c.call("GET", "/api/v1/users/me", actor=actor, schema="User")
start = time.monotonic()
operator("seal")
rows = []
try:
while time.monotonic() - start < 33:
live = c.call("GET", "/health/live", schema="Health")
response = c.call(
"GET", "/api/v1/users/me", expected=[200, 503], actor=actor
)
rows.append(
{
"seconds": round(time.monotonic() - start, 3),
"live": live["status"],
"business": response["status"],
}
)
time.sleep(1)
check(rows[-1]["business"] == 503, "service used credentials beyond lease")
finally:
unseal_start = time.monotonic()
operator("unseal")
for _ in range(20):
response = c.call("GET", "/health/ready", expected=[200, 503])
if response["status"] == 200:
break
time.sleep(0.25)
c.call("GET", "/api/v1/users/me", actor=actor, schema="User")
result = {
"lease_seconds": 30,
"local_cache_max_seconds": 29,
"rows": rows,
"first_business_503_seconds": next(
(r["seconds"] for r in rows if r["business"] == 503), None
),
"recovery_seconds": round(time.monotonic() - unseal_start, 3),
"passed": rows[-1]["business"] == 503 and response["status"] == 200,
}
(ROOT / "evidence/sealed-expiry.json").write_text(json.dumps(result, indent=2) + "\n")
check(result["passed"], "recovery failed")
print(json.dumps({k: v for k, v in result.items() if k != "rows"}))
if __name__ == "__main__":
main()