forked from mtusi2026/mtusi2026lab7
58 lines
2.0 KiB
Python
58 lines
2.0 KiB
Python
"""Verify persistent seal state and fail-closed service startup on real restart."""
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
|
|
from bao_operator import seal_status
|
|
from test_resilience import COMPOSE, ready
|
|
|
|
|
|
def inspect():
|
|
identifier = subprocess.check_output([*COMPOSE, "ps", "-q", "openbao"], text=True).strip()
|
|
result = json.loads(subprocess.check_output(["docker", "inspect", identifier]))[0]
|
|
volume = next(m["Name"] for m in result["Mounts"] if m["Destination"] == "/openbao/file")
|
|
return identifier, result["State"]["StartedAt"], volume
|
|
|
|
|
|
def client(service, expect_sealed=False):
|
|
script = """
|
|
import sys
|
|
from src.config import bao_config
|
|
try:
|
|
configuration = bao_config()
|
|
except RuntimeError:
|
|
if sys.argv[1] != 'sealed':
|
|
raise
|
|
print('PASS: new process refuses to start with sealed OpenBao')
|
|
else:
|
|
assert sys.argv[1] == 'unsealed' and len(configuration) == 5
|
|
print('PASS: scoped KV restored without bootstrap rewrite')
|
|
"""
|
|
subprocess.run([*COMPOSE, "run", "--rm", "--no-deps", "-T", service,
|
|
"python", "-c", script, "sealed" if expect_sealed else "unsealed"], check=True)
|
|
|
|
|
|
def main():
|
|
before = inspect()
|
|
assert seal_status()["sealed"] is False
|
|
print("Before restart:", *before, flush=True)
|
|
subprocess.run([*COMPOSE, "restart", "openbao"], check=True)
|
|
try:
|
|
status = seal_status()
|
|
after = inspect()
|
|
assert before[0] == after[0] and before[2] == after[2]
|
|
assert before[1] != after[1] and status["initialized"] and status["sealed"]
|
|
print("After restart:", *after, "initialized=True sealed=True", flush=True)
|
|
client("worker", expect_sealed=True)
|
|
ready()
|
|
print("PASS: running API retains its in-memory dependencies while OpenBao is sealed", flush=True)
|
|
finally:
|
|
subprocess.run([sys.executable, "scripts/bao_operator.py", "unseal"], check=True)
|
|
assert not seal_status()["sealed"]
|
|
client("api1")
|
|
client("worker")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|