diff --git a/.gitea/actions/trivy-db-cache/action.yml b/.gitea/actions/trivy-db-cache/action.yml new file mode 100644 index 00000000..835b1b0e --- /dev/null +++ b/.gitea/actions/trivy-db-cache/action.yml @@ -0,0 +1,52 @@ +name: Restore the Trivy databases +description: >- + Restores Trivy's vulnerability database, and with java-db its Java index, from the runner's + cache. A run on main also saves the current copy. + +# scripts/ci/trivy-scan.sh downloaded the vulnerability database (133 MB, about 30 seconds) in every +# scan job, and the image scan also the Java index (952 MB, about two minutes of the 7-minute image +# job, release run 125). Trivy decides from a database's own metadata whether it is current and +# downloads a new one when it is not, so a restored copy changes how long a scan takes, not which +# advisories it knows. +# +# The keys carry a period because an entry is never overwritten: under a fixed key the first copy +# would stay forever. The vulnerability database gets a key a day; the Java index, which Trivy +# itself refreshes every three days, a key every three days, so its 952 MB lands in the runner's +# cache about twice a week instead of daily. Only main saves, the rule the JDK and Gradle caches +# follow (setup-java-gradle): a branch cannot decide what a later main run scans against. +inputs: + java-db: + description: Also restore Trivy's Java index, which only the image scan reads + default: "false" +runs: + using: composite + steps: + - id: period + shell: bash + run: | + echo "day=$(date -u +%Y-%m-%d)" >> "${GITHUB_OUTPUT}" + echo "three-days=$(( $(date -u +%s) / 259200 ))" >> "${GITHUB_OUTPUT}" + - if: ${{ github.ref == 'refs/heads/main' }} + uses: https://github.com/actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # actions/cache@v4.3.0 + with: + path: ~/.cache/trivy/db + key: trivy-db-${{ runner.os }}-${{ steps.period.outputs.day }} + restore-keys: trivy-db-${{ runner.os }}- + - if: ${{ github.ref != 'refs/heads/main' }} + uses: https://github.com/actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # actions/cache/restore@v4.3.0 + with: + path: ~/.cache/trivy/db + key: trivy-db-${{ runner.os }}-${{ steps.period.outputs.day }} + restore-keys: trivy-db-${{ runner.os }}- + - if: ${{ inputs.java-db == 'true' && github.ref == 'refs/heads/main' }} + uses: https://github.com/actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # actions/cache@v4.3.0 + with: + path: ~/.cache/trivy/java-db + key: trivy-java-db-${{ runner.os }}-${{ steps.period.outputs.three-days }} + restore-keys: trivy-java-db-${{ runner.os }}- + - if: ${{ inputs.java-db == 'true' && github.ref != 'refs/heads/main' }} + uses: https://github.com/actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # actions/cache/restore@v4.3.0 + with: + path: ~/.cache/trivy/java-db + key: trivy-java-db-${{ runner.os }}-${{ steps.period.outputs.three-days }} + restore-keys: trivy-java-db-${{ runner.os }}- diff --git a/.gitea/workflows/main.yml b/.gitea/workflows/main.yml index 5311e09a..fbd1638b 100644 --- a/.gitea/workflows/main.yml +++ b/.gitea/workflows/main.yml @@ -109,6 +109,7 @@ jobs: timeout-minutes: 20 steps: - uses: https://github.com/actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # actions/checkout@v4.2.2 + - uses: ./.gitea/actions/trivy-db-cache - run: bash scripts/ci/trivy-scan.sh filesystem # Experimental by design: the result is reported, never used to block anything. diff --git a/.gitea/workflows/pr.yml b/.gitea/workflows/pr.yml index fa082b97..fdf4ee97 100644 --- a/.gitea/workflows/pr.yml +++ b/.gitea/workflows/pr.yml @@ -42,5 +42,6 @@ jobs: - run: ./gradlew :prCheck # Plans every stage (--dry-run: nothing executes), so it cannot be a dependency of prCheck. - run: ./gradlew :verifyStrictLaneStages --dry-run + - uses: ./.gitea/actions/trivy-db-cache # Known High and Critical vulnerabilities and CISA KEV matches in the dependency tree. - run: bash scripts/ci/trivy-scan.sh filesystem diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 2b178cfc..bd306484 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -197,6 +197,10 @@ jobs: . # The SBOM is printed and kept in the log before the blocking scan, so the inventory of an image # that is refused still exists. A vulnerable image is never pushed: the push comes after. + # The image scan also reads Trivy's Java index (952 MB), the bulk of this job's download time. + - uses: ./.gitea/actions/trivy-db-cache + with: + java-db: "true" - name: Inventory and scan the release image run: | set -euo pipefail diff --git a/scripts/ci/trivy-scan.sh b/scripts/ci/trivy-scan.sh index b98b0bf9..92ae8459 100755 --- a/scripts/ci/trivy-scan.sh +++ b/scripts/ci/trivy-scan.sh @@ -15,7 +15,9 @@ # temporary directory): a checksum is a supply-chain claim that can be checked offline, an action # pinned to a commit is not. .trivyignore.yaml is the one suppression source; each entry carries a # reason and an expiry by policy (.github/dependency-vulnerability-policy.md). -# Optional mirrors: TRIVY_DOWNLOAD_BASE_URL, JQ_DOWNLOAD_BASE_URL, KEV_FEED_URL. +# Optional mirrors: TRIVY_DOWNLOAD_BASE_URL, JQ_DOWNLOAD_BASE_URL, KEV_FEED_URL. The workflows restore +# Trivy's databases into its default cache directory first (.gitea/actions/trivy-db-cache); Trivy +# still downloads a newer one when the restored copy's own metadata says it is out of date. set -euo pipefail readonly TRIVY_VERSION='0.71.2'