Skip to content

Commit 1d04ae8

Browse files
authored
Make govulncheck run more reliable (#6021)
* Make govulncheck run more reliable * Update go version to 1.23.7 (#6003) * Update go version to 1.23.7 * Update version for GH workflows that had " or ' delimiters * Use GO_VERSION pattern on gitlab-ci too * make gendependabot * Add required options and log versions * Renames and clean up * Describe why not using the full package name
1 parent 288a495 commit 1d04ae8

File tree

4 files changed

+104
-6
lines changed

4 files changed

+104
-6
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
mkdir -p ./govulncheck 2>/dev/null
6+
7+
# Get all package directories
8+
ALL_PKG_DIRS=$(go list ./...)
9+
10+
# Initialize failure flag
11+
FAILED=0
12+
13+
# Repository prefix to remove from package names
14+
REPO_PREFIX=$(go list -m)
15+
16+
# Run govulncheck for each package
17+
for pkg in $ALL_PKG_DIRS; do
18+
# Remove the repository prefix from the package name to keep the category names short
19+
# and replace slashes with underscores to make clear that the categories are not nested.
20+
OUTPUT_FILE="./govulncheck/$(echo "$pkg" | sed "s|^$REPO_PREFIX/||" | tr '/' '_').sarif"
21+
echo -e "\nRunning govulncheck for package $pkg"
22+
if ! govulncheck ${GOVULN_OPTS:-} "$pkg" > "$OUTPUT_FILE"; then
23+
echo "govulncheck failed for package $pkg, output saved to $OUTPUT_FILE"
24+
FAILED=1
25+
else
26+
echo "govulncheck succeeded for package $pkg, output saved to $OUTPUT_FILE"
27+
fi
28+
done
29+
30+
if [ $FAILED -ne 0 ]; then
31+
echo -e "\ngovulncheck failed for one or more packages"
32+
exit 1
33+
fi
34+
35+
echo -e "\ngovulncheck completed successfully for all packages"

.github/workflows/vuln-scans.yml

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ jobs:
267267
with:
268268
sarif_file: snyk.sarif
269269

270-
govulncheck:
270+
govulncheck-run:
271271
runs-on: ubuntu-24.04
272272
timeout-minutes: 30
273273
steps:
@@ -278,11 +278,63 @@ jobs:
278278
with:
279279
go-version: ${{ env.GO_VERSION }}
280280
cache-dependency-path: '**/go.sum'
281+
281282
- name: Install Tools
282283
run: make install-tools
283-
- name: Run `govulncheck`
284-
run: govulncheck -format sarif ./... > govulncheck.sarif
284+
285+
- run: govulncheck --version
286+
287+
- name: Run `govulncheck` script
288+
env:
289+
GOVULN_OPTS: --format sarif --scan package
290+
run: ./.github/workflows/scripts/govulncheck-run.sh
291+
292+
- name: Save govulncheck results as artifact
293+
uses: actions/upload-artifact@v4
294+
with:
295+
name: govulncheck-results
296+
path: ./govulncheck/
297+
298+
govulncheck-categories:
299+
runs-on: ubuntu-24.04
300+
outputs:
301+
matrix: ${{ steps.capture-packages.outputs.matrix }}
302+
steps:
303+
- name: Checkout Repo
304+
uses: actions/checkout@v4
305+
- name: Setup Go
306+
uses: actions/setup-go@v5
307+
with:
308+
go-version: ${{ env.GO_VERSION }}
309+
cache-dependency-path: '**/go.sum'
310+
- name: Capture Go Packages
311+
id: capture-packages
312+
run: |
313+
repoPrefix=$(go list -m)
314+
packages=$(go list ./... | sed "s|^$repoPrefix/||" | tr '/' '_')
315+
category=$(for p in $(echo -e "$packages"); do echo "\"$p\","; done)
316+
matrix=$(echo "{\"category\": [${category%,}]}" | tr -d '\n')
317+
echo "$matrix" | jq
318+
echo "matrix=${matrix}" >> $GITHUB_OUTPUT
319+
320+
govulncheck-upload:
321+
runs-on: ubuntu-24.04
322+
needs: [govulncheck-run, govulncheck-categories]
323+
strategy:
324+
matrix: ${{ fromJSON(needs.govulncheck-categories.outputs.matrix) }}
325+
steps:
326+
- name: Checkout Repo
327+
uses: actions/checkout@v4
328+
329+
- name: Download govulncheck results artifact
330+
uses: actions/download-artifact@v4
331+
with:
332+
name: govulncheck-results
333+
path: ./govulncheck/
334+
285335
- name: Upload result to GitHub Code Scanning
336+
if: always()
286337
uses: github/codeql-action/upload-sarif@v3
287338
with:
288-
sarif_file: govulncheck.sarif
339+
sarif_file: ./govulncheck/${{ matrix.category }}.sarif
340+
category: ${{ matrix.category }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@ deployments/heroku/test/node_modules
5555

5656
# temp file created during testing
5757
tests/installation/testdata/systemd/splunk-otel-collector.conf
58+
59+
# For convenience excluding sarif files generated by ./.github/workflows/scripts/govulncheck-run.sh
60+
/govulncheck/

Makefile.Common

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,13 @@ moddownload:
136136
$(GOCMD) mod download
137137

138138
.PHONY: govulncheck
139-
govulncheck: install-tools
140-
govulncheck ./...
139+
govulncheck:
140+
@FAILED=0; \
141+
@for pkg in $(shell $(GOCMD) list $(ALL_PKG_DIRS)); do \
142+
echo "\nRunning govulncheck for package $$pkg\n"; \
143+
govulncheck $${GOVULN_OPTS} $$pkg || FAILED=1; \
144+
done; \
145+
@if [ $$FAILED -ne 0 ]; then \
146+
echo "\ngovulncheck failed for one or more packages"; \
147+
exit 1; \
148+
fi

0 commit comments

Comments
 (0)