#!/usr/bin/env python3 """Level 4: bounded behaviour while OpenBao is sealed (operator incident drill). 1. operator seals OpenBao (scripts/bao.sh seal) 2. running API/worker keep the secrets they hold: public smoke + an upload pipeline still pass; the refresher logs openbao_unavailable (no crash, no fallback to anything weaker) 3. a worker restarted while sealed does NOT start with missing/empty credentials: it waits (openbao_not_ready), stays unhealthy, and the other worker keeps processing 4. operator unseals (scripts/bao.sh unseal): the waiting worker logs in, becomes healthy; times are recorded python3 student-tests/test_bao_sealed.py (needs the operator OpenBao material) """ import subprocess import time from stand import ROOT, Client, check, compose, log, run, upload, wait_status WORKER2 = "mtusi-lab7-worker-2" def bao(cmd): out = subprocess.run(["sh", "scripts/bao.sh", cmd], cwd=ROOT, capture_output=True, text=True) check(out.returncode == 0, "bao.sh %s failed: %s" % (cmd, out.stderr[-200:])) return out.stdout.strip().splitlines()[-1] def health(container): return subprocess.run(["docker", "inspect", "-f", "{{.State.Health.Status}}", container], capture_output=True, text=True).stdout.strip() def main(): c = Client() alice, _bob, _eve, chat = c.bootstrap() t0 = time.time() log(bao("seal")) try: status = compose("exec", "-T", "-e", "BAO_ADDR=https://127.0.0.1:8200", "-e", "BAO_CACERT=/run/secrets/ca.crt", "openbao", "bao", "status", check_rc=False) check("Sealed true" in status, "OpenBao must be sealed") smoke = subprocess.run(["make", "test", "CA_FILE=%s" % (ROOT / "secrets/pki/ca.crt")], cwd=ROOT, capture_output=True, text=True) check(smoke.returncode == 0, "public smoke must pass while OpenBao is sealed") log("sealed: running services keep working -> %s" % smoke.stdout.strip().splitlines()[-1]) time.sleep(20) logs = compose("logs", "--since", "60s", "api-1", "worker") check("openbao_unavailable" in logs, "refresher must report the sealed OpenBao") log("refresher: openbao_unavailable ... keeping cached secret version (seen in API/worker logs)") subprocess.run(["docker", "restart", "-t", "20", WORKER2], capture_output=True, check=True) time.sleep(35) state = health(WORKER2) wlogs = subprocess.run(["docker", "logs", "--since", "40s", WORKER2], capture_output=True, text=True) waiting = "openbao_not_ready" in (wlogs.stdout + wlogs.stderr) log("worker-2 restarted while sealed: health=%s, waiting for OpenBao=%s" % (state, waiting)) check(state != "healthy" and waiting, "a restarted worker must wait, not run without credentials") att = upload(c, alice, chat, filename="sealed.png") wait_status(c, alice, att["id"], ("ready",), 90) log("upload during the seal processed by the remaining worker: ready") finally: t1 = time.time() log(bao("unseal")) deadline = time.time() + 90 while time.time() < deadline and health(WORKER2) != "healthy": time.sleep(2) check(health(WORKER2) == "healthy", "worker-2 must recover after unseal") log("worker-2 healthy %.0f s after unseal (sealed for %.0f s in total)" % (time.time() - t1, t1 - t0)) print("PASS: sealed OpenBao -> running services continue, new process waits, recovery after unseal") if __name__ == "__main__": run(main)