340 lines
12 KiB
Bash
Executable File
340 lines
12 KiB
Bash
Executable File
#!/bin/sh
|
|
# Scan the built Crank server images with a pinned Trivy and fail closed.
|
|
#
|
|
# The wrapper loads or downloads and checksum-verifies one fixed Trivy release,
|
|
# downloads one fresh vulnerability database, then scans every requested image by its
|
|
# immutable Docker id with a fully pinned policy. It never trusts a local
|
|
# trivy.yaml, .trivyignore, VEX file or skip list: --config and --ignorefile
|
|
# point at /dev/null and every inherited TRIVY_* variable is removed before
|
|
# Trivy runs. Docker context and proxy variables are preserved.
|
|
#
|
|
# Artifacts are written to the evidence directory (default
|
|
# .tmp/image-scan-evidence, relocatable only through CRANK_IMAGE_SCAN_OUTPUT_DIR)
|
|
# and are kept even when the gate is red:
|
|
#
|
|
# db/metadata.json copy of the actual downloaded Trivy DB metadata
|
|
# db-download.log database download log
|
|
# scanner-cache-source.log archive source provenance
|
|
# scanner.json pinned scanner version and scan policy receipt
|
|
# <NN-slug>.json raw Trivy JSON report per image
|
|
# <NN-slug>.scan.log Trivy scan log per image
|
|
# <NN-slug>.gate.log checker output per image
|
|
# <NN-slug>.decision.json checker decision per image
|
|
# receipt.json aggregate tag<->immutable id<->raw<->decision receipt
|
|
#
|
|
# Every scan is finished by the accepted checker (scripts/check-image-scan.py),
|
|
# which owns the PASS/BLOCKED/ERROR decision and the DB-age check. The wrapper
|
|
# exits 0 only when every image passed, 1 when at least one image is blocked and
|
|
# 2 on any bootstrap, scan or evidence error.
|
|
set -eu
|
|
|
|
if [ "$#" -eq 0 ]; then
|
|
echo "usage: scripts/scan-images.sh IMAGE [IMAGE ...]" >&2
|
|
exit 2
|
|
fi
|
|
|
|
# Strip inherited TRIVY_* overrides so neither the environment nor a local Trivy
|
|
# config can loosen the policy. Only names are read; no values are logged.
|
|
for name in $(env | sed -n 's/^\(TRIVY_[A-Za-z0-9_]*\)=.*/\1/p'); do
|
|
unset "$name"
|
|
done
|
|
|
|
script_dir="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
|
|
checker="$script_dir/check-image-scan.py"
|
|
|
|
trivy_version="0.70.0"
|
|
archive="trivy_${trivy_version}_Linux-64bit.tar.gz"
|
|
expected_sha256="8b4376d5d6befe5c24d503f10ff136d9e0c49f9127a4279fd110b727929a5aa9"
|
|
download_url="https://github.com/aquasecurity/trivy/releases/download/v${trivy_version}/${archive}"
|
|
# Fixed GitHub asset ID for the Trivy 0.70.0 Linux 64-bit release archive.
|
|
asset_api_url="https://api.github.com/repos/aquasecurity/trivy/releases/assets/398381981"
|
|
tool_cache_root="${RUNNER_TOOL_CACHE:-/opt/hostedtoolcache}"
|
|
cached_archive="$tool_cache_root/trivy/$trivy_version/$archive"
|
|
db_repository_primary="ghcr.io/aquasecurity/trivy-db:2"
|
|
db_repository_fallback="mirror.gcr.io/aquasec/trivy-db:2"
|
|
|
|
output_dir="${CRANK_IMAGE_SCAN_OUTPUT_DIR:-.tmp/image-scan-evidence}"
|
|
if ! mkdir -p "$output_dir"; then
|
|
echo "scan-images: cannot create evidence directory $output_dir" >&2
|
|
exit 2
|
|
fi
|
|
if ! output_dir="$(CDPATH= cd -- "$output_dir" && pwd)"; then
|
|
echo "scan-images: cannot resolve evidence directory $output_dir" >&2
|
|
exit 2
|
|
fi
|
|
# Each invocation owns a fresh artifact directory. An absent new report can
|
|
# never be mistaken for a successful report left by a previous invocation.
|
|
if ! output_dir="$(mktemp -d "$output_dir/run.XXXXXX")"; then
|
|
echo "scan-images: cannot create a fresh evidence run directory" >&2
|
|
exit 2
|
|
fi
|
|
|
|
temp_dir="$(mktemp -d)"
|
|
cache_dir="$temp_dir/trivy-cache"
|
|
mkdir -p "$cache_dir"
|
|
cleanup() { rm -rf "$temp_dir"; }
|
|
trap cleanup EXIT HUP INT TERM
|
|
|
|
requested="$temp_dir/requested-images.txt"
|
|
printf '%s\n' "$@" > "$requested"
|
|
records="$temp_dir/records.jsonl"
|
|
: > "$records"
|
|
|
|
# Bootstrap failures still produce a receipt naming every requested image.
|
|
fail_all() {
|
|
reason="$1"
|
|
python3 - "$output_dir/receipt.json" "$reason" "$requested" <<'PY'
|
|
import json
|
|
import sys
|
|
|
|
receipt_path, reason, requested_path = sys.argv[1:4]
|
|
with open(requested_path, encoding="utf-8") as stream:
|
|
images = [line for line in stream.read().splitlines() if line]
|
|
receipt = {
|
|
"schema": "crank.image-scan.receipt.v1",
|
|
"error": reason,
|
|
"overall": {"status": "error", "exit_code": 2},
|
|
"images": [
|
|
{"index": index + 1, "tag": tag, "status": "error", "error": reason}
|
|
for index, tag in enumerate(images)
|
|
],
|
|
}
|
|
with open(receipt_path, "w", encoding="utf-8") as stream:
|
|
json.dump(receipt, stream, indent=2, sort_keys=True)
|
|
stream.write("\n")
|
|
PY
|
|
echo "scan-images: $reason" >&2
|
|
exit 2
|
|
}
|
|
|
|
# Load the exact-version archive from the runner cache when preinstalled;
|
|
# otherwise use the bounded network routes. The shared cache is read-only here.
|
|
cache_source_log="$output_dir/scanner-cache-source.log"
|
|
: > "$cache_source_log"
|
|
if [ -f "$cached_archive" ]; then
|
|
printf 'archive_source=runner-tool-cache\n' > "$cache_source_log"
|
|
if ! cp "$cached_archive" "$temp_dir/$archive"; then
|
|
fail_all "could not copy the pinned Trivy archive from runner tool cache"
|
|
fi
|
|
else
|
|
printf 'archive_source=network\n' > "$cache_source_log"
|
|
if ! curl --fail --silent --show-error --location --max-time 300 \
|
|
"$download_url" --output "$temp_dir/$archive" \
|
|
>"$output_dir/scanner-download.log" 2>&1; then
|
|
rm -f "$temp_dir/$archive"
|
|
if ! curl --fail --silent --show-error --location --max-time 300 \
|
|
--header 'Accept: application/octet-stream' \
|
|
"$asset_api_url" --output "$temp_dir/$archive" \
|
|
>"$output_dir/scanner-api-download.log" 2>&1; then
|
|
printf 'network_download=failed\n' >> "$cache_source_log"
|
|
fail_all "could not download pinned Trivy ${trivy_version} from release URL or GitHub asset API"
|
|
fi
|
|
printf 'archive_source=github-release-asset-api\n' > "$cache_source_log"
|
|
printf 'Downloaded pinned Trivy %s from GitHub release asset API.\n' "$trivy_version" \
|
|
>>"$output_dir/scanner-api-download.log"
|
|
else
|
|
printf 'archive_source=github-release-download\n' > "$cache_source_log"
|
|
fi
|
|
fi
|
|
if ! printf '%s %s\n' "$expected_sha256" "$temp_dir/$archive" \
|
|
| sha256sum --check --status; then
|
|
fail_all "pinned Trivy archive checksum mismatch"
|
|
fi
|
|
trivy_bin="$temp_dir/trivy"
|
|
if ! tar -C "$temp_dir" -xzf "$temp_dir/$archive" trivy; then
|
|
fail_all "could not extract the pinned Trivy archive"
|
|
fi
|
|
if [ ! -x "$trivy_bin" ]; then
|
|
fail_all "pinned Trivy executable is missing after extraction"
|
|
fi
|
|
|
|
# Download one fresh vulnerability database; every scan reuses it read-only.
|
|
if ! "$trivy_bin" image --config /dev/null --download-db-only --cache-dir "$cache_dir" \
|
|
--db-repository "$db_repository_primary" \
|
|
--db-repository "$db_repository_fallback" \
|
|
--no-progress --skip-version-check \
|
|
>"$output_dir/db-download.log" 2>&1; then
|
|
fail_all "could not download the Trivy vulnerability database"
|
|
fi
|
|
db_metadata="$output_dir/db/metadata.json"
|
|
mkdir -p "$output_dir/db"
|
|
if ! cp "$cache_dir/db/metadata.json" "$db_metadata"; then
|
|
fail_all "Trivy database metadata was not produced"
|
|
fi
|
|
|
|
if ! version_output="$("$trivy_bin" --config /dev/null --cache-dir "$cache_dir" --version 2>&1)"; then
|
|
fail_all "could not read pinned Trivy version"
|
|
fi
|
|
reported_version="$(printf '%s\n' "$version_output" | sed -n 's/^Version:[[:space:]]*//p' | head -n 1)"
|
|
if [ "$reported_version" != "$trivy_version" ]; then
|
|
fail_all "pinned Trivy reported an unexpected version"
|
|
fi
|
|
python3 - "$output_dir/scanner.json" "$trivy_version" "$expected_sha256" \
|
|
"$archive" "$version_output" <<'PY'
|
|
import json
|
|
import sys
|
|
|
|
path, expected, digest, archive, reported = sys.argv[1:6]
|
|
reported_version = next(
|
|
(line.strip() for line in reported.splitlines() if line.strip()), ""
|
|
)
|
|
payload = {
|
|
"schema": "crank.image-scan.scanner.v1",
|
|
"expected_version": expected,
|
|
"reported_version": reported_version,
|
|
"archive": archive,
|
|
"archive_sha256": digest,
|
|
"policy": {
|
|
"image_source": "docker",
|
|
"config": "/dev/null",
|
|
"ignorefile": "/dev/null",
|
|
"ignore_unfixed": False,
|
|
"list_all_pkgs": True,
|
|
"severities": ["UNKNOWN", "LOW", "MEDIUM", "HIGH", "CRITICAL"],
|
|
"scanners": ["vuln"],
|
|
"skip_db_update": True,
|
|
"skip_version_check": True,
|
|
},
|
|
}
|
|
with open(path, "w", encoding="utf-8") as stream:
|
|
json.dump(payload, stream, indent=2, sort_keys=True)
|
|
stream.write("\n")
|
|
PY
|
|
|
|
overall_rc=0
|
|
index=0
|
|
for image in "$@"; do
|
|
index=$((index + 1))
|
|
slug="$(printf '%s' "$image" \
|
|
| sed 's/[^A-Za-z0-9._-]/_/g; s/^[.-]*//; s/[.-]*$//' \
|
|
| cut -c1-80)"
|
|
[ -n "$slug" ] || slug="image"
|
|
base="$(printf '%02d' "$index")-$slug"
|
|
raw="$output_dir/$base.json"
|
|
scan_log="$output_dir/$base.scan.log"
|
|
gate_log="$output_dir/$base.gate.log"
|
|
decision="$output_dir/$base.decision.json"
|
|
|
|
image_id="$(docker image inspect --format '{{.Id}}' "$image" \
|
|
2>"$output_dir/$base.inspect.log" || true)"
|
|
image_id="$(printf '%s' "$image_id" | tr -d '[:space:]')"
|
|
|
|
scan_rc=0
|
|
if [ -z "$image_id" ]; then
|
|
scan_rc=2
|
|
printf 'scan-images: cannot resolve an immutable id for %s\n' "$image" >&2
|
|
elif "$trivy_bin" image \
|
|
--image-src docker \
|
|
--config /dev/null \
|
|
--ignorefile /dev/null \
|
|
--ignore-unfixed=false \
|
|
--list-all-pkgs=true \
|
|
--severity UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL \
|
|
--scanners vuln \
|
|
--format json \
|
|
--exit-code 0 \
|
|
--skip-db-update \
|
|
--cache-dir "$cache_dir" \
|
|
--no-progress \
|
|
--skip-version-check \
|
|
--output "$raw" \
|
|
"$image_id" >"$scan_log" 2>&1; then
|
|
scan_rc=0
|
|
else
|
|
scan_rc=$?
|
|
fi
|
|
|
|
# The checker is always invoked so every image gets a decision artifact,
|
|
# even when the scan itself failed. Missing or malformed evidence is
|
|
# reported by the checker itself with exit code 2.
|
|
if gate_output="$(python3 "$checker" \
|
|
--report "$raw" \
|
|
--image-id "$image_id" \
|
|
--db-metadata "$db_metadata" \
|
|
--decision "$decision" 2>&1)"; then
|
|
gate_rc=0
|
|
else
|
|
gate_rc=$?
|
|
fi
|
|
printf '%s\n' "$gate_output" >"$gate_log"
|
|
printf '%s\n' "$gate_output"
|
|
|
|
effective_rc="$gate_rc"
|
|
if [ "$scan_rc" -ne 0 ] && [ "$effective_rc" -lt 2 ]; then
|
|
effective_rc=2
|
|
fi
|
|
if [ "$effective_rc" -gt "$overall_rc" ]; then
|
|
overall_rc="$effective_rc"
|
|
fi
|
|
|
|
case "$effective_rc" in
|
|
0) status="pass" ;;
|
|
1) status="blocked" ;;
|
|
*) status="error" ;;
|
|
esac
|
|
|
|
python3 - "$records" "$index" "$image" "$image_id" "$raw" "$scan_log" \
|
|
"$gate_log" "$decision" "$scan_rc" "$gate_rc" "$status" <<'PY'
|
|
import json
|
|
import sys
|
|
|
|
(
|
|
records_path, index, tag, image_id, raw, scan_log, gate_log, decision,
|
|
scan_exit, gate_exit, status,
|
|
) = sys.argv[1:12]
|
|
record = {
|
|
"index": int(index),
|
|
"tag": tag,
|
|
"image_id": image_id or None,
|
|
"raw": raw,
|
|
"scan_log": scan_log,
|
|
"gate_log": gate_log,
|
|
"decision": decision,
|
|
"scan_exit": int(scan_exit),
|
|
"gate_exit": int(gate_exit),
|
|
"status": status,
|
|
}
|
|
with open(records_path, "a", encoding="utf-8") as stream:
|
|
stream.write(json.dumps(record) + "\n")
|
|
PY
|
|
done
|
|
|
|
case "$overall_rc" in
|
|
0) overall_status="pass" ;;
|
|
1) overall_status="blocked" ;;
|
|
*) overall_status="error" ;;
|
|
esac
|
|
|
|
python3 - "$records" "$output_dir/receipt.json" "$db_metadata" \
|
|
"$output_dir/scanner.json" "$overall_rc" "$overall_status" "$output_dir" <<'PY'
|
|
import json
|
|
import sys
|
|
|
|
records_path, receipt_path, db_path, scanner_path, exit_code, status, out_dir = (
|
|
sys.argv[1:8]
|
|
)
|
|
with open(records_path, encoding="utf-8") as stream:
|
|
images = [json.loads(line) for line in stream if line.strip()]
|
|
with open(scanner_path, encoding="utf-8") as stream:
|
|
scanner = json.load(stream)
|
|
with open(db_path, encoding="utf-8") as stream:
|
|
db_metadata = json.load(stream)
|
|
receipt = {
|
|
"schema": "crank.image-scan.receipt.v1",
|
|
"output_dir": out_dir,
|
|
"scanner": scanner,
|
|
"database": {
|
|
"metadata_path": db_path,
|
|
"updated_at": db_metadata.get("UpdatedAt"),
|
|
},
|
|
"overall": {"status": status, "exit_code": int(exit_code)},
|
|
"images": images,
|
|
}
|
|
with open(receipt_path, "w", encoding="utf-8") as stream:
|
|
json.dump(receipt, stream, indent=2, sort_keys=True)
|
|
stream.write("\n")
|
|
PY
|
|
|
|
printf 'scan-images: %s (%s/receipt.json)\n' "$overall_status" "$output_dir"
|
|
exit "$overall_rc"
|