forked from mtusi2026/mtusi2026lab7
test(security): automate lifecycle and login budget measurements
This commit is contained in:
+127
-22
@@ -37,23 +37,57 @@ def save_incomplete():
|
||||
atexit.register(save_incomplete)
|
||||
|
||||
|
||||
def redact(text, cwd):
|
||||
def values(value):
|
||||
if isinstance(value, dict):
|
||||
for item in value.values():
|
||||
yield from values(item)
|
||||
elif isinstance(value, list):
|
||||
for item in value:
|
||||
yield from values(item)
|
||||
elif isinstance(value, str) and len(value) >= 12:
|
||||
yield value
|
||||
|
||||
for folder in [cwd / "secrets", cwd / "operator"]:
|
||||
for path in folder.rglob("*"):
|
||||
if not path.is_file():
|
||||
continue
|
||||
try:
|
||||
raw = path.read_text().strip()
|
||||
except UnicodeDecodeError:
|
||||
continue
|
||||
if path.suffix == ".json":
|
||||
try:
|
||||
for value in values(json.loads(raw)):
|
||||
text = text.replace(value, "<private-value>")
|
||||
except ValueError:
|
||||
pass
|
||||
if len(raw) >= 12:
|
||||
text = text.replace(raw, "<private-file>")
|
||||
return (
|
||||
text.replace(str(cwd), "<clean-clone>")
|
||||
.replace(str(ROOT), "<repository>")
|
||||
.replace(str(Path.home()), "<home>")
|
||||
)
|
||||
|
||||
|
||||
def run(args, *, cwd, env, expected=0):
|
||||
print("CHECK: " + redact(" ".join(args[:3]), cwd), flush=True)
|
||||
result = subprocess.run(
|
||||
args, cwd=cwd, env=env, capture_output=True, text=True
|
||||
)
|
||||
|
||||
def redact(text):
|
||||
return (
|
||||
text.replace(str(cwd), "<clean-clone>")
|
||||
.replace(str(ROOT), "<repository>")
|
||||
.replace(str(Path.home()), "<home>")
|
||||
)
|
||||
|
||||
if result.returncode != expected:
|
||||
if "receipt" in PROGRESS:
|
||||
PROGRESS["receipt"]["failed_action"] = redact(" ".join(args))
|
||||
raise RuntimeError(redact(result.stdout + result.stderr))
|
||||
return redact(result.stdout.strip())
|
||||
PROGRESS["receipt"]["failed_action"] = redact(" ".join(args), cwd)
|
||||
raise RuntimeError(redact(result.stdout + result.stderr, cwd))
|
||||
return redact(result.stdout.strip(), cwd)
|
||||
|
||||
|
||||
def free_port():
|
||||
with socket.socket() as reservation:
|
||||
reservation.bind(("127.0.0.1", 0))
|
||||
return reservation.getsockname()[1]
|
||||
|
||||
|
||||
def main():
|
||||
@@ -70,7 +104,7 @@ def main():
|
||||
["git", "rev-parse", "HEAD"], cwd=ROOT, text=True
|
||||
).strip()
|
||||
identity = uuid4().hex[:10]
|
||||
project = "mtusi-lab6-clean-" + identity
|
||||
project = "mtusi-lab7-clean-" + identity
|
||||
with socket.socket() as reservation:
|
||||
reservation.bind(("127.0.0.1", 0))
|
||||
public_port = str(reservation.getsockname()[1])
|
||||
@@ -80,9 +114,12 @@ def main():
|
||||
env = {
|
||||
**os.environ,
|
||||
"COMPOSE_PROJECT_NAME": project,
|
||||
"API_IMAGE": "mtusi-messenger:lab6-clean-" + identity,
|
||||
"API_IMAGE": "mtusi-messenger:lab7-clean-" + identity,
|
||||
"PUBLIC_PORT": public_port,
|
||||
"GRAFANA_PORT": grafana_port,
|
||||
"BASE_URL": "https://localhost:" + public_port,
|
||||
"BAO_PORT": str(free_port()),
|
||||
"S3_ADMIN_PORT": str(free_port()),
|
||||
"VCS_REF": sha,
|
||||
"COMPOSE_FILE": "compose.yaml:compose.test.yaml",
|
||||
"NO_PROXY": "localhost,127.0.0.1",
|
||||
@@ -96,8 +133,8 @@ def main():
|
||||
"fixed_host_port_for_restart": int(public_port),
|
||||
}
|
||||
PROGRESS.update(receipt=receipt, output=output)
|
||||
with tempfile.TemporaryDirectory(prefix="mtusi-lab6-clean-") as temporary:
|
||||
clone = Path(temporary) / "lab6"
|
||||
with tempfile.TemporaryDirectory(prefix="mtusi-lab7-clean-") as temporary:
|
||||
clone = Path(temporary) / "lab7"
|
||||
run(
|
||||
[
|
||||
"git",
|
||||
@@ -123,11 +160,12 @@ def main():
|
||||
run(["make", "bootstrap"], cwd=clone, env=env)
|
||||
try:
|
||||
run(
|
||||
["docker", "compose", "up", "-d", "--build", "--wait"],
|
||||
[".venv/bin/python", "-m", "scripts.up_security", "--build"],
|
||||
cwd=clone,
|
||||
env=env,
|
||||
)
|
||||
env["BASE_URL"] = "http://127.0.0.1:" + public_port
|
||||
env["BASE_URL"] = "https://localhost:" + public_port
|
||||
env["CA_FILE"] = str(clone / "trust/ca.pem")
|
||||
receipt["smoke"] = run(
|
||||
["make", "test", "BASE_URL=" + env["BASE_URL"]],
|
||||
cwd=clone,
|
||||
@@ -136,18 +174,40 @@ def main():
|
||||
receipt["http_tests"] = run(
|
||||
[".venv/bin/pytest", "student-tests", "-q"], cwd=clone, env=env
|
||||
)
|
||||
receipt["login_budget"] = run(
|
||||
[
|
||||
".venv/bin/python",
|
||||
"-m",
|
||||
"scripts.verify_login_budget",
|
||||
"--output",
|
||||
"evidence/clean-login-budget.json",
|
||||
],
|
||||
cwd=clone,
|
||||
env=env,
|
||||
)
|
||||
receipt["public_security"] = run(
|
||||
[
|
||||
"make",
|
||||
"test-security",
|
||||
"PYTHON=.venv/bin/python",
|
||||
"BASE_URL=" + env["BASE_URL"],
|
||||
],
|
||||
cwd=clone,
|
||||
env=env,
|
||||
)
|
||||
for module, extra in [
|
||||
("verify_security", []),
|
||||
("verify_telemetry", []),
|
||||
(
|
||||
"verify_observability_persistence",
|
||||
["--causal-evidence", "evidence/clean-telemetry.json"],
|
||||
),
|
||||
]:
|
||||
name = (
|
||||
"telemetry"
|
||||
if module == "verify_telemetry"
|
||||
else "observability-persistence"
|
||||
)
|
||||
name = {
|
||||
"verify_security": "security",
|
||||
"verify_telemetry": "telemetry",
|
||||
"verify_observability_persistence": "observability-persistence",
|
||||
}[module]
|
||||
run(
|
||||
[
|
||||
".venv/bin/python",
|
||||
@@ -267,8 +327,53 @@ def main():
|
||||
["git", "fsck", "--full"], cwd=clone, env=env
|
||||
)
|
||||
receipt["checks_completed"] = True
|
||||
except BaseException:
|
||||
receipt["partial_evidence"] = {}
|
||||
for name in [
|
||||
"security",
|
||||
"login-budget",
|
||||
"telemetry",
|
||||
"observability-persistence",
|
||||
"cluster",
|
||||
"pipeline",
|
||||
"cleanup",
|
||||
"gc-seed",
|
||||
]:
|
||||
partial = clone / "evidence" / ("clean-" + name + ".json")
|
||||
if partial.exists():
|
||||
receipt["partial_evidence"][name] = json.loads(
|
||||
redact(partial.read_text(), clone)
|
||||
)
|
||||
receipt["failure_diagnostics"] = {}
|
||||
for label, arguments in [
|
||||
("services", ["ps", "-a", "--format", "json"]),
|
||||
(
|
||||
"logs",
|
||||
[
|
||||
"logs",
|
||||
"--no-color",
|
||||
"--tail",
|
||||
"60",
|
||||
"api",
|
||||
"s3",
|
||||
"migrate",
|
||||
"cache",
|
||||
],
|
||||
),
|
||||
]:
|
||||
diagnostic = subprocess.run(
|
||||
["docker", "compose", *arguments],
|
||||
cwd=clone,
|
||||
env=env,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
receipt["failure_diagnostics"][label] = redact(
|
||||
diagnostic.stdout + diagnostic.stderr, clone
|
||||
)
|
||||
raise
|
||||
finally:
|
||||
assert project.startswith("mtusi-lab6-clean-")
|
||||
assert project.startswith("mtusi-lab7-clean-")
|
||||
run(
|
||||
["docker", "compose", "down", "--volumes", "--remove-orphans"],
|
||||
cwd=clone,
|
||||
|
||||
Reference in New Issue
Block a user