Skip to content

Commit 112a360

Browse files
author
Vitaly Pryakhin
authored
add docker cve check (#624)
* add feature: scan docker images with grype tool before publishing * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * add python script to generate HTML report for the "docker-cve-scan-biweekly" Jenkins job * add template for HTML report * WIP * control CVE tolerance with single function
1 parent 7aca19a commit 112a360

File tree

6 files changed

+355
-0
lines changed

6 files changed

+355
-0
lines changed

helper.fish

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,25 @@ function debDockerImage; set -gx DOCKER_DISTRO deb ; end
365365
if test -z "$DOCKER_DISTRO"; alpineDockerImage
366366
else ; set -gx DOCKER_DISTRO $DOCKER_DISTRO ; end
367367
368+
function enableDockerCveCheck ; set -gx RUN_CVE_CHECKS_FOR_DOCKER_IMAGE 1 ; end
369+
function disableDockerCveCheck ; set -gx RUN_CVE_CHECKS_FOR_DOCKER_IMAGE 0 ; end
370+
function enableCveReport ; set -gx CREATE_CVE_REPORT_FOR_DOCKER_IMAGE 1 ; end
371+
function disableCveReport ; set -gx CREATE_CVE_REPORT_FOR_DOCKER_IMAGE 0 ; end
372+
function cveTolerance
373+
set allowed_values "negligible" "low" "medium" "high" "critical"
374+
if test (count $argv) -ne 1
375+
echo "Usage: cveTolerance [value]. Possible values: $allowed_values"
376+
return 1
377+
end
378+
set tolerance (string lower $argv[1])
379+
if contains -- $tolerance $allowed_values
380+
set -gx CVE_SEVERITY_THRESHOLD $tolerance
381+
else
382+
echo "Invalid tolerance value: $tolerance. Possible values: $allowed_values"
383+
return 1
384+
end
385+
end
386+
368387
function skipNondeterministic ; set -gx SKIPNONDETERMINISTIC true ; end
369388
function includeNondeterministic ; set -gx SKIPNONDETERMINISTIC false ; end
370389
if test -z "$SKIPNONDETERMINISTIC"; skipNondeterministic
@@ -2424,6 +2443,11 @@ function moveResultsToWorkspace
24242443
echo "mv JUnit XMLs ($WORKDIR/work/ArangoDB/testrunXml)"
24252444
mv $WORKDIR/work/ArangoDB/testrunXml $WORKSPACE/testrunXml
24262445
end
2446+
2447+
if test -d $WORKDIR/work/grype_reports
2448+
echo "mv grype reports ($WORKDIR/work/grype_reports)"
2449+
mv $WORKDIR/work/grype_reports $WORKSPACE/grype_reports
2450+
end
24272451
end
24282452
end
24292453

helper.linux.fish

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,7 @@ function buildDockerAny
12251225
and if test "$IMAGE_NAME1" != "$IMAGE_NAME2"
12261226
docker tag $IMAGE_NAME1 $IMAGE_NAME2
12271227
end
1228+
and validateDockerImageIfNeeded $IMAGE_NAME2
12281229
and pushDockerImage $IMAGE_NAME2
12291230
and if test "$GCR_REG" = "On"
12301231
docker tag $IMAGE_NAME1 $GCR_REG_PREFIX$IMAGE_NAME2
@@ -1245,6 +1246,36 @@ function buildDockerAny
12451246
end
12461247
end
12471248

1249+
function validateDockerImageIfNeeded
1250+
if test (count $argv) -eq 0
1251+
echo Must give docker image name as argument
1252+
return 1
1253+
end
1254+
set -l image_name $argv[1]
1255+
echo "going to scan docker image for CVEs: $image_name"
1256+
set -l filesafe_image_name (string replace "/" "-" -- $image_name)
1257+
if test "$RUN_CVE_CHECKS_FOR_DOCKER_IMAGE" = "1"; or test "$RUN_CVE_CHECKS_FOR_DOCKER_IMAGE" = "On"
1258+
if test "$CREATE_CVE_REPORT_FOR_DOCKER_IMAGE" = "1"; or test "$CREATE_CVE_REPORT_FOR_DOCKER_IMAGE" = "On"
1259+
set -l grype_report_dir $WORKDIR/work/grype_reports
1260+
if ! test -d $grype_report_dir
1261+
mkdir -p $grype_report_dir
1262+
end
1263+
set -l CVE_REPORT_FILE $grype_report_dir/grype-cve-report-$filesafe_image_name.txt
1264+
checkDockerImageForCves $image_name $CVE_REPORT_FILE
1265+
else
1266+
checkDockerImageForCves $image_name
1267+
end
1268+
end
1269+
if test $status -ne 0
1270+
echo "Grype CVE check failed for $image_name"
1271+
if test "$PUBLISH_DOCKER_IMAGE_ONLY_IF_CVE_CHECKS_PASS" = "1"; or test "$PUBLISH_DOCKER_IMAGE_ONLY_IF_CVE_CHECKS_PASS" = "On"
1272+
return 1
1273+
else
1274+
return 0
1275+
end
1276+
end
1277+
end
1278+
12481279
function buildDockerArgs
12491280
if test (count $argv) -eq 0
12501281
echo Must give image distro as argument
@@ -1732,6 +1763,10 @@ function runInContainer
17321763
-e PROMTOOL_PATH="$PROMTOOL_PATH" \
17331764
-e BUILD_REPO_INFO="$BUILD_REPO_INFO" \
17341765
-e ARANGODB_BUILD_DATE="$ARANGODB_BUILD_DATE" \
1766+
-e RUN_CVE_CHECKS_FOR_DOCKER_IMAGE="$RUN_CVE_CHECKS_FOR_DOCKER_IMAGE" \
1767+
-e CREATE_CVE_REPORT_FOR_DOCKER_IMAGE="$CREATE_CVE_REPORT_FOR_DOCKER_IMAGE" \
1768+
-e PUBLISH_DOCKER_IMAGE_ONLY_IF_CVE_CHECKS_PASS="$PUBLISH_DOCKER_IMAGE_ONLY_IF_CVE_CHECKS_PASS" \
1769+
-e CVE_SEVERITY_THRESHOLD="$CVE_SEVERITY_THRESHOLD" \
17351770
$argv)
17361771
function termhandler --on-signal TERM --inherit-variable c
17371772
if test -n "$c"
@@ -1857,6 +1892,10 @@ function interactiveContainer
18571892
-e PROMTOOL_PATH="$PROMTOOL_PATH" \
18581893
-e BUILD_REPO_INFO="$BUILD_REPO_INFO" \
18591894
-e ARANGODB_BUILD_DATE="$ARANGODB_BUILD_DATE" \
1895+
-e RUN_CVE_CHECKS_FOR_DOCKER_IMAGE="$RUN_CVE_CHECKS_FOR_DOCKER_IMAGE" \
1896+
-e CREATE_CVE_REPORT_FOR_DOCKER_IMAGE="$CREATE_CVE_REPORT_FOR_DOCKER_IMAGE" \
1897+
-e PUBLISH_DOCKER_IMAGE_ONLY_IF_CVE_CHECKS_PASS="$PUBLISH_DOCKER_IMAGE_ONLY_IF_CVE_CHECKS_PASS" \
1898+
-e CVE_SEVERITY_THRESHOLD="$CVE_SEVERITY_THRESHOLD" \
18601899
$argv
18611900

