|
1 | 1 | name: Trivy Vulnerability Scanner |
2 | 2 | on: |
| 3 | + schedule: |
| 4 | + - cron: '0 6 * * *' # Daily at 6:00 AM UTC |
3 | 5 | push: |
4 | 6 | branches: |
5 | 7 | - main |
|
11 | 13 | permissions: |
12 | 14 | contents: read |
13 | 15 | packages: write |
| 16 | + issues: write |
14 | 17 |
|
15 | 18 | env: |
16 | 19 | REGISTRY: ghcr.io |
|
22 | 25 |
|
23 | 26 | jobs: |
24 | 27 | export-registry: |
25 | | - runs-on: ubuntu-latest #Latest tag points to the latest LTS release of Ubuntu per docker hub |
| 28 | + runs-on: ubuntu-latest |
26 | 29 | outputs: |
27 | 30 | registry: ${{ steps.export.outputs.registry }} |
28 | 31 | steps: |
29 | 32 | - id: export |
30 | 33 | run: | |
31 | 34 | # registry must be in lowercase |
32 | 35 | # store the images under dev |
33 | | - # TODO: need to cleanup dev images periodically |
| 36 | + # TODO: need to cleanup dev images periodically |
34 | 37 | echo "registry=$(echo "${{ env.REGISTRY }}/${{ github.repository }}" | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT" |
35 | 38 | scan-images: |
36 | 39 | needs: export-registry |
37 | 40 | env: |
38 | 41 | REGISTRY: ${{ needs.export-registry.outputs.registry }} |
39 | | - runs-on: ubuntu-latest #Latest tag points to the latest LTS release of Ubuntu per docker hub |
| 42 | + runs-on: ubuntu-latest |
40 | 43 | steps: |
41 | 44 | - name: Set up Go ${{ env.GO_VERSION }} |
42 | 45 | uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6 |
@@ -67,44 +70,133 @@ jobs: |
67 | 70 | uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 |
68 | 71 | with: |
69 | 72 | image-ref: ${{ env.REGISTRY }}/${{ env.HUB_AGENT_IMAGE_NAME }}:${{ env.IMAGE_VERSION }} |
70 | | - format: 'table' |
71 | | - exit-code: '1' |
| 73 | + format: 'json' |
| 74 | + output: 'trivy-hub-agent.json' |
72 | 75 | ignore-unfixed: true |
73 | 76 | vuln-type: 'os,library' |
74 | 77 | severity: 'CRITICAL,HIGH' |
75 | 78 | timeout: '5m0s' |
76 | 79 | env: |
77 | 80 | TRIVY_USERNAME: ${{ github.actor }} |
78 | 81 | TRIVY_PASSWORD: ${{ secrets.GITHUB_TOKEN }} |
79 | | - TRIVY_DB_REPOSITORY: mcr.microsoft.com/mirror/ghcr/aquasecurity/trivy-db |
80 | | - |
| 82 | + TRIVY_DB_REPOSITORY: mcr.microsoft.com/mirror/ghcr/aquasecurity/trivy-db |
81 | 83 |
|
82 | 84 | - name: Scan ${{ env.REGISTRY }}/${{ env.MEMBER_AGENT_IMAGE_NAME }}:${{ env.IMAGE_VERSION }} |
83 | 85 | uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 |
84 | 86 | with: |
85 | 87 | image-ref: ${{ env.REGISTRY }}/${{ env.MEMBER_AGENT_IMAGE_NAME }}:${{ env.IMAGE_VERSION }} |
86 | | - format: 'table' |
87 | | - exit-code: '1' |
| 88 | + format: 'json' |
| 89 | + output: 'trivy-member-agent.json' |
88 | 90 | ignore-unfixed: true |
89 | 91 | vuln-type: 'os,library' |
90 | 92 | severity: 'CRITICAL,HIGH' |
91 | 93 | timeout: '5m0s' |
92 | 94 | env: |
93 | 95 | TRIVY_USERNAME: ${{ github.actor }} |
94 | 96 | TRIVY_PASSWORD: ${{ secrets.GITHUB_TOKEN }} |
95 | | - TRIVY_DB_REPOSITORY: mcr.microsoft.com/mirror/ghcr/aquasecurity/trivy-db |
| 97 | + TRIVY_DB_REPOSITORY: mcr.microsoft.com/mirror/ghcr/aquasecurity/trivy-db |
96 | 98 |
|
97 | 99 | - name: Scan ${{ env.REGISTRY }}/${{ env.REFRESH_TOKEN_IMAGE_NAME }}:${{ env.IMAGE_VERSION }} |
98 | 100 | uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 |
99 | 101 | with: |
100 | 102 | image-ref: ${{ env.REGISTRY }}/${{ env.REFRESH_TOKEN_IMAGE_NAME }}:${{ env.IMAGE_VERSION }} |
101 | | - format: 'table' |
102 | | - exit-code: '1' |
| 103 | + format: 'json' |
| 104 | + output: 'trivy-refresh-token.json' |
103 | 105 | ignore-unfixed: true |
104 | 106 | vuln-type: 'os,library' |
105 | 107 | severity: 'CRITICAL,HIGH' |
106 | 108 | timeout: '5m0s' |
107 | 109 | env: |
108 | 110 | TRIVY_USERNAME: ${{ github.actor }} |
109 | 111 | TRIVY_PASSWORD: ${{ secrets.GITHUB_TOKEN }} |
110 | | - TRIVY_DB_REPOSITORY: mcr.microsoft.com/mirror/ghcr/aquasecurity/trivy-db |
| 112 | + TRIVY_DB_REPOSITORY: mcr.microsoft.com/mirror/ghcr/aquasecurity/trivy-db |
| 113 | + |
| 114 | + - name: Check for vulnerabilities |
| 115 | + id: check-vulns |
| 116 | + run: | |
| 117 | + has_vulns=false |
| 118 | + for file in trivy-hub-agent.json trivy-member-agent.json trivy-refresh-token.json; do |
| 119 | + count=$(jq '[.Results[]? | .Vulnerabilities[]?] | length' "$file") |
| 120 | + if [ "$count" -gt 0 ]; then |
| 121 | + has_vulns=true |
| 122 | + break |
| 123 | + fi |
| 124 | + done |
| 125 | + echo "has_vulns=$has_vulns" >> "$GITHUB_OUTPUT" |
| 126 | +
|
| 127 | + - name: Fail on vulnerabilities (non-scheduled runs) |
| 128 | + if: steps.check-vulns.outputs.has_vulns == 'true' && github.event_name != 'schedule' |
| 129 | + run: | |
| 130 | + echo "::error::Vulnerabilities found. See trivy scan output." |
| 131 | + for file in trivy-hub-agent.json trivy-member-agent.json trivy-refresh-token.json; do |
| 132 | + echo "--- $file ---" |
| 133 | + jq -r '.Results[]? | .Vulnerabilities[]? | "\(.VulnerabilityID) \(.Severity) \(.PkgName) \(.InstalledVersion) -> \(.FixedVersion)"' "$file" |
| 134 | + done |
| 135 | + exit 1 |
| 136 | +
|
| 137 | + - name: Build vulnerability summary |
| 138 | + if: steps.check-vulns.outputs.has_vulns == 'true' && github.event_name == 'schedule' |
| 139 | + id: vuln-summary |
| 140 | + run: | |
| 141 | + { |
| 142 | + echo 'body<<EOF' |
| 143 | + echo "## Trivy CVE Scan - $(date -u +%Y-%m-%d)" |
| 144 | + echo "" |
| 145 | + echo "The daily vulnerability scan found **HIGH/CRITICAL** CVEs in our container images." |
| 146 | + echo "Please update the affected Go dependencies in \`go.mod\` to the fixed versions listed below," |
| 147 | + echo "then run \`go mod tidy\` and verify the build passes with \`make build\`." |
| 148 | + echo "" |
| 149 | + echo "### Vulnerabilities" |
| 150 | + echo "" |
| 151 | + echo "| CVE | Severity | Package | Installed | Fixed | Image |" |
| 152 | + echo "|-----|----------|---------|-----------|-------|-------|" |
| 153 | + for file in trivy-hub-agent.json trivy-member-agent.json trivy-refresh-token.json; do |
| 154 | + image=$(echo "$file" | sed 's/trivy-//;s/.json//') |
| 155 | + jq -r --arg img "$image" \ |
| 156 | + '.Results[]? | .Vulnerabilities[]? | "| \(.VulnerabilityID) | \(.Severity) | \(.PkgName) | \(.InstalledVersion) | \(.FixedVersion) | \($img) |"' \ |
| 157 | + "$file" |
| 158 | + done | sort -u |
| 159 | + echo "" |
| 160 | + echo "### Instructions" |
| 161 | + echo "" |
| 162 | + echo "1. For each Go library CVE, run: \`go get <package>@<fixed_version>\`" |
| 163 | + echo "2. Run \`go mod tidy\` to clean up dependencies." |
| 164 | + echo "3. Run \`make build\` to verify the build passes." |
| 165 | + echo "4. Run \`make test\` to verify tests pass." |
| 166 | + echo 'EOF' |
| 167 | + } >> "$GITHUB_OUTPUT" |
| 168 | +
|
| 169 | + - name: Create issue for Copilot |
| 170 | + if: steps.check-vulns.outputs.has_vulns == 'true' && github.event_name == 'schedule' |
| 171 | + uses: actions/github-script@v7 |
| 172 | + with: |
| 173 | + script: | |
| 174 | + const today = new Date().toISOString().split('T')[0]; |
| 175 | + const title = `fix: address trivy CVEs found on ${today}`; |
| 176 | +
|
| 177 | + // Check if an open issue already exists for today |
| 178 | + const existing = await github.rest.issues.listForRepo({ |
| 179 | + owner: context.repo.owner, |
| 180 | + repo: context.repo.repo, |
| 181 | + state: 'open', |
| 182 | + labels: 'security,trivy', |
| 183 | + per_page: 10 |
| 184 | + }); |
| 185 | + const alreadyExists = existing.data.some(i => i.title === title); |
| 186 | + if (alreadyExists) { |
| 187 | + console.log('Issue already exists for today, skipping.'); |
| 188 | + return; |
| 189 | + } |
| 190 | +
|
| 191 | + const body = process.env.ISSUE_BODY; |
| 192 | + await github.rest.issues.create({ |
| 193 | + owner: context.repo.owner, |
| 194 | + repo: context.repo.repo, |
| 195 | + title: title, |
| 196 | + body: body, |
| 197 | + labels: ['security', 'trivy'], |
| 198 | + assignees: ['copilot'] |
| 199 | + }); |
| 200 | + env: |
| 201 | + ISSUE_BODY: ${{ steps.vuln-summary.outputs.body }} |
| 202 | + |
0 commit comments