#!/usr/bin/env python3 """Measure loss and restoration of service after sealing the local OpenBao.""" import json from pathlib import Path import ssl import sys import time import urllib.error import urllib.request ROOT = Path(__file__).resolve().parents[1] sys.path[:0] = [str(ROOT / "scripts"), str(ROOT / "tests")] from stand import bao, root_token, unseal from client import Client, Failure, check client = Client("https://localhost:8443", str(ROOT / ".runtime/trust/ca.crt"), timeout=2) actor = client.user("seal") client.call("GET", "/api/v1/users/me", actor=actor) sealed_at = time.monotonic() bao("sys/seal", {}, root_token()) try: status = bao("sys/seal-status") check(status["sealed"], "seal did not take effect") consecutive = 0 last_ready = None ca = ssl.create_default_context(cafile=str(ROOT / ".runtime/trust/ca.crt")) while consecutive < 3: elapsed = time.monotonic() - sealed_at check(elapsed < 55, "API served beyond credential grace") try: with urllib.request.urlopen("https://localhost:8443/health/ready", context=ca, timeout=3) as result: check(result.status == 200, "unexpected readiness status") last_ready = elapsed consecutive = 0 except urllib.error.HTTPError as error: check(error.code == 503, "unexpected ingress readiness error") if elapsed >= 20: consecutive += 1 except OSError: consecutive = 0 time.sleep(0.5) unavailable_at = time.monotonic() check(last_ready is not None, "no successful sample within the grace period") try: client.call("GET", "/api/v1/users/me", actor=actor) except Failure: pass else: raise AssertionError("session still served while the credentials lease is lost") finally: unseal() start = time.monotonic() while True: try: client.call("GET", "/api/v1/users/me", actor=actor) break except Failure: check(time.monotonic() - start < 45, "original session did not recover") time.sleep(0.5) print(json.dumps({"sealed_to_unavailable_s": round(unavailable_at - sealed_at, 2), "last_ready_after_seal_s": round(last_ready, 2), "unseal_to_ready_s": round(time.monotonic() - start, 2), "previous_session_preserved": True}))