Default scan reports to .build and refuse writing under docs/. Co-authored-by: Cursor <cursoragent@cursor.com>
115 lines
4.3 KiB
Bash
Executable File
115 lines
4.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Scan published Marqdo CI images by digest.
|
|
#
|
|
# Records:
|
|
# - registry digest / platform manifests (buildx imagetools)
|
|
# - optional Trivy CVE report when `trivy` or `$TRIVY_IMAGE` is available
|
|
# - a fail-closed provenance check that consumers pin immutable tags
|
|
#
|
|
# Usage:
|
|
# ci/images/scan.sh [--out <dir>]
|
|
# Output is a CI/local artifact directory only. Do not commit scan reports into
|
|
# the repository; record results on the tracking Issue or as pipeline assets.
|
|
set -euo pipefail
|
|
root="$(cd "$(dirname "$0")/../.." && pwd)"
|
|
registry="${MARQDO_CI_REGISTRY:-git.cflmy.cn/cflmy}"
|
|
version="${MARQDO_CLI_VERSION:-3.0.0-rc.5}"
|
|
out="$root/.build/ci-image-scan-$version"
|
|
if [[ "${1:-}" == "--out" ]]; then
|
|
out="${2:?}"
|
|
elif [[ -n "${1:-}" ]]; then
|
|
out="$1"
|
|
fi
|
|
mkdir -p "$out"
|
|
# Refuse to write under tracked docs/ — that inflates the repository.
|
|
case "$out" in
|
|
"$root/docs"|"$root/docs"/*)
|
|
echo "scan.sh: refuse output under docs/; use .build/ or a CI workspace" >&2
|
|
exit 2
|
|
;;
|
|
esac
|
|
images=(
|
|
"marqdo-ci-shell:$version"
|
|
"marqdo-ci-rust:$version"
|
|
"marqdo-ci-web:$version"
|
|
)
|
|
|
|
echo "==> inspect manifests"
|
|
: >"$out/digests.txt"
|
|
for image in "${images[@]}"; do
|
|
ref="$registry/$image"
|
|
echo "--- $ref" | tee -a "$out/digests.txt"
|
|
if ! docker buildx imagetools inspect "$ref" >"$out/${image//[:\/]/_}.inspect.txt" 2>"$out/${image//[:\/]/_}.inspect.err"; then
|
|
echo "UNVERIFIED: cannot inspect $ref" | tee -a "$out/digests.txt"
|
|
continue
|
|
fi
|
|
digest=$(awk '/^Digest:/{print $2; exit}' "$out/${image//[:\/]/_}.inspect.txt")
|
|
platforms=$(awk '/Platform:/{print $2}' "$out/${image//[:\/]/_}.inspect.txt" | paste -sd, -)
|
|
echo "$ref digest=$digest platforms=$platforms" | tee -a "$out/digests.txt"
|
|
done
|
|
|
|
echo "==> optional Trivy scan"
|
|
trivy_bin="${TRIVY_BIN:-}"
|
|
if [[ -z "$trivy_bin" && -x "$root/.build/tools/trivy" ]]; then
|
|
trivy_bin="$root/.build/tools/trivy"
|
|
fi
|
|
if [[ -n "$trivy_bin" ]]; then
|
|
for image in "${images[@]}"; do
|
|
ref="$registry/$image"
|
|
"$trivy_bin" image --quiet --format json --output "$out/${image//[:\/]/_}.trivy.json" "$ref" || \
|
|
echo "trivy failed for $ref" >"$out/${image//[:\/]/_}.trivy.err"
|
|
done
|
|
elif [[ -n "${TRIVY_IMAGE:-}" ]]; then
|
|
for image in "${images[@]}"; do
|
|
ref="$registry/$image"
|
|
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
|
|
"$TRIVY_IMAGE" image --quiet --format json "$ref" \
|
|
>"$out/${image//[:\/]/_}.trivy.json" || \
|
|
echo "trivy container failed for $ref" >"$out/${image//[:\/]/_}.trivy.err"
|
|
done
|
|
else
|
|
echo "Trivy binary/image unavailable; CVE scan marked UNVERIFIED" | tee "$out/trivy-UNVERIFIED.txt"
|
|
fi
|
|
|
|
echo "==> provenance / pin check"
|
|
ROOT="$root" OUT="$out" VERSION="$version" python3 - <<'PY'
|
|
import os, pathlib, re, sys
|
|
root = pathlib.Path(os.environ["ROOT"])
|
|
out = pathlib.Path(os.environ["OUT"])
|
|
version = os.environ["VERSION"]
|
|
pattern = re.compile(r"git\.cflmy\.cn/cflmy/marqdo-ci-(shell|rust|web):([^\s\"']+)")
|
|
bad = []
|
|
skip = {".build", "node_modules", ".git", "cargo-home", ".cargo"}
|
|
for path in list(root.glob("**/.woodpecker/*.yml")) + list(root.glob("**/.woodpecker/*.yaml")):
|
|
if any(part in skip for part in path.parts):
|
|
continue
|
|
text = path.read_text(encoding="utf-8", errors="replace")
|
|
for match in pattern.finditer(text):
|
|
tag = match.group(2)
|
|
if tag in ("latest", "main", "master") or tag.endswith("-latest"):
|
|
bad.append(f"{path}: floating tag {tag}")
|
|
elif tag != version and not tag.startswith(version):
|
|
bad.append(f"{path}: unexpected tag {tag} (expected {version})")
|
|
report = out / "pin-check.txt"
|
|
if bad:
|
|
report.write_text("FAIL\n" + "\n".join(bad) + "\n", encoding="utf-8")
|
|
print("pin check failed; see", report, file=sys.stderr)
|
|
raise SystemExit(1)
|
|
report.write_text(f"PASS: workflows pin marqdo-ci-*:{version}\n", encoding="utf-8")
|
|
print(report.read_text().strip())
|
|
PY
|
|
|
|
cat >"$out/SUMMARY.md" <<EOF
|
|
# CI image scan ($version)
|
|
|
|
- Registry: \`$registry\`
|
|
- Digests: see \`digests.txt\`
|
|
- Pin check: see \`pin-check.txt\`
|
|
- CVE: Trivy output when available; otherwise \`trivy-UNVERIFIED.txt\`
|
|
- Platforms: current production consumers use \`linux/amd64\`. Multi-arch
|
|
builds are produced by \`ci/images/build.sh\` when \`MARQDO_CI_PLATFORMS\`
|
|
includes \`linux/arm64\`; arm64 CI execution remains UNVERIFIED until an
|
|
arm64 runner exists.
|
|
EOF
|
|
echo "scan complete: $out"
|