Skip to content

Commit a4678c7

Browse files
committed
CI: Minor image scanning tweaks
1 parent 711d85a commit a4678c7

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
@@ -244,14 +249,14 @@ jobs:
244249
run: if [ $(wc -l < ${{ matrix.distro.name }}-${{ matrix.distro.release }}-container-images) -le 1 ]; then exit 1; fi
245250

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

249254
- name: Move image scan logs to output artifact
250255
run: mv image-scan-output image-build-logs/image-scan-output
251256

252-
- name: Fail if no images have passed scanning
257+
- name: Fail if any images have critical vulnerabilities
253258
run: if [ $(wc -l < image-build-logs/image-scan-output/critical-images.txt) -gt 0 ]; then exit 1; fi
254-
if: ${{ !inputs.push-dirty }}
259+
if: ${{ !inputs.push-critical }}
255260

256261
- name: Copy clean images to push-attempt-images list
257262
run: cp image-build-logs/image-scan-output/clean-images.txt image-build-logs/push-attempt-images.txt
@@ -261,13 +266,13 @@ jobs:
261266
# This should be reverted when it's decided to filter high level CVEs as well.
262267
- name: Append dirty images to push list
263268
run: |
264-
cat image-build-logs/image-scan-output/dirty-images.txt >> image-build-logs/push-attempt-images.txt
269+
cat image-build-logs/image-scan-output/high-images.txt >> image-build-logs/push-attempt-images.txt
265270
if: ${{ inputs.push }}
266271

267272
- name: Append images with critical vulnerabilities to push list
268273
run: |
269274
cat image-build-logs/image-scan-output/critical-images.txt >> image-build-logs/push-attempt-images.txt
270-
if: ${{ inputs.push && inputs.push-dirty }}
275+
if: ${{ inputs.push && inputs.push-critical }}
271276

272277
- name: Push images
273278
run: |
@@ -316,12 +321,12 @@ jobs:
316321
# This can be used again instead of "Fail when critical vulnerabilities are found" when it's
317322
# decided to fail the job on detecting high CVEs as well.
318323
# - name: Fail when images failed scanning
319-
# 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
320-
# if: ${{ !inputs.push-dirty && !cancelled() }}
324+
# 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
325+
# if: ${{ !inputs.push-critical && !cancelled() }}
321326

322327
- name: Fail when critical vulnerabilities are found
323328
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
324-
if: ${{ !inputs.push-dirty && !cancelled() }}
329+
if: ${{ !inputs.push-critical && !cancelled() }}
325330

326331
# NOTE(mgoddard): Trigger another CI workflow in the
327332
# 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)