forked from mtusi2026/mtusi2026lab7
37 lines
1.5 KiB
Bash
Executable File
37 lines
1.5 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
cd "$(dirname "$0")/.."
|
|
cache=mtusi2026lab7_grype_cache
|
|
grype=anchore/grype:v0.110.0
|
|
docker run --rm -e GRYPE_DB_CACHE_DIR=/cache -v "$cache:/cache" "$grype" db update >&2
|
|
work=$(mktemp -d)
|
|
trap 'rm -rf "$work"' EXIT
|
|
if [ "$#" -gt 0 ]; then
|
|
images=$(printf '%s\n' "$@")
|
|
else
|
|
images=$(docker compose --profile operator config --images | sort -u)
|
|
fi
|
|
printf '%s\n' "$images" | while IFS= read -r image; do
|
|
[ -n "$image" ] || continue
|
|
docker image save "$image" -o "$work/image.tar"
|
|
docker run --rm -e GRYPE_DB_CACHE_DIR=/cache -e GRYPE_DB_AUTO_UPDATE=false \
|
|
-v "$cache:/cache" -v "$work/image.tar:/image.tar:ro" "$grype" docker-archive:/image.tar -o json \
|
|
> "$work/result.json"
|
|
python3 - "$image" "$work/result.json" <<'PY'
|
|
import collections
|
|
import json
|
|
import sys
|
|
doc = json.load(open(sys.argv[2]))
|
|
assert doc["descriptor"]["db"]["status"]["valid"]
|
|
counts = collections.Counter(entry["vulnerability"]["severity"] for entry in doc["matches"])
|
|
examples = []
|
|
for entry in doc["matches"]:
|
|
vuln, pkg = entry["vulnerability"], entry["artifact"]
|
|
if vuln["severity"] == "Critical" or (pkg["type"] == "python" and vuln["severity"] == "High"):
|
|
examples.append({"id": vuln["id"], "package": pkg["name"], "version": pkg["version"],
|
|
"fix": vuln["fix"]["versions"]})
|
|
print(json.dumps({"image": sys.argv[1], "db_built": doc["descriptor"]["db"]["status"]["built"],
|
|
"counts": dict(counts), "critical_or_python_high": examples[:10]}))
|
|
PY
|
|
done
|