18621901
if test -n "$agentstarted"
@@ -2047,6 +2086,73 @@ function unpackBuildFiles
20472086
runInContainer (eval echo \$UBUNTUBUILDIMAGE_$ARANGODB_VERSION_MAJOR$ARANGODB_VERSION_MINOR) $SCRIPTSDIR/unpackBuildFiles.fish "$argv[1]"
20482087
end
20492088

2089+
function installGrype
2090+
if not set -q GRYPE_DIR[1]
2091+
set -gx GRYPE_DIR "$WORKDIR/work/tools/grype"
2092+
end
2093+
echo "Installing grype to $GRYPE_DIR"
2094+
if test ! -d "$GRYPE_DIR"
2095+
mkdir -p "$GRYPE_DIR"
2096+
end
2097+
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b $GRYPE_DIR
2098+
set -l GRYPE_BIN "$GRYPE_DIR/grype"
2099+
$GRYPE_BIN version 2>&1 > /dev/null
2100+
if test $status -ne 0
2101+
echo "Failed to install grype"
2102+
return 1
2103+
end
2104+
set -gx GRYPE_BIN $GRYPE_BIN
2105+
echo "Grype is installed successfully"
2106+
end
2107+
2108+
function downloadOrUpdateGrype
2109+
# if grype location is not predefined, set it to default value
2110+
if not set -q GRYPE_DIR[1]
2111+
set -gx GRYPE_DIR "$WORKDIR/work/tools/grype"
2112+
end
2113+
if not set -q GRYPE_BIN[1]
2114+
set -gx GRYPE_BIN "$GRYPE_DIR/grype"
2115+
end
2116+
if test -f "$GRYPE_BIN"
2117+
$GRYPE_BIN version
2118+
if test $status -eq 0
2119+
echo "Grype is already installed. Updating the CVE database"
2120+
$GRYPE_BIN db update
2121+
if test $status -eq 0
2122+
echo "Grype CVE database is updated successfully"
2123+
return 0
2124+
end
2125+
echo "Failed to update the Grype CVE database"
2126+
return 1
2127+
end
2128+
end
2129+
2130+
# grype is not installed
2131+
installGrype
2132+
end
2133+
2134+
function checkDockerImageForCves
2135+
if not set -q GRYPE_BIN[1]
2136+
downloadOrUpdateGrype
2137+
if test $status -ne 0
2138+
return 1
2139+
end
2140+
end
2141+
set -l image $argv[1]
2142+
set -l report_file $argv[2]
2143+
if not set -q CVE_SEVERITY_THRESHOLD[1]
2144+
set CVE_SEVERITY_THRESHOLD "high"
2145+
end
2146+
echo "scanning image for CVEs: $image"
2147+
if set -q report_file[1]
2148+
$GRYPE_BIN -f $CVE_SEVERITY_THRESHOLD -s all-layers --file $report_file docker:$image
2149+
or return $status
2150+
else
2151+
$GRYPE_BIN -f $CVE_SEVERITY_THRESHOLD -s all-layers docker:$image
2152+
or return $status
2153+
end
2154+
end
2155+
20502156
## #############################################################################
20512157
## set PARALLELISM in a sensible way
20522158
## #############################################################################

