forked from mtusi2026/mtusi2026lab7
test(security): verify full lifecycle and audit clean-clone private material
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
"""Check submission files and all reachable Git objects against local secrets."""
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def strings(value):
|
||||
if isinstance(value, dict):
|
||||
for child in value.values():
|
||||
yield from strings(child)
|
||||
elif isinstance(value, list):
|
||||
for child in value:
|
||||
yield from strings(child)
|
||||
elif isinstance(value, str) and len(value) >= 20:
|
||||
yield value.encode()
|
||||
|
||||
|
||||
def main():
|
||||
known = set()
|
||||
for directory in [ROOT / "secrets", ROOT / "operator"]:
|
||||
for path in directory.rglob("*"):
|
||||
if not path.is_file():
|
||||
continue
|
||||
raw = path.read_bytes().strip()
|
||||
if path.suffix == ".json":
|
||||
if path.name in {
|
||||
"service-secrets.json",
|
||||
"operator-token.json",
|
||||
"secrets.json",
|
||||
} or path.name.startswith("unseal-"):
|
||||
known.update(strings(json.loads(raw)))
|
||||
elif path.name == "s3-rotation.json":
|
||||
journal = json.loads(raw)
|
||||
known.update(strings(journal.get("previous_values", {})))
|
||||
known.update(strings(journal.get("next_values", {})))
|
||||
elif len(raw) >= 20 and (
|
||||
path.suffix != ".pem" or "key" in path.name
|
||||
):
|
||||
known.add(raw)
|
||||
|
||||
def inspect(data):
|
||||
assert not any(value in data for value in known), (
|
||||
"Local private material was found in submission content"
|
||||
)
|
||||
assert b"/" + b"Users/" not in data, "Machine path in submission"
|
||||
|
||||
result = subprocess.check_output(
|
||||
[
|
||||
"git",
|
||||
"ls-files",
|
||||
"-z",
|
||||
"--cached",
|
||||
"--others",
|
||||
"--exclude-standard",
|
||||
],
|
||||
cwd=ROOT,
|
||||
)
|
||||
files = 0
|
||||
for name in result.split(b"\0"):
|
||||
if name and (path := ROOT / name.decode()).is_file():
|
||||
inspect(path.read_bytes())
|
||||
files += 1
|
||||
objects = subprocess.check_output(
|
||||
["git", "rev-list", "--objects", "--all"], cwd=ROOT
|
||||
).splitlines()
|
||||
with subprocess.Popen(
|
||||
["git", "cat-file", "--batch"],
|
||||
cwd=ROOT,
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
) as process:
|
||||
for line in objects:
|
||||
process.stdin.write(line.split()[0] + b"\n")
|
||||
process.stdin.flush()
|
||||
header = process.stdout.readline().split()
|
||||
assert len(header) == 3
|
||||
inspect(process.stdout.read(int(header[2])))
|
||||
assert process.stdout.read(1) == b"\n"
|
||||
process.stdin.close()
|
||||
assert process.wait(timeout=10) == 0
|
||||
print(
|
||||
f"PASS: {files} submission files, {len(objects)} reachable objects, {len(known)} local private values absent"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -97,6 +97,11 @@ def main():
|
||||
output = ROOT / args.output
|
||||
if output.exists():
|
||||
raise SystemExit("Choose new output path")
|
||||
for name in ["pki-research-final.json", "pki-research-final-requests.csv"]:
|
||||
if (ROOT / "evidence" / name).exists():
|
||||
raise SystemExit(
|
||||
"Retain the existing research receipt under a distinct name before a fresh full run"
|
||||
)
|
||||
assert not subprocess.check_output(
|
||||
["git", "status", "--porcelain"], cwd=ROOT
|
||||
)
|
||||
@@ -271,6 +276,31 @@ def main():
|
||||
receipt["cluster"] = json.loads(
|
||||
(clone / "evidence/clean-cluster.json").read_text()
|
||||
)
|
||||
run(
|
||||
[
|
||||
".venv/bin/python",
|
||||
"-m",
|
||||
"scripts.research_security",
|
||||
"--output",
|
||||
"evidence/pki-research-final.json",
|
||||
],
|
||||
cwd=clone,
|
||||
env=env,
|
||||
)
|
||||
receipt["security_research"] = json.loads(
|
||||
(clone / "evidence/pki-research-final.json").read_text()
|
||||
)
|
||||
for name in [
|
||||
"pki-research-final.json",
|
||||
"pki-research-final-requests.csv",
|
||||
]:
|
||||
destination = ROOT / "evidence" / name
|
||||
assert not destination.exists(), (
|
||||
"Choose a fresh research receipt path"
|
||||
)
|
||||
destination.write_text(
|
||||
redact((clone / "evidence" / name).read_text(), clone)
|
||||
)
|
||||
import hashlib
|
||||
|
||||
expected = {
|
||||
@@ -326,6 +356,11 @@ def main():
|
||||
receipt["fsck"] = run(
|
||||
["git", "fsck", "--full"], cwd=clone, env=env
|
||||
)
|
||||
receipt["private_material_audit"] = run(
|
||||
[".venv/bin/python", "-m", "scripts.audit_secrets"],
|
||||
cwd=clone,
|
||||
env=env,
|
||||
)
|
||||
receipt["checks_completed"] = True
|
||||
except BaseException:
|
||||
receipt["partial_evidence"] = {}
|
||||
@@ -338,6 +373,7 @@ def main():
|
||||
"pipeline",
|
||||
"cleanup",
|
||||
"gc-seed",
|
||||
"pki-research-final",
|
||||
]:
|
||||
partial = clone / "evidence" / ("clean-" + name + ".json")
|
||||
if partial.exists():
|
||||
|
||||
@@ -175,7 +175,7 @@ def main():
|
||||
]
|
||||
limited = [x for x in report["measurements"] if x["status"] == 429]
|
||||
assert limited and admitted
|
||||
assert len(admitted) <= 8 + int(2 * duration) + 1
|
||||
assert len(admitted) <= 16 + int(8 * duration) + 1
|
||||
assert len({x["instance"] for x in report["measurements"]}) == 2
|
||||
assert health and all(x["status"] == 200 for x in health)
|
||||
buckets = int(
|
||||
@@ -195,7 +195,7 @@ def main():
|
||||
),
|
||||
"both_replicas_observed": True,
|
||||
"new_account_buckets": buckets,
|
||||
"shared_admission_bound": 8 + int(2 * duration) + 1,
|
||||
"shared_admission_bound": 16 + int(8 * duration) + 1,
|
||||
"limited_median_seconds": statistics.median(
|
||||
x["seconds"] for x in limited
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user