-
Notifications
You must be signed in to change notification settings - Fork 3
ci: add Trivy security scan to PR pipeline #1634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
yaroslavmokflmg
wants to merge
19
commits into
main
Choose a base branch
from
security/trivy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−2
Draft
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a68f695
ci: add Trivy security scan to PR pipeline
yaroslavmokflmg c02de14
ci: harden trivy gate — no persisted git credentials, visible warning…
yaroslavmokflmg 1a8b5cc
ci: make trivy gate non-blocking (allow fail) until devs have capacity
yaroslavmokflmg 3b5cf43
ci: add Trivy security scan to PR pipeline
yaroslavmokflmg 761f158
ci: harden trivy gate — no persisted git credentials, visible warning…
yaroslavmokflmg 7071174
ci: make trivy gate non-blocking (allow fail) until devs have capacity
yaroslavmokflmg f2b4238
ci: trivy code+docker scan in one job, in-build image scan, per-scan …
yaroslavmokflmg a7bcb07
Merge branch 'security/trivy' of https://github.com/flamingo-stack/op…
yaroslavmokflmg 89ce205
ci: merge all trivy reports into one artifact per run, skip empty rep…
yaroslavmokflmg 4333c55
ci: merge all trivy reports into one artifact per run, dedupe upload …
yaroslavmokflmg 1fbc99f
ci: compact trivy action — piped scans, unified evaluate, single merg…
yaroslavmokflmg adf92ad
ci: report unique vulnerability count in trivy error message
yaroslavmokflmg fc5cf7f
ci: rename scan_code/report jobs, split scan_code and scan_image repo…
yaroslavmokflmg d7ed1ac
Merge remote-tracking branch 'origin/main' into security/trivy
yaroslavmokflmg b8b1e8f
ci: rename scan steps — Scan code / Scan image, action Trivy -> Scan
yaroslavmokflmg 8517b05
ci: rename scan steps — Scan code / Scan image, action name Vulnerabi…
yaroslavmokflmg 3feb686
ci: point error message to scan_code/scan_image artifact
yaroslavmokflmg 9022129
ci: per-service code scan matrix with root coverage, merge artifacts …
yaroslavmokflmg 7765f58
ci: strip trailing whitespace in test.yml
yaroslavmokflmg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| name: "Vulnerability Scan" | ||
| description: "Trivy PR security scans. scan: code = repo dependencies (HIGH/CRITICAL) + Dockerfile base images (CRITICAL), image = image built in the pipeline (HIGH/CRITICAL). Each scan uploads its TSV report as an artifact" | ||
|
|
||
| inputs: | ||
| scan: | ||
| description: "code | image" | ||
| required: true | ||
| maven-token: | ||
| description: "Token for internal GitHub Packages: Maven resolution (scan: code) and docker build secret (scan: image)" | ||
| required: false | ||
| default: ${{ github.token }} | ||
| image-name: | ||
| description: "Service name from the matrix; used in the artifact name" | ||
| required: false | ||
| path: | ||
| description: "Directory to scan (scan: code), e.g. a single service; defaults to the whole repo" | ||
| required: false | ||
| default: "." | ||
| skip-dirs: | ||
| description: "Comma-separated directories to exclude from the code scan (e.g. service dirs already scanned by the matrix)" | ||
| required: false | ||
| default: "" | ||
| dockerfile: | ||
| description: "Path to the service Dockerfile (scan: image)" | ||
| required: false | ||
| context: | ||
| description: "Docker build context (scan: image)" | ||
| required: false | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Install Trivy | ||
| uses: aquasecurity/setup-trivy@v0.3.1 | ||
| with: | ||
| version: v0.73.0 | ||
| cache: true | ||
|
|
||
| - name: Scan dependencies and Dockerfile base images | ||
| if: inputs.scan == 'code' | ||
| shell: bash | ||
| env: | ||
| GITHUB_TOKEN: ${{ inputs.maven-token }} | ||
| GITHUB_ACTOR: ${{ github.actor }} | ||
| run: | | ||
| set -euo pipefail | ||
| dir="${{ inputs.path }}" | ||
| if [ -f "$dir/pom.xml" ]; then # warm ~/.m2 via Google's Central mirror so trivy sees parent-managed versions | ||
| echo '<settings><mirrors><mirror><id>google-central</id><url>https://maven-central.storage-download.googleapis.com/maven2/</url><mirrorOf>central</mirrorOf></mirror></mirrors></settings>' > /tmp/mirror.xml | ||
| mvn -B -q -fn -DskipTests -gs /tmp/mirror.xml -f "$dir/pom.xml" $([ -f .mvn/settings.xml ] && echo '-s .mvn/settings.xml') dependency:go-offline | tee /tmp/mvn.log || true | ||
| ! grep -q '\[ERROR\]' /tmp/mvn.log || echo "::warning::Some Maven dependencies could not be resolved; scan depth may be reduced" | ||
| fi | ||
| skip="${{ inputs.skip-dirs }}" | ||
| targs=(--no-progress --scanners vuln --severity HIGH,CRITICAL --ignore-unfixed --offline-scan --format json) | ||
| [ -n "$skip" ] && targs+=(--skip-dirs "$skip") | ||
| trivy fs "${targs[@]}" "$dir" | | ||
| jq -r '.Results[]? as $r | $r.Vulnerabilities[]? | [.Severity, .PkgName, .InstalledVersion, (.FixedVersion // "-"), .VulnerabilityID, $r.Target] | @tsv' | sort -u > "trivy-${GITHUB_REPOSITORY##*/}-code-report.tsv" | ||
|
|
||
| out="trivy-${GITHUB_REPOSITORY##*/}-docker-report.tsv"; : > "$out" | ||
| fargs=(-name 'Dockerfile*' -not -path '*/.git/*' -not -path '*/node_modules/*') | ||
| [ -n "$skip" ] && for d in ${skip//,/ }; do fargs+=(-not -path "*/${d}/*" -not -path "${d}/*"); done | ||
| find "$dir" "${fargs[@]}" -print0 | | ||
| xargs -0 -r awk 'toupper($1)=="FROM" {i=$2; if (i ~ /^--/) i=$3; print i; if (toupper($3)=="AS") print "~"$4; if (toupper($4)=="AS") print "~"$5}' | sort -u > /tmp/from.txt | ||
| for img in $(grep -v '^~' /tmp/from.txt); do | ||
| [ "$img" = scratch ] && continue | ||
| case "$img" in *'$'*) echo "::warning::Skipping base image with unresolved variable: ${img}"; continue ;; esac | ||
| grep -qxF "~$img" /tmp/from.txt && continue | ||
| trivy image --no-progress --scanners vuln --severity CRITICAL --ignore-unfixed --format json "$img" | | ||
| jq -r --arg img "$img" '.Results[]? as $r | $r.Vulnerabilities[]? | [.Severity, .PkgName, .InstalledVersion, (.FixedVersion // "-"), .VulnerabilityID, $img] | @tsv' >> "$out" || | ||
| echo "::warning::Base image ${img} could not be scanned (pull/scan error); it was NOT checked" | ||
| done | ||
| sort -u "$out" -o "$out" | ||
| find . -maxdepth 1 -name 'trivy-*.tsv' -empty -delete | ||
|
|
||
| - name: Scan built image | ||
| if: inputs.scan == 'image' | ||
| shell: bash | ||
| env: | ||
| GITHUB_TOKEN: ${{ inputs.maven-token }} | ||
| GITHUB_ACTOR: ${{ github.actor }} | ||
| run: | | ||
| set -euo pipefail | ||
| docker buildx build --platform linux/amd64 --secret id=GITHUB_TOKEN,env=GITHUB_TOKEN --build-arg GITHUB_ACTOR="$GITHUB_ACTOR" \ | ||
| -f "${{ inputs.dockerfile }}" --output type=oci,dest=/tmp/image.tar "${{ inputs.context }}" | ||
| out="trivy-${GITHUB_REPOSITORY##*/}-image-report-${{ inputs.image-name }}.tsv" | ||
| trivy image --no-progress --scanners vuln --severity HIGH,CRITICAL --ignore-unfixed --input /tmp/image.tar --format json | | ||
| jq -r '.Results[]? as $r | $r.Vulnerabilities[]? | [.Severity, .PkgName, .InstalledVersion, (.FixedVersion // "-"), .VulnerabilityID, $r.Target] | @tsv' | sort -u > "$out" | ||
| find . -maxdepth 1 -name 'trivy-*.tsv' -empty -delete | ||
|
|
||
| - name: Upload report | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: ${{ inputs.scan == 'code' && (inputs.image-name && format('scan_code-{0}', inputs.image-name) || 'scan_code') || format('scan_image-{0}', inputs.image-name) }} | ||
| path: trivy-*.tsv | ||
| if-no-files-found: ignore | ||
|
|
||
| - name: Evaluate | ||
| shell: bash | ||
| env: | ||
| ARTIFACT: ${{ inputs.scan == 'code' && 'scan_code' || 'scan_image' }} | ||
| run: | | ||
| set -euo pipefail | ||
| files=$(ls trivy-*.tsv 2>/dev/null) || { echo "No vulnerabilities found."; exit 0; } | ||
| column -t -s "$(printf '\t')" $files | ||
| echo "::error::Trivy found $(cut -f1,2,5 $files | sort -u | wc -l | tr -d ' ') unique HIGH/CRITICAL vulnerabilities ($(cat $files | wc -l | tr -d ' ') occurrences across modules) — full report in the ${ARTIFACT} artifact" | ||
| exit 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Vulnerabilities to skip in the Trivy scan: one CVE/GHSA id per line, e.g.: | ||
| # | ||
| # CVE-2026-12345 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.