fix(ci): download pinned Trivy asset through GitHub API on HTTP failure #117
No files matched your search
+11
-1
@@ -45,6 +45,8 @@ 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"
|
||||
db_repository_primary="ghcr.io/aquasecurity/trivy-db:2"
|
||||
db_repository_fallback="mirror.gcr.io/aquasec/trivy-db:2"
|
||||
|
||||
@@ -106,7 +108,15 @@ PY
|
||||
if ! curl --fail --silent --show-error --location --max-time 300 \
|
||||
"$download_url" --output "$temp_dir/$archive" \
|
||||
>"$output_dir/scanner-download.log" 2>&1; then
|
||||
fail_all "could not download pinned Trivy ${trivy_version}"
|
||||
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
|
||||
fail_all "could not download pinned Trivy ${trivy_version} from release URL or GitHub asset API"
|
||||
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
|
||||
|
||||
@@ -27,9 +27,18 @@ import os
|
||||
import sys
|
||||
|
||||
args = sys.argv[1:]
|
||||
if os.environ.get("FAKE_CURL_FAIL") == "1":
|
||||
sys.stderr.write("curl: (7) failed to connect\\n")
|
||||
url = next((value for value in args if value.startswith("https://")), "")
|
||||
api_download = url == "https://api.github.com/repos/aquasecurity/trivy/releases/assets/398381981"
|
||||
failed = os.environ.get("FAKE_CURL_FAIL") == "1"
|
||||
failed = failed or (not api_download and os.environ.get("FAKE_CURL_DIRECT_FAIL") == "1")
|
||||
failed = failed or (api_download and os.environ.get("FAKE_CURL_API_FAIL") == "1")
|
||||
if failed:
|
||||
sys.stderr.write("curl: (22) fixture download failure for " + url + "\\n")
|
||||
sys.exit(7)
|
||||
if api_download:
|
||||
if "--header" not in args or args[args.index("--header") + 1] != "Accept: application/octet-stream":
|
||||
sys.stderr.write("curl: missing octet-stream Accept header\\n")
|
||||
sys.exit(2)
|
||||
output = None
|
||||
for index, value in enumerate(args):
|
||||
if value in ("--output", "-o"):
|
||||
@@ -37,7 +46,8 @@ for index, value in enumerate(args):
|
||||
if output is None:
|
||||
sys.stderr.write("curl: missing --output\\n")
|
||||
sys.exit(2)
|
||||
with open(os.environ["FAKE_CURL_ARCHIVE"], "rb") as source:
|
||||
archive_path = os.environ.get("FAKE_CURL_API_ARCHIVE", os.environ["FAKE_CURL_ARCHIVE"]) if api_download else os.environ["FAKE_CURL_ARCHIVE"]
|
||||
with open(archive_path, "rb") as source:
|
||||
payload = source.read()
|
||||
with open(output, "wb") as target:
|
||||
target.write(payload)
|
||||
@@ -176,6 +186,9 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
images=(VULN_TAG, CLEAN_TAG),
|
||||
trivy_fail_id=None,
|
||||
curl_fail=False,
|
||||
curl_direct_fail=False,
|
||||
curl_api_fail=False,
|
||||
api_checksum_mismatch=False,
|
||||
extra_env=None,
|
||||
workdir_files=None,
|
||||
run_in_workdir=False,
|
||||
@@ -214,6 +227,8 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
|
||||
archive_path = root / "trivy-fixture.tar.gz"
|
||||
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)
|
||||
|
||||
reports = root / "reports"
|
||||
reports.mkdir()
|
||||
@@ -248,6 +263,7 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
"PATH": f"{bin_dir}:{env['PATH']}",
|
||||
"CRANK_IMAGE_SCAN_OUTPUT_DIR": str(evidence),
|
||||
"FAKE_CURL_ARCHIVE": str(archive_path),
|
||||
"FAKE_CURL_API_ARCHIVE": str(api_archive_path),
|
||||
"FAKE_IMAGE_MAP": str(image_map),
|
||||
"FAKE_REPORT_DIR": str(reports),
|
||||
"FAKE_DOCKER_LOG": str(docker_log),
|
||||
@@ -258,6 +274,10 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
env["FAKE_TRIVY_FAIL_ID"] = trivy_fail_id
|
||||
if curl_fail:
|
||||
env["FAKE_CURL_FAIL"] = "1"
|
||||
if curl_direct_fail:
|
||||
env["FAKE_CURL_DIRECT_FAIL"] = "1"
|
||||
if curl_api_fail:
|
||||
env["FAKE_CURL_API_FAIL"] = "1"
|
||||
if extra_env:
|
||||
env.update(extra_env)
|
||||
|
||||
@@ -442,6 +462,37 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
self.assertEqual(docker_calls, [])
|
||||
self.assertEqual(trivy_calls, [])
|
||||
|
||||
def test_pinned_asset_api_fallback_downloads_and_verifies_same_archive(self):
|
||||
result, _, evidence, receipt, _, trivy_calls = self.run_scan(
|
||||
images=(CLEAN_TAG,), curl_direct_fail=True
|
||||
)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertEqual(receipt["overall"]["status"], "pass")
|
||||
self.assertIn("fixture download failure", (evidence / "scanner-download.log").read_text())
|
||||
self.assertIn("GitHub release asset API", (evidence / "scanner-api-download.log").read_text())
|
||||
self.assertTrue(any("--download-db-only" in call["args"] for call in trivy_calls))
|
||||
scanner = json.loads((evidence / "scanner.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(scanner["expected_version"], "0.70.0")
|
||||
|
||||
def test_pinned_asset_api_fallback_failure_fails_closed_with_both_logs(self):
|
||||
result, _, evidence, receipt, _, trivy_calls = self.run_scan(
|
||||
images=(CLEAN_TAG,), curl_direct_fail=True, curl_api_fail=True
|
||||
)
|
||||
self.assertEqual(result.returncode, 2)
|
||||
self.assertEqual(receipt["overall"]["status"], "error")
|
||||
self.assertIn("fixture download failure", (evidence / "scanner-download.log").read_text())
|
||||
self.assertIn("fixture download failure", (evidence / "scanner-api-download.log").read_text())
|
||||
self.assertEqual(trivy_calls, [])
|
||||
|
||||
def test_pinned_asset_api_fallback_checksum_mismatch_never_executes_trivy(self):
|
||||
result, _, evidence, receipt, _, trivy_calls = self.run_scan(
|
||||
images=(CLEAN_TAG,), curl_direct_fail=True, api_checksum_mismatch=True
|
||||
)
|
||||
self.assertEqual(result.returncode, 2)
|
||||
self.assertEqual(receipt["overall"]["status"], "error")
|
||||
self.assertIn("checksum mismatch", result.stderr)
|
||||
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))
|
||||
@@ -457,7 +508,8 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
result, _, evidence, receipt, _, _ = self.run_scan(curl_fail=True)
|
||||
self.assertEqual(result.returncode, 2, result.stderr)
|
||||
self.assertEqual(receipt["overall"]["status"], "error")
|
||||
self.assertIn("failed to connect", (evidence / "scanner-download.log").read_text())
|
||||
self.assertIn("fixture download failure", (evidence / "scanner-download.log").read_text())
|
||||
self.assertIn("fixture download failure", (evidence / "scanner-api-download.log").read_text())
|
||||
|
||||
def test_evidence_kept_even_when_gate_is_red(self):
|
||||
_, _, evidence, receipt, _, _ = self.run_scan()
|
||||
|
||||
Reference in new issue
Block a user