-
Notifications
You must be signed in to change notification settings - Fork 117
241 lines (213 loc) · 9.66 KB
/
Copy pathdocker-scan.yml
File metadata and controls
241 lines (213 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
name: Docker Image Scanning (Trivy)
# Issue #772 — Add Docker image vulnerability scanning to CI
on:
push:
branches: [main, 'feature/**']
paths:
- 'backend/Dockerfile'
- 'frontend/Dockerfile'
- 'backend/**'
- 'frontend/**'
- '.trivyignore'
- '.github/workflows/docker-scan.yml'
pull_request:
branches: [main]
schedule:
# Nightly scan to catch newly published CVEs against already-merged images
- cron: '0 3 * * *'
permissions:
contents: read
packages: write # push the scan-tagged image to GHCR so it can be keylessly signed
id-token: write # required for cosign's keyless OIDC signing
jobs:
trivy-backend:
name: Trivy — Backend Image
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232e2 # v3.10.0
- name: Build backend Docker image
uses: docker/build-push-action@14487ce63c7a62a4a324b0bfb37086795e31c6c1 # v6.16.0
with:
context: ./backend
push: false
load: true
tags: future-backend:${{ github.sha }}
cache-from: type=gha,scope=backend
cache-to: type=gha,mode=max,scope=backend
- name: Generate backend SBOM (CycloneDX)
uses: anchore/sbom-action@e22c389904149dbc22b58101806040fa8d37a610 # v0.24.0
with:
image: future-backend:${{ github.sha }}
format: cyclonedx-json
output-file: sbom-backend.cyclonedx.json
upload-artifact: false
- name: Upload backend SBOM
uses: actions/upload-artifact@v4
with:
name: sbom-backend-${{ github.sha }}
path: sbom-backend.cyclonedx.json
retention-days: 30
- name: Run Trivy on backend image
uses: aquasecurity/trivy-action@76071ef0d7ec797419534a183b498b4d6366cf37 # v0.31.0
with:
image-ref: future-backend:${{ github.sha }}
format: sarif
output: trivy-backend.sarif
severity: CRITICAL,HIGH
exit-code: '1'
ignore-unfixed: true
trivyignores: .trivyignore
- name: Upload backend Trivy SARIF report
if: always()
uses: actions/upload-artifact@v4
with:
name: trivy-backend-report-${{ github.sha }}
path: trivy-backend.sarif
retention-days: 30
# Signing requires pushing to GHCR, which forked PRs can't authenticate
# to — skip on untrusted (fork) pull_request events, run everywhere else.
- name: Log in to GitHub Container Registry
if: success() && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push scan-tagged backend image to GHCR
id: push_backend
if: success() && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
uses: docker/build-push-action@14487ce63c7a62a4a324b0bfb37086795e31c6c1 # v6.16.0
with:
context: ./backend
push: true
tags: ghcr.io/${{ github.repository }}/backend:scan-${{ github.sha }}
cache-from: type=gha,scope=backend
cache-to: type=gha,mode=max,scope=backend
- name: Install cosign
if: success() && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2
- name: Sign backend image (keyless)
if: success() && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
run: cosign sign --yes "ghcr.io/${{ github.repository }}/backend@${{ steps.push_backend.outputs.digest }}"
trivy-frontend:
name: Trivy — Frontend Image
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232e2 # v3.10.0
- name: Build frontend Docker image
uses: docker/build-push-action@14487ce63c7a62a4a324b0bfb37086795e31c6c1 # v6.16.0
with:
context: ./frontend
push: false
load: true
tags: future-frontend:${{ github.sha }}
cache-from: type=gha,scope=frontend
cache-to: type=gha,mode=max,scope=frontend
- name: Generate frontend SBOM (CycloneDX)
uses: anchore/sbom-action@e22c389904149dbc22b58101806040fa8d37a610 # v0.24.0
with:
image: future-frontend:${{ github.sha }}
format: cyclonedx-json
output-file: sbom-frontend.cyclonedx.json
upload-artifact: false
- name: Upload frontend SBOM
uses: actions/upload-artifact@v4
with:
name: sbom-frontend-${{ github.sha }}
path: sbom-frontend.cyclonedx.json
retention-days: 30
- name: Run Trivy on frontend image
uses: aquasecurity/trivy-action@76071ef0d7ec797419534a183b498b4d6366cf37 # v0.31.0
with:
image-ref: future-frontend:${{ github.sha }}
format: sarif
output: trivy-frontend.sarif
severity: CRITICAL,HIGH
exit-code: '1'
ignore-unfixed: true
trivyignores: .trivyignore
- name: Upload frontend Trivy SARIF report
if: always()
uses: actions/upload-artifact@v4
with:
name: trivy-frontend-report-${{ github.sha }}
path: trivy-frontend.sarif
retention-days: 30
# Signing requires pushing to GHCR, which forked PRs can't authenticate
# to — skip on untrusted (fork) pull_request events, run everywhere else.
- name: Log in to GitHub Container Registry
if: success() && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push scan-tagged frontend image to GHCR
id: push_frontend
if: success() && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
uses: docker/build-push-action@14487ce63c7a62a4a324b0bfb37086795e31c6c1 # v6.16.0
with:
context: ./frontend
push: true
tags: ghcr.io/${{ github.repository }}/frontend:scan-${{ github.sha }}
cache-from: type=gha,scope=frontend
cache-to: type=gha,mode=max,scope=frontend
- name: Install cosign
if: success() && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2
- name: Sign frontend image (keyless)
if: success() && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
run: cosign sign --yes "ghcr.io/${{ github.repository }}/frontend@${{ steps.push_frontend.outputs.digest }}"
docker-scan-gate:
name: Docker Scan Gate
runs-on: ubuntu-latest
needs: [trivy-backend, trivy-frontend, dockerfile-nonroot]
if: always()
steps:
- name: Check scan results
run: |
if [ "${{ needs.trivy-backend.result }}" != "success" ]; then
echo "❌ Backend image scan failed — HIGH/CRITICAL CVEs detected"
exit 1
fi
if [ "${{ needs.trivy-frontend.result }}" != "success" ]; then
echo "❌ Frontend image scan failed — HIGH/CRITICAL CVEs detected"
exit 1
fi
if [ "${{ needs.dockerfile-nonroot.result }}" != "success" ]; then
echo "❌ Production Dockerfiles must set a non-root USER"
exit 1
fi
echo "✅ Docker image scans passed — no unfixed HIGH/CRITICAL CVEs, USER set"
- name: Comment PR on scan failure
if: failure() && github.event_name == 'pull_request'
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '🔒 **Docker image scan failed.** HIGH or CRITICAL CVEs were detected in the container image(s), or a production Dockerfile is missing a non-root USER. Download the `trivy-*-report` artifacts for details. Add justified suppressions to `.trivyignore` or update the base image to remediate.'
})
dockerfile-nonroot:
name: Dockerfiles must run as non-root
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Require USER in production Dockerfiles
run: |
failed=0
for f in backend/Dockerfile frontend/Dockerfile; do
if ! grep -qE '^USER[[:space:]]+' "$f"; then
echo "ERROR: $f has no USER directive — runtime would run as root"
failed=1
else
echo "OK: $f sets USER $(grep -E '^USER[[:space:]]+' "$f" | awk '{print $2}' | paste -sd, -)"
fi
done
exit "$failed"