From f0a79362eb4edfcae44f6503f500769c38093846 Mon Sep 17 00:00:00 2001 From: bsodfather Date: Sat, 7 Nov 2026 18:24:08 +0300 Subject: [PATCH] fix(ci): prefer reachable official Trivy database source --- .gitea/workflows/release.yml | 14 +++++++++++++- scripts/scan-images.sh | 4 ++++ tests/unit/test_ci_release_resume.py | 19 +++++++++++++++++++ tests/unit/test_scan_images.py | 12 ++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index ff38332..3ffce56 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -53,6 +53,16 @@ jobs: RELEASE_TAG: ${{ inputs.resume_tag }} RESUME_SOURCE_SHA: ${{ inputs.resume_source_sha }} run: python3 scripts/ci-release-resume.py + - name: Preserve updated image scanner for resume + id: pin_resume_scanner + if: inputs.resume_tag != '' + run: | + set -eu + scanner_dir="$(mktemp -d "${RUNNER_TEMP:?RUNNER_TEMP is unavailable}/crank-release-scanner.XXXXXX")" + cp scripts/scan-images.sh "$scanner_dir/scan-images.sh" + cp scripts/check-image-scan.py "$scanner_dir/check-image-scan.py" + chmod +x "$scanner_dir/scan-images.sh" + printf 'path=%s\n' "$scanner_dir/scan-images.sh" >> "$GITHUB_OUTPUT" - name: Checkout validated release source if: inputs.resume_tag != '' uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 @@ -302,6 +312,8 @@ jobs: docker login '${{ env.REGISTRY }}' -u "$DEPLOY_REGISTRY_USER" --password-stdin - name: Build and push release images + env: + RELEASE_IMAGE_SCANNER: ${{ steps.pin_resume_scanner.outputs.path || 'scripts/scan-images.sh' }} run: | docker build -f apps/admin-api/Dockerfile \ -t '${{ env.ADMIN_API_IMAGE }}:${{ env.IMAGE_TAG }}' \ @@ -338,7 +350,7 @@ jobs: --env-file .tmp/release-migration-smoke.env --profile local-db up -d --wait docker compose -f deploy/community/docker-compose.images.yml \ --env-file .tmp/release-migration-smoke.env --profile local-db logs migrate | grep '"status":"applied"' - scripts/scan-images.sh \ + "$RELEASE_IMAGE_SCANNER" \ '${{ env.ADMIN_API_IMAGE }}:${{ env.IMAGE_TAG }}' \ '${{ env.MCP_SERVER_IMAGE }}:${{ env.IMAGE_TAG }}' \ '${{ env.UI_IMAGE }}:${{ env.IMAGE_TAG }}' diff --git a/scripts/scan-images.sh b/scripts/scan-images.sh index 7ce7170..97dc069 100755 --- a/scripts/scan-images.sh +++ b/scripts/scan-images.sh @@ -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}" +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 @@ -120,6 +122,8 @@ 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" diff --git a/tests/unit/test_ci_release_resume.py b/tests/unit/test_ci_release_resume.py index 67847ec..7125ac1 100644 --- a/tests/unit/test_ci_release_resume.py +++ b/tests/unit/test_ci_release_resume.py @@ -214,6 +214,25 @@ class WorkflowResumeTests(unittest.TestCase): self.assertIn("if: inputs.resume_tag == '' && startsWith(gitea.ref, 'refs/tags/v')", workflow) self.assertIn("if: inputs.resume_tag == ''\n run: |\n ls -lh dist", workflow) + def test_resume_preserves_updated_scanner_before_old_source_checkout(self): + workflow = (ROOT / ".gitea/workflows/release.yml").read_text() + pin = workflow.index("- name: Preserve updated image scanner for resume") + old_checkout = workflow.index("- name: Checkout validated release source") + source_assert = workflow.index("- name: Assert exact release source checkout") + image_build = workflow.index("- name: Build and push release images") + self.assertLess(pin, old_checkout) + self.assertLess(old_checkout, source_assert) + self.assertLess(source_assert, image_build) + pin_step = workflow[pin:old_checkout] + self.assertIn("if: inputs.resume_tag != ''", pin_step) + self.assertIn('cp scripts/scan-images.sh "$scanner_dir/scan-images.sh"', pin_step) + self.assertIn('cp scripts/check-image-scan.py "$scanner_dir/check-image-scan.py"', pin_step) + build_step = workflow[image_build:] + self.assertIn("RELEASE_IMAGE_SCANNER: ${{ steps.pin_resume_scanner.outputs.path || 'scripts/scan-images.sh' }}", build_step) + self.assertIn('"$RELEASE_IMAGE_SCANNER" \\', build_step) + self.assertIn("ref: ${{ steps.resume.outputs.release_tag }}", workflow) + self.assertIn('test "$(git rev-parse HEAD)" = "$EXPECTED_SOURCE_SHA"', workflow) + if __name__ == "__main__": unittest.main() diff --git a/tests/unit/test_scan_images.py b/tests/unit/test_scan_images.py index dec0b33..57e24b5 100644 --- a/tests/unit/test_scan_images.py +++ b/tests/unit/test_scan_images.py @@ -412,6 +412,18 @@ class ScanImagesWrapperTests(unittest.TestCase): self.assertEqual(result.returncode, 2, result.stderr) self.assertEqual(receipt["overall"]["status"], "error") + def test_database_download_uses_official_priority_and_fallback(self): + result, _, _, _, _, trivy_calls = self.run_scan(images=(CLEAN_TAG,)) + self.assertEqual(result.returncode, 0, result.stderr) + downloads = [call["args"] for call in trivy_calls if "--download-db-only" in call["args"]] + self.assertEqual(len(downloads), 1) + args = downloads[0] + repositories = [args[index + 1] for index, item in enumerate(args) if item == "--db-repository"] + self.assertEqual( + repositories, + ["ghcr.io/aquasecurity/trivy-db:2", "mirror.gcr.io/aquasec/trivy-db:2"], + ) + def test_rerun_cannot_reuse_an_old_successful_raw_report(self): result, _, evidence, receipt, _, _ = self.run_scan(images=(CLEAN_TAG,), repeat_without_output=True) self.assertEqual(result.returncode, 2, result.stderr) -- 2.54.0