forked from mtusi2026/mtusi2026lab7
169 lines
5.4 KiB
Python
169 lines
5.4 KiB
Python
import hashlib
|
|
import json
|
|
import subprocess
|
|
from collections import Counter
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
RAW = ROOT / "build/scans"
|
|
SCANNER = (
|
|
"aquasec/trivy:0.69.3@sha256:bcc376de8d77cfe086a917230e818dc9f8528e3c852f7b1aff648949b6258d1c"
|
|
)
|
|
|
|
|
|
def scan(name, mode, target, mount=None, extra=()):
|
|
command = [
|
|
"docker",
|
|
"run",
|
|
"--rm",
|
|
"--security-opt",
|
|
"no-new-privileges:true",
|
|
"--cap-drop",
|
|
"ALL",
|
|
"-v",
|
|
"messenger-lab7-trivy-cache:/root/.cache/trivy",
|
|
"-v",
|
|
str(RAW) + ":/reports",
|
|
]
|
|
if mount:
|
|
command += ["-v", mount]
|
|
command += [
|
|
SCANNER,
|
|
mode,
|
|
"--timeout",
|
|
"15m",
|
|
"--scanners",
|
|
"vuln",
|
|
"--no-progress",
|
|
"--skip-version-check",
|
|
"--format",
|
|
"json",
|
|
"--output",
|
|
"/reports/" + name + ".json",
|
|
*extra,
|
|
target,
|
|
]
|
|
result = subprocess.run(command, capture_output=True)
|
|
if result.returncode:
|
|
raise RuntimeError(
|
|
"Scanner failed for " + name + ": " + result.stderr.decode(errors="replace")[-1000:]
|
|
)
|
|
return command
|
|
|
|
|
|
def summary(path, command):
|
|
raw = path.read_bytes()
|
|
document = json.loads(raw)
|
|
rows = []
|
|
packages = set()
|
|
for result in document.get("Results", []):
|
|
for package in result.get("Packages", []):
|
|
packages.add((package["Name"], package.get("Version", "not recorded")))
|
|
for value in result.get("Vulnerabilities", []):
|
|
rows.append(
|
|
{
|
|
k: value.get(k)
|
|
for k in (
|
|
"VulnerabilityID",
|
|
"PkgName",
|
|
"InstalledVersion",
|
|
"FixedVersion",
|
|
"Severity",
|
|
"Status",
|
|
"PrimaryURL",
|
|
)
|
|
}
|
|
)
|
|
return dict(
|
|
command=command,
|
|
exit_code=0,
|
|
artifact=document.get("ArtifactName"),
|
|
scanner=document.get("Metadata", {}).get("RepoDigests", []),
|
|
raw_sha256=hashlib.sha256(raw).hexdigest(),
|
|
findings=len(rows),
|
|
severity=dict(Counter(r["Severity"] for r in rows)),
|
|
resolved_package_records=len(packages),
|
|
vulnerabilities=rows,
|
|
)
|
|
|
|
|
|
def main():
|
|
RAW.mkdir(parents=True, exist_ok=True)
|
|
compose = yaml.safe_load((ROOT / "docker-compose.yml").read_text())
|
|
jars = list((ROOT / "build/libs").glob("messenger-lab7-*.jar"))
|
|
if len(jars) != 1:
|
|
raise RuntimeError("Build exactly one current messenger-lab7 bootJar first")
|
|
jobs = [
|
|
(
|
|
"worker-resolved-scan",
|
|
"fs",
|
|
"/scan",
|
|
str(ROOT / "worker/requirements-resolved.txt") + ":/scan/requirements.txt:ro",
|
|
(),
|
|
),
|
|
("java-scan", "rootfs", "/scan", str(jars[0]) + ":/scan/app.jar:ro", ()),
|
|
(
|
|
"image-jre-scan",
|
|
"image",
|
|
(ROOT / "Dockerfile").read_text().split("FROM ")[-1].split()[0],
|
|
None,
|
|
("--image-src", "remote", "--pkg-types", "os"),
|
|
),
|
|
(
|
|
"image-worker-updated-scan",
|
|
"image",
|
|
(ROOT / "worker/Dockerfile").read_text().splitlines()[0].split()[1],
|
|
None,
|
|
("--image-src", "remote", "--pkg-types", "os"),
|
|
),
|
|
(
|
|
"image-nginx-scan",
|
|
"image",
|
|
compose["services"]["gateway"]["image"],
|
|
None,
|
|
("--image-src", "remote"),
|
|
),
|
|
(
|
|
"image-bao-scan",
|
|
"image",
|
|
compose["services"]["openbao"]["image"],
|
|
None,
|
|
("--image-src", "remote"),
|
|
),
|
|
]
|
|
previous = ROOT / "evidence/scan-summary.json"
|
|
old = json.loads(previous.read_text()).get("reports", {}) if previous.exists() else {}
|
|
results = {
|
|
name: value
|
|
for name, value in old.items()
|
|
if "before" in name or name == "image-worker-base-scan"
|
|
}
|
|
for name, mode, target, mount, extra in jobs:
|
|
command = scan(name, mode, target, mount, extra)
|
|
results[name] = summary(RAW / (name + ".json"), command)
|
|
print(name + ": " + str(results[name]["severity"]), flush=True)
|
|
for path in (ROOT / "evidence").glob("*-scan*.json"):
|
|
if path.name == "scan-summary.json":
|
|
continue
|
|
if "before" in path.stem or path.stem == "image-worker-base-scan":
|
|
results[path.stem] = summary(
|
|
path, "Initial recorded Trivy 0.69.3 baseline; see SCAN_TRIAGE.md"
|
|
)
|
|
output = dict(
|
|
scanner=SCANNER,
|
|
scope="Runtime Java JAR, all resolved worker packages including pip, four production base images. OS packages are unchanged by application Dockerfiles; no socket or secrets mount. Native libraries bundled inside wheels and all other infrastructure images are not a complete SBOM coverage claim.",
|
|
reports=results,
|
|
)
|
|
(ROOT / "evidence/scan-summary.json").write_text(
|
|
json.dumps(output, indent=2) + "\n", encoding="utf-8"
|
|
)
|
|
for name in ("worker-resolved-scan", "java-scan"):
|
|
if results[name]["findings"]:
|
|
raise RuntimeError("Runtime application dependency findings require triage: " + name)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|