-
Notifications
You must be signed in to change notification settings - Fork 5
315 lines (266 loc) · 10.7 KB
/
cve-comparison.yml
File metadata and controls
315 lines (266 loc) · 10.7 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# =============================================================================
# CVE Before/After Comparison Report
# =============================================================================
#
# Manual-only workflow that scans all images, rebuilds them locally, and
# produces a markdown report showing CVE changes (before vs after rebuild).
# Nothing is pushed — this is purely informational.
#
# =============================================================================
name: CVE comparison report
on:
workflow_dispatch:
inputs:
filter:
description: 'Image filter (regex, e.g. "redis|mariadb"). Leave empty for all.'
type: string
default: ''
severity:
description: 'Minimum severity to report'
type: choice
options:
- low
- medium
- high
- critical
default: medium
permissions:
contents: read
concurrency:
group: cve-comparison
cancel-in-progress: true
jobs:
# ---------------------------------------------------------------------------
# Discover all images from TAGS files, optionally filtered
# ---------------------------------------------------------------------------
discover:
name: Discover images
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.discover.outputs.matrix }}
steps:
- uses: actions/checkout@v5
- name: Build image matrix
id: discover
env:
FILTER: ${{ inputs.filter }}
run: |
matrix="[]"
for tags_file in */*/TAGS; do
[ -f "$tags_file" ] || continue
image_name=$(echo "$tags_file" | cut -d'/' -f1)
version_dir=$(echo "$tags_file" | cut -d'/' -f2)
primary_tag=$(head -1 "$tags_file" | tr -d '[:space:]')
[ -z "$primary_tag" ] && continue
# Apply filter if set
if [ -n "$FILTER" ]; then
echo "${image_name}/${version_dir}" | grep -qE "$FILTER" || continue
fi
# Check Dockerfile exists
[ -f "${image_name}/${version_dir}/Dockerfile" ] || continue
matrix=$(echo "$matrix" | jq \
--arg img "$image_name" \
--arg dir "$version_dir" \
--arg tag "$primary_tag" \
'. + [{"image": $img, "dir": $dir, "tag": $tag}]')
done
echo "matrix=$(echo "$matrix" | jq -c '.')" >> "$GITHUB_OUTPUT"
count=$(echo "$matrix" | jq 'length')
echo "Discovered $count images"
echo "## Images to scan: $count" >> "$GITHUB_STEP_SUMMARY"
# ---------------------------------------------------------------------------
# Scan current + rebuild + scan new for each image
# ---------------------------------------------------------------------------
compare:
name: "${{ matrix.entry.image }}:${{ matrix.entry.tag }}"
needs: discover
if: ${{ fromJson(needs.discover.outputs.matrix)[0] != null }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
max-parallel: 4
matrix:
entry: ${{ fromJson(needs.discover.outputs.matrix) }}
env:
DOCKER_USER: ${{ secrets.DOCKER_USER }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
steps:
- uses: actions/checkout@v5
- name: Install Docker Scout
run: |
curl -fsSL https://raw.githubusercontent.com/docker/scout-cli/main/install.sh -o install-scout.sh
sh install-scout.sh
- name: Docker login
run: |
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USER" --password-stdin
- name: Login to DHI registry
run: |
if [ -n "$DOCKER_PASSWORD" ]; then
echo "$DOCKER_PASSWORD" | docker login dhi.io -u "$DOCKER_USER" --password-stdin 2>/dev/null || true
fi
- name: Scan current published image
id: before
env:
IMAGE: "wunderio/${{ matrix.entry.image }}:${{ matrix.entry.tag }}"
run: |
echo "Scanning $IMAGE ..."
mkdir -p /tmp/results
if docker scout cves "$IMAGE" --only-fixed 2>&1 | tee /tmp/results/before.txt; then
echo "scan_ok=true" >> "$GITHUB_OUTPUT"
else
if grep -q "MANIFEST_UNKNOWN\|not found" /tmp/results/before.txt; then
echo "scan_ok=false" >> "$GITHUB_OUTPUT"
else
echo "scan_ok=true" >> "$GITHUB_OUTPUT"
fi
fi
- name: Rebuild image locally
id: build
env:
IMAGE_NAME: ${{ matrix.entry.image }}
VERSION_DIR: ${{ matrix.entry.dir }}
TAG: ${{ matrix.entry.tag }}
run: |
build_dir="${IMAGE_NAME}/${VERSION_DIR}"
local_tag="local/${IMAGE_NAME}:${TAG}-rebuilt"
echo "Building ${build_dir} ..."
if docker build "$build_dir" --tag "$local_tag" --pull --no-cache --quiet; then
echo "build_ok=true" >> "$GITHUB_OUTPUT"
echo "local_tag=$local_tag" >> "$GITHUB_OUTPUT"
else
echo "::warning::Build failed for ${IMAGE_NAME}:${TAG}"
echo "build_ok=false" >> "$GITHUB_OUTPUT"
fi
- name: Scan rebuilt image
if: steps.build.outputs.build_ok == 'true'
env:
LOCAL_TAG: ${{ steps.build.outputs.local_tag }}
run: |
echo "Scanning rebuilt image ..."
docker scout cves "local://$LOCAL_TAG" --only-fixed 2>&1 | tee /tmp/results/after.txt || true
- name: Generate comparison data
env:
IMAGE_NAME: ${{ matrix.entry.image }}
TAG: ${{ matrix.entry.tag }}
BUILD_OK: ${{ steps.build.outputs.build_ok }}
SCAN_OK: ${{ steps.before.outputs.scan_ok }}
run: |
parse_counts() {
local file="$1"
if [ ! -f "$file" ]; then echo "0,0,0,0,0"; return; fi
critical=$(grep -oP '\d+(?=C)' "$file" 2>/dev/null | head -1 || echo "0")
high=$(grep -oP '\d+(?=H)' "$file" 2>/dev/null | head -1 || echo "0")
medium=$(grep -oP '\d+(?=M)' "$file" 2>/dev/null | head -1 || echo "0")
low=$(grep -oP '\d+(?=L)' "$file" 2>/dev/null | head -1 || echo "0")
[ -z "$critical" ] && critical=0
[ -z "$high" ] && high=0
[ -z "$medium" ] && medium=0
[ -z "$low" ] && low=0
total=$((critical + high + medium + low))
echo "${critical},${high},${medium},${low},${total}"
}
before_counts=$(parse_counts /tmp/results/before.txt)
if [ "$BUILD_OK" = "true" ]; then
after_counts=$(parse_counts /tmp/results/after.txt)
else
after_counts="N/A,N/A,N/A,N/A,N/A"
fi
mkdir -p /tmp/comparison
echo "${IMAGE_NAME},${TAG},${SCAN_OK},${BUILD_OK},${before_counts},${after_counts}" \
> "/tmp/comparison/${IMAGE_NAME}__${TAG}.csv"
- name: Upload comparison data
uses: actions/upload-artifact@v4
with:
name: "compare-${{ matrix.entry.image }}-${{ matrix.entry.dir }}"
path: /tmp/comparison/
retention-days: 30
# ---------------------------------------------------------------------------
# Assemble final markdown report
# ---------------------------------------------------------------------------
report:
name: Generate report
needs: [discover, compare]
if: always() && needs.discover.result == 'success'
runs-on: ubuntu-latest
steps:
- name: Download all comparison data
uses: actions/download-artifact@v4
with:
path: /tmp/all-comparisons
pattern: compare-*
merge-multiple: true
- name: Build markdown report
env:
SEVERITY_FILTER: ${{ inputs.severity }}
IMAGE_FILTER: ${{ inputs.filter }}
run: |
report="/tmp/cve-comparison-report.md"
date=$(date -u +%Y-%m-%d)
cat > "$report" <<EOF
# CVE Before/After Comparison Report
**Date:** ${date}
**Severity filter:** ${SEVERITY_FILTER:-medium}+
**Image filter:** ${IMAGE_FILTER:-all}
## Results
| Image | Tag | Before (C/H/M/L) | After (C/H/M/L) | Before Total | After Total | Delta | Status |
|-------|-----|-------------------|------------------|:------------:|:-----------:|:-----:|:------:|
EOF
improved=0
unchanged=0
degraded=0
build_failed=0
not_published=0
for csv_file in /tmp/all-comparisons/*.csv; do
[ -f "$csv_file" ] || continue
IFS=',' read -r image tag scan_ok build_ok \
bc bh bm bl btotal \
ac ah am al atotal < "$csv_file"
before_sev="${bc}/${bh}/${bm}/${bl}"
if [ "$scan_ok" = "false" ]; then
echo "| \`${image}\` | \`${tag}\` | _not published_ | — | — | — | — | :new: |" >> "$report"
not_published=$((not_published + 1))
continue
fi
if [ "$build_ok" != "true" ]; then
echo "| \`${image}\` | \`${tag}\` | ${before_sev} | _build failed_ | ${btotal} | — | — | :x: |" >> "$report"
build_failed=$((build_failed + 1))
continue
fi
after_sev="${ac}/${ah}/${am}/${al}"
delta=$((atotal - btotal))
if [ "$delta" -lt 0 ]; then
abs_delta=${delta#-}
status=":white_check_mark: -${abs_delta}"
improved=$((improved + 1))
elif [ "$delta" -gt 0 ]; then
status=":warning: +${delta}"
degraded=$((degraded + 1))
else
status=":heavy_minus_sign: 0"
unchanged=$((unchanged + 1))
fi
echo "| \`${image}\` | \`${tag}\` | ${before_sev} | ${after_sev} | ${btotal} | ${atotal} | ${delta} | ${status} |" >> "$report"
done
total=$((improved + unchanged + degraded + build_failed + not_published))
cat >> "$report" <<EOF
## Summary
| Category | Count |
|----------|:-----:|
| Improved (fewer CVEs after rebuild) | ${improved} |
| Unchanged | ${unchanged} |
| Degraded (more CVEs — investigate) | ${degraded} |
| Build failed | ${build_failed} |
| Not yet published | ${not_published} |
| **Total images scanned** | **${total}** |
---
*Generated by cve-comparison.yml workflow*
EOF
# Show in workflow summary and console
cat "$report"
cat "$report" >> "$GITHUB_STEP_SUMMARY"
- name: Upload report
uses: actions/upload-artifact@v4
with:
name: cve-comparison-report
path: /tmp/cve-comparison-report.md
retention-days: 90