forked from mtusi2026/mtusi2026lab7
106 lines
3.7 KiB
Python
106 lines
3.7 KiB
Python
import base64
|
|
import concurrent.futures
|
|
import importlib
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import security_operator as operator
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
Client = importlib.import_module("client").Client
|
|
|
|
|
|
def compose(*args):
|
|
return subprocess.run(
|
|
["docker", "compose", *args], capture_output=True, text=True, check=True
|
|
).stdout
|
|
|
|
|
|
def lifecycle():
|
|
client = Client("https://localhost:18443", str(ROOT / "secrets/pki/operator/ca.crt"))
|
|
alice, _, _, chat = client.bootstrap()
|
|
original = client.message(alice, chat, "Persist through sealed Bao")["json"]
|
|
start = time.monotonic()
|
|
operator.call("sys/seal", {}, operator.operator_token())
|
|
deadline = start + 85
|
|
stopped = False
|
|
try:
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
client.call("GET", "/health/ready", schema="Health")
|
|
except AssertionError:
|
|
stopped = True
|
|
break
|
|
time.sleep(1)
|
|
if not stopped:
|
|
raise RuntimeError("API continued after bounded authorization expired")
|
|
outage = time.monotonic() - start
|
|
compose("restart", "openbao")
|
|
time.sleep(3)
|
|
if not operator.call("sys/seal-status")["sealed"]:
|
|
raise RuntimeError("Bao automatically unsealed after restart")
|
|
recovery = time.monotonic()
|
|
finally:
|
|
operator.unseal()
|
|
subprocess.run(["scripts/lb-wait.sh"], check=True, capture_output=True)
|
|
client.message(alice, chat, "Existing session after unseal")
|
|
messages = client.call(
|
|
"GET", "/api/v1/chats/" + chat["id"] + "/messages", actor=alice, schema="MessagePage"
|
|
)["json"]
|
|
if original["id"] not in [item["id"] for item in messages["items"]]:
|
|
raise RuntimeError("Data lost after OpenBao restart")
|
|
return {
|
|
"sealed_api_stop_seconds": round(outage, 3),
|
|
"unseal_recovery_seconds": round(time.monotonic() - recovery, 3),
|
|
"bao_restart_stays_sealed": True,
|
|
"persistent_configuration": True,
|
|
"session_and_message_preserved": True,
|
|
}
|
|
|
|
|
|
def login_limit():
|
|
operator.sql("DELETE FROM login_budget;")
|
|
|
|
def attempt(_):
|
|
client = Client("https://localhost:18443", str(ROOT / "secrets/pki/operator/ca.crt"))
|
|
return client.call(
|
|
"POST",
|
|
"/api/v1/auth/sessions",
|
|
expected=[401, 429],
|
|
headers={
|
|
"Authorization": "Basic " + base64.b64encode(b"nonexistent:incorrect").decode()
|
|
},
|
|
)
|
|
|
|
while time.time() % 10 > 2:
|
|
time.sleep(0.1)
|
|
start = time.monotonic()
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=30) as pool:
|
|
responses = list(pool.map(attempt, range(30)))
|
|
statuses = [item["status"] for item in responses]
|
|
instances = {item["headers"]["X-Instance-Id"] for item in responses}
|
|
if statuses.count(401) != 20 or statuses.count(429) != 10 or len(instances) != 2:
|
|
raise RuntimeError("Shared login budget did not match configured limit")
|
|
if not all(item["headers"].get("Retry-After") for item in responses if item["status"] == 429):
|
|
raise RuntimeError("Retry-After missing")
|
|
return {
|
|
"attempts": 30,
|
|
"kdf_admitted": 20,
|
|
"rate_limited": 10,
|
|
"instances": len(instances),
|
|
"retry_after_present": True,
|
|
"elapsed_seconds": round(time.monotonic() - start, 3),
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
os.chdir(ROOT)
|
|
result = {"lifecycle": lifecycle(), "login_limit": login_limit()}
|
|
print(json.dumps(result, indent=2))
|
|
(ROOT / "evidence/security/lifecycle.json").write_text(json.dumps(result, indent=2) + "\n")
|