|
| 1 | +name: govulncheck |
| 2 | +on: |
| 3 | + merge_group: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: [main] |
| 7 | + pull_request: |
| 8 | + branches: [main] |
| 9 | + schedule: |
| 10 | + # Scheduled runs catch new vulnerability reports published against |
| 11 | + # existing code. PR runs catch new code and dependencies. |
| 12 | + - cron: "22 10 * * *" |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 19 | + cancel-in-progress: true |
| 20 | + |
| 21 | +jobs: |
| 22 | + govulncheck: |
| 23 | + name: govulncheck (${{ matrix.goos }}) |
| 24 | + strategy: |
| 25 | + fail-fast: false |
| 26 | + matrix: |
| 27 | + goos: [linux, windows] |
| 28 | + runs-on: ubuntu-latest |
| 29 | + timeout-minutes: 15 |
| 30 | + permissions: |
| 31 | + actions: read |
| 32 | + contents: read |
| 33 | + security-events: write |
| 34 | + steps: |
| 35 | + - name: Checkout repository |
| 36 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 37 | + # Scan with the toolchain the shipped images build with, so stdlib |
| 38 | + # findings track the builder image rather than the go.mod minimum. |
| 39 | + # The tag pins a minor version, so read the patch version from the |
| 40 | + # image config rather than from the tag. |
| 41 | + - name: Read builder Go version |
| 42 | + id: goversion |
| 43 | + run: | |
| 44 | + image="$(grep -m1 -oP '^FROM .*\Kmcr\.microsoft\.com/oss/go/microsoft/golang:\S+' controller/Dockerfile)" |
| 45 | + version="$(docker buildx imagetools inspect "${image}" --format '{{ json .Image }}' \ |
| 46 | + | jq -r 'first(.. | objects | select(has("Env")) | .Env[] | select(startswith("GOLANG_VERSION=")))' \ |
| 47 | + | cut -d= -f2)" |
| 48 | + # An empty version silently installs the wrong toolchain, so stop here. |
| 49 | + if [ -z "${version}" ]; then |
| 50 | + echo "::error::could not read GOLANG_VERSION from ${image}" |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + echo "builder image ${image} provides Go ${version}" |
| 54 | + echo "version=${version}" >> "${GITHUB_OUTPUT}" |
| 55 | + - name: Setup go |
| 56 | + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 |
| 57 | + with: |
| 58 | + go-version: ${{ steps.goversion.outputs.version }} |
| 59 | + # The tool version is pinned by the go.mod tool block; the scanner |
| 60 | + # fetches the vulnerability database from vuln.go.dev at run time. |
| 61 | + - name: Build govulncheck |
| 62 | + run: go build -o "${RUNNER_TEMP}/govulncheck" golang.org/x/vuln/cmd/govulncheck |
| 63 | + - name: Run govulncheck |
| 64 | + id: scan |
| 65 | + env: |
| 66 | + GOOS: ${{ matrix.goos }} |
| 67 | + run: | |
| 68 | + "${RUNNER_TEMP}/govulncheck" -version |
| 69 | + set -o pipefail |
| 70 | + rc=0 |
| 71 | + "${RUNNER_TEMP}/govulncheck" ./... | tee govulncheck.txt || rc=$? |
| 72 | + # Exit status 3 means reachable findings; anything else non-zero |
| 73 | + # is an operational failure and must not read as a CVE signal. |
| 74 | + case "${rc}" in |
| 75 | + 0) result=clean ;; |
| 76 | + 3) result=findings ;; |
| 77 | + *) result=error ;; |
| 78 | + esac |
| 79 | + echo "result=${result}" >> "${GITHUB_OUTPUT}" |
| 80 | + exit "${rc}" |
| 81 | + - name: Report results |
| 82 | + if: always() |
| 83 | + env: |
| 84 | + GOOS: ${{ matrix.goos }} |
| 85 | + OUTCOME: ${{ steps.scan.outcome }} |
| 86 | + RESULT: ${{ steps.scan.outputs.result }} |
| 87 | + run: | |
| 88 | + { |
| 89 | + echo "## govulncheck (${GOOS})" |
| 90 | + if [ "${RESULT}" = "findings" ]; then |
| 91 | + echo '```' |
| 92 | + cat govulncheck.txt 2>/dev/null || echo "The scan produced no output; see the run log." |
| 93 | + echo '```' |
| 94 | + elif [ "${OUTCOME}" = "success" ]; then |
| 95 | + echo "No reachable vulnerabilities." |
| 96 | + else |
| 97 | + echo "The scan did not complete; see the run log." |
| 98 | + fi |
| 99 | + } >> "${GITHUB_STEP_SUMMARY}" |
| 100 | + if [ "${RESULT}" = "findings" ]; then |
| 101 | + echo "::error::govulncheck found reachable vulnerabilities (GOOS=${GOOS}); see the job summary" |
| 102 | + elif [ "${OUTCOME}" = "failure" ]; then |
| 103 | + echo "::error::the govulncheck scan failed without reporting findings (GOOS=${GOOS}); see the run log" |
| 104 | + fi |
| 105 | + # Code-scanning alerts track main. PR and merge-queue runs skip the |
| 106 | + # SARIF steps; fork PR tokens also lack security-events write. |
| 107 | + - name: Generate SARIF |
| 108 | + if: always() && github.ref == 'refs/heads/main' |
| 109 | + env: |
| 110 | + GOOS: ${{ matrix.goos }} |
| 111 | + run: '"${RUNNER_TEMP}/govulncheck" -format sarif ./... > govulncheck.sarif' |
| 112 | + - name: Upload SARIF to code scanning |
| 113 | + if: always() && github.ref == 'refs/heads/main' |
| 114 | + uses: github/codeql-action/upload-sarif@ff2f1c621b7f889edc0d3c761ac2e6a3f8cdb0dd # v4.37.7 |
| 115 | + with: |
| 116 | + sarif_file: govulncheck.sarif |
| 117 | + category: govulncheck-${{ matrix.goos }} |
0 commit comments