forked from mtusi2026/mtusi2026lab7
94 lines
3.0 KiB
Python
94 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Scan local image archives; scanner gets no Docker socket or application secrets."""
|
|
|
|
import argparse
|
|
import collections
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SCANNER = "aquasec/trivy:0.69.3"
|
|
IMAGES = {
|
|
"app": "mtusi-messenger:lab7",
|
|
"minio": "mtusi-minio:RELEASE.2025-10-15T17-29-55Z",
|
|
"postgres": "postgres:16.6-bookworm",
|
|
"redis": "redis:7.4.2-bookworm",
|
|
"gateway": "haproxy:3.0.12-alpine",
|
|
"openbao": "openbao/openbao:2.5.0",
|
|
"collector": "otel/opentelemetry-collector-contrib:0.123.0",
|
|
"prometheus": "prom/prometheus:v3.2.1",
|
|
"loki": "grafana/loki:3.4.2",
|
|
"tempo": "grafana/tempo:2.7.2",
|
|
"grafana": "grafana/grafana:11.6.0",
|
|
}
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--only", nargs="*")
|
|
args = parser.parse_args()
|
|
folder = ROOT / ".local/operator/scan"
|
|
folder.mkdir(exist_ok=True)
|
|
summary = []
|
|
for name, image in IMAGES.items():
|
|
if args.only and name not in args.only:
|
|
continue
|
|
archive = folder / (name + ".tar")
|
|
output = ROOT / "evidence" / ("scan-" + name + ".json")
|
|
subprocess.run(["docker", "save", image, "-o", str(archive)], check=True)
|
|
subprocess.run(
|
|
[
|
|
"docker",
|
|
"run",
|
|
"--rm",
|
|
"-v",
|
|
str(archive) + ":/scan/image.tar:ro",
|
|
"-v",
|
|
"mtusi2026lab7_trivy_cache:/root/.cache/trivy",
|
|
"-v",
|
|
str(ROOT / "evidence") + ":/out",
|
|
SCANNER,
|
|
"image",
|
|
"--quiet",
|
|
"--timeout",
|
|
"15m",
|
|
"--scanners",
|
|
"vuln",
|
|
"--input",
|
|
"/scan/image.tar",
|
|
"--format",
|
|
"json",
|
|
"--output",
|
|
"/out/" + output.name,
|
|
],
|
|
check=True,
|
|
)
|
|
data = json.loads(output.read_text())
|
|
findings = [
|
|
v for result in data.get("Results", []) for v in result.get("Vulnerabilities", [])
|
|
]
|
|
item = {
|
|
"image": image,
|
|
"report": output.name,
|
|
"severity": dict(collections.Counter(v["Severity"] for v in findings)),
|
|
"findings": len(findings),
|
|
"fix_available": sum(bool(v.get("FixedVersion")) for v in findings),
|
|
}
|
|
summary.append(item)
|
|
archive.unlink()
|
|
print(name, item["severity"], flush=True)
|
|
previous = ROOT / "evidence/scan-summary.json"
|
|
if args.only and previous.exists():
|
|
existing = json.loads(previous.read_text())["images"]
|
|
updated = {row["image"]: row for row in existing}
|
|
updated.update({row["image"]: row for row in summary})
|
|
summary = list(updated.values())
|
|
(ROOT / "evidence/scan-summary.json").write_text(
|
|
json.dumps({"scanner": SCANNER, "images": summary}, indent=2) + "\n"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|