fix(ci): load checksum-verified Trivy from runner tool cache

This commit is contained in:
bsodfather committed 2026-11-08 14:21:16 +03:00
1 parent cfe11a0a04
commit 4a7b230f35
2 files changed
+69 -13

No files matched your search

+31 -13
View File
@@ -1,8 +1,8 @@
#!/bin/sh
# Scan the built Crank server images with a pinned Trivy and fail closed.
#
# The wrapper downloads and checksum-verifies one fixed Trivy release, downloads
# one fresh vulnerability database, then scans every requested image by its
# 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
@@ -14,6 +14,7 @@
#
# 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
@@ -47,6 +48,8 @@ expected_sha256="8b4376d5d6befe5c24d503f10ff136d9e0c49f9127a4279fd110b727929a5aa
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"
@@ -104,19 +107,34 @@ PY
exit 2
}
# Download and checksum-verify the pinned scanner exactly once.
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"
# 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 \
--header 'Accept: application/octet-stream' \
"$asset_api_url" --output "$temp_dir/$archive" \
>"$output_dir/scanner-api-download.log" 2>&1; then
fail_all "could not download pinned Trivy ${trivy_version} from release URL or GitHub asset API"
"$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
printf 'Downloaded pinned Trivy %s from GitHub release asset API.\n' "$trivy_version" \
>>"$output_dir/scanner-api-download.log"
fi
if ! printf '%s %s\n' "$expected_sha256" "$temp_dir/$archive" \
| sha256sum --check --status; then
+38
View File
@@ -189,6 +189,7 @@ class ScanImagesWrapperTests(unittest.TestCase):
curl_direct_fail=False,
curl_api_fail=False,
api_checksum_mismatch=False,
cache_archive=None,
extra_env=None,
workdir_files=None,
run_in_workdir=False,
@@ -229,6 +230,11 @@ class ScanImagesWrapperTests(unittest.TestCase):
archive_path.write_bytes(fixture_archive)
api_archive_path = root / "trivy-api-fixture.tar.gz"
api_archive_path.write_bytes(fixture_archive + b"tampered" if api_checksum_mismatch else fixture_archive)
tool_cache = root / "runner-tool-cache"
if cache_archive is not None:
cached_path = tool_cache / "trivy" / "0.70.0" / "trivy_0.70.0_Linux-64bit.tar.gz"
cached_path.parent.mkdir(parents=True)
cached_path.write_bytes(fixture_archive if cache_archive == "valid" else b"invalid archive")
reports = root / "reports"
reports.mkdir()
@@ -261,6 +267,7 @@ class ScanImagesWrapperTests(unittest.TestCase):
env.update(
{
"PATH": f"{bin_dir}:{env['PATH']}",
"RUNNER_TOOL_CACHE": str(tool_cache),
"CRANK_IMAGE_SCAN_OUTPUT_DIR": str(evidence),
"FAKE_CURL_ARCHIVE": str(archive_path),
"FAKE_CURL_API_ARCHIVE": str(api_archive_path),
@@ -493,6 +500,37 @@ class ScanImagesWrapperTests(unittest.TestCase):
self.assertIn("checksum mismatch", result.stderr)
self.assertEqual(trivy_calls, [])
def test_preloaded_pinned_archive_runs_without_network(self):
result, root, evidence, receipt, _, trivy_calls = self.run_scan(
images=(CLEAN_TAG,), cache_archive="valid", curl_fail=True
)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(receipt["overall"]["status"], "pass")
self.assertEqual(
(evidence / "scanner-cache-source.log").read_text(),
"archive_source=runner-tool-cache\n",
)
self.assertFalse((evidence / "scanner-download.log").exists())
self.assertFalse((evidence / "scanner-api-download.log").exists())
cached = root / "runner-tool-cache" / "trivy" / "0.70.0" / "trivy_0.70.0_Linux-64bit.tar.gz"
self.assertEqual(cached.read_bytes(), (root / "trivy-fixture.tar.gz").read_bytes())
self.assertTrue(any("--download-db-only" in call["args"] for call in trivy_calls))
def test_preloaded_archive_hash_mismatch_fails_before_network_or_execution(self):
result, _, evidence, receipt, _, trivy_calls = self.run_scan(
images=(CLEAN_TAG,), cache_archive="bad", curl_fail=True
)
self.assertEqual(result.returncode, 2)
self.assertEqual(receipt["overall"]["status"], "error")
self.assertIn("checksum mismatch", result.stderr)
self.assertEqual(
(evidence / "scanner-cache-source.log").read_text(),
"archive_source=runner-tool-cache\n",
)
self.assertFalse((evidence / "scanner-download.log").exists())
self.assertFalse((evidence / "scanner-api-download.log").exists())
self.assertEqual(trivy_calls, [])
def test_unknown_image_is_error_and_is_never_scanned(self):
missing = "crank/missing:ci"
result, _, _, receipt, _, trivy_calls = self.run_scan(images=(missing, CLEAN_TAG))