jenkins/forTestDocker.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ and downloadStarter
3838
and setArchSuffix
3939
and set -xg HUB_COMMUNITY "arangodb/arangodb-test:$DOCKER_TAG_JENKINS$archSuffix"
4040
and buildDockerImage $HUB_COMMUNITY
41+
and validateDockerImageIfNeeded $HUB_COMMUNITY
4142
and docker push $HUB_COMMUNITY
4243
and docker tag $HUB_COMMUNITY $GCR_REG_PREFIX$HUB_COMMUNITY
4344
and docker push $GCR_REG_PREFIX$HUB_COMMUNITY

jenkins/forTestDockerCommunity.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ and downloadStarter
3838
and setArchSuffix
3939
and set -xg HUB_COMMUNITY "arangodb/arangodb-test:$DOCKER_TAG_JENKINS$archSuffix"
4040
and buildDockerImage $HUB_COMMUNITY
41+
and validateDockerImageIfNeeded $HUB_COMMUNITY
4142
and docker push $HUB_COMMUNITY
4243
and docker tag $HUB_COMMUNITY $GCR_REG_PREFIX$HUB_COMMUNITY
4344
and docker push $GCR_REG_PREFIX$HUB_COMMUNITY

jenkins/forTestDockerEnterprise.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ and copyRclone "linux"
4040
and setArchSuffix
4141
and set -xg HUB_ENTERPRISE "arangodb/enterprise-test:$DOCKER_TAG_JENKINS$archSuffix"
4242
and buildDockerImage $HUB_ENTERPRISE
43+
and validateDockerImageIfNeeded $HUB_COMMUNITY
4344
and docker push $HUB_ENTERPRISE
4445
and docker tag $HUB_ENTERPRISE $GCR_REG_PREFIX$HUB_ENTERPRISE
4546
and docker push $GCR_REG_PREFIX$HUB_ENTERPRISE

0 commit comments

Comments
 (0)