Skip to content

Commit 01f949c

Browse files
committed
CI: Minor image scanning tweaks
1 parent feedec5 commit 01f949c

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

.github/workflows/stackhpc-container-image-build.yml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ on:
3333
type: boolean
3434
required: false
3535
default: true
36-
push-dirty:
36+
sbom:
37+
description: Generate SBOM?
38+
type: boolean
39+
required: false
40+
default: true
41+
push-critical:
3742
description: Push scanned images that have critical vulnerabilities?
3843
type: boolean
3944
required: false
@@ -252,14 +257,14 @@ jobs:
252257
run: if [ $(wc -l < ${{ matrix.distro.name }}-${{ matrix.distro.release }}-container-images) -le 1 ]; then exit 1; fi
253258

254259
- name: Scan built container images
255-
run: src/kayobe-config/tools/scan-images.sh ${{ matrix.distro.name }}-${{ matrix.distro.release }} ${{ steps.write-kolla-tag.outputs.kolla-tag }}
260+
run: src/kayobe-config/tools/scan-images.sh ${{ matrix.distro.name }}-${{ matrix.distro.release }} ${{ steps.write-kolla-tag.outputs.kolla-tag }} ${{ inputs.sbom && '--sbom'}}
256261

257262
- name: Move image scan logs to output artifact
258263
run: mv image-scan-output image-build-logs/image-scan-output
259264

260-
- name: Fail if no images have passed scanning
265+
- name: Fail if any images have critical vulnerabilities
261266
run: if [ $(wc -l < image-build-logs/image-scan-output/critical-images.txt) -gt 0 ]; then exit 1; fi
262-
if: ${{ !inputs.push-dirty }}
267+
if: ${{ !inputs.push-critical }}
263268

264269
- name: Copy clean images to push-attempt-images list
265270
run: cp image-build-logs/image-scan-output/clean-images.txt image-build-logs/push-attempt-images.txt
@@ -269,13 +274,13 @@ jobs:
269274
# This should be reverted when it's decided to filter high level CVEs as well.
270275
- name: Append dirty images to push list
271276
run: |
272-
cat image-build-logs/image-scan-output/dirty-images.txt >> image-build-logs/push-attempt-images.txt
277+
cat image-build-logs/image-scan-output/high-images.txt >> image-build-logs/push-attempt-images.txt
273278
if: ${{ inputs.push }}
274279

275280
- name: Append images with critical vulnerabilities to push list
276281
run: |
277282
cat image-build-logs/image-scan-output/critical-images.txt >> image-build-logs/push-attempt-images.txt
278-
if: ${{ inputs.push && inputs.push-dirty }}
283+
if: ${{ inputs.push && inputs.push-critical }}
279284

280285
- name: Push images
281286
run: |
@@ -324,12 +329,12 @@ jobs:
324329
# This can be used again instead of "Fail when critical vulnerabilities are found" when it's
325330
# decided to fail the job on detecting high CVEs as well.
326331
# - name: Fail when images failed scanning
327-
# run: if [ $(wc -l < image-build-logs/image-scan-output/dirty-images.txt) -gt 0 ]; then cat image-build-logs/image-scan-output/dirty-images.txt && exit 1; fi
328-
# if: ${{ !inputs.push-dirty && !cancelled() }}
332+
# run: if [ $(wc -l < image-build-logs/image-scan-output/high-images.txt) -gt 0 ]; then cat image-build-logs/image-scan-output/high-images.txt && exit 1; fi
333+
# if: ${{ !inputs.push-critical && !cancelled() }}
329334

330335
- name: Fail when critical vulnerabilities are found
331336
run: if [ $(wc -l < image-build-logs/image-scan-output/critical-images.txt) -gt 0 ]; then cat image-build-logs/image-scan-output/critical-images.txt && exit 1; fi
332-
if: ${{ !inputs.push-dirty && !cancelled() }}
337+
if: ${{ !inputs.push-critical && !cancelled() }}
333338

334339
# NOTE(mgoddard): Trigger another CI workflow in the
335340
# stackhpc-release-train repository.

tools/scan-images.sh

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ check_deps_installed() {
3535
file_prep() {
3636
rm -rf image-scan-output
3737
mkdir -p image-scan-output
38-
touch image-scan-output/clean-images.txt image-scan-output/dirty-images.txt image-scan-output/critical-images.txt
38+
touch image-scan-output/clean-images.txt image-scan-output/high-images.txt image-scan-output/critical-images.txt
3939
}
4040

4141
# Gather image lists
@@ -99,7 +99,21 @@ categorise_image() {
9999
fi
100100
}
101101

102-
# Scan images, generate SBOMs if requested
102+
# Generate SBOM, return correct scan command for SBOM
103+
generate_sbom() {
104+
local imagename=$1
105+
local filename=$2
106+
local image=$3
107+
trivy image \
108+
--format spdx-json \
109+
--output image-scan-output/${imagename}/${filename}-sbom.json \
110+
$image > /dev/null 2>&1
111+
echo "trivy sbom $scan_common_args \
112+
--output image-scan-output/${imagename}/${filename}-scan.json \
113+
image-scan-output/${imagename}/${filename}-sbom.json"
114+
}
115+
116+
# Scan images, generate SBOMs if requested
103117
scan_image() {
104118
local image=$1
105119
local filename=$(basename $image | sed 's/:/\./g')
@@ -108,25 +122,19 @@ scan_image() {
108122
mkdir -p image-scan-output/$imagename
109123
generate_trivy_ignore $imagename
110124

111-
echo "Scanning $imagename"
112-
113-
# If SBOM is required, generate that first, then generate scan results from it
125+
# If SBOM is required, generate it first and scan the results, otherwise we
126+
# scan the image directly.
114127
if $generate_sbom; then
115-
trivy image \
116-
--format spdx-json \
117-
--output image-scan-output/${imagename}/${filename}-sbom.json \
118-
$image
119-
scan_command="trivy sbom $scan_common_args \
120-
--output image-scan-output/${imagename}/${filename}-scan.json \
121-
image-scan-output/${imagename}/${filename}-sbom.json"
128+
echo "Generating SBOM for $imagename"
129+
scan_command=$(generate_sbom $imagename $filename $image)
122130
else
123131
scan_command="trivy image $scan_common_args \
124132
--output image-scan-output/${imagename}/${filename}-scan.json $image"
125133
fi
126-
echo "scan command"
127-
echo "$scan_command"
128-
# Run scan, against image or SBOM. If no results, delete files.
129-
if $scan_command; then
134+
135+
# Run scan against image or SBOM, format output. If no results, delete files.
136+
echo "Scanning $imagename for vulnerabilities"
137+
if $scan_command > /dev/null 2>&1; then
130138
rm -f image-scan-output/${imagename}/${filename}-scan.json
131139
echo "${image}" >> image-scan-output/clean-images.txt
132140
else

0 commit comments

Comments
 (0)