Skip to content

Commit 4b0abc8

Browse files
add dependent image scanning (#199)
1 parent cf1bc48 commit 4b0abc8

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

Jenkinsfile

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import groovy.json.JsonSlurperClassic
77

88
9+
910
gitCredID = 'marklogic-builder-github'
1011
JIRA_ID = ''
1112
JIRA_ID_PATTERN = /(?i)(MLE)-\d{3,6}/
@@ -104,7 +105,7 @@ void resultNotification(message) {
104105
emailList = params.emailList
105106
}
106107
jira_link = "https://project.marklogic.com/jira/browse/${JIRA_ID}"
107-
email_body = "<b>Jenkins pipeline for</b> ${env.JOB_NAME} <br><b>Build Number: </b>${env.BUILD_NUMBER} <b><br><br>Lint Output: <br></b><pre><code>${LINT_OUTPUT}</code></pre><br><br><b>Build URL: </b><br>${env.BUILD_URL}"
108+
email_body = "<b>Jenkins pipeline for</b> ${env.JOB_NAME} <br><b>Build Number: </b>${env.BUILD_NUMBER} <br><br><b>Lint Output: </b><br><pre><code>${LINT_OUTPUT}</code></pre><br><br><b>Scan Output: </b><br><pre><code>${SCAN_OUTPUT}</code></pre><br><br><b>Build URL: </b><br>${env.BUILD_URL}"
108109
jira_email_body = "${email_body} <br><br><b>Jira URL: </b><br>${jira_link}"
109110

110111
if (JIRA_ID) {
@@ -128,6 +129,18 @@ void lint() {
128129
'''
129130
}
130131

132+
void imageScan() {
133+
sh "make image-scan saveOutput=true"
134+
135+
SCAN_OUTPUT = sh(returnStdout: true, script:'cat dep-image-scan.txt')
136+
hasCriticalOrHigh = SCAN_OUTPUT.contains("High") || SCAN_OUTPUT.contains("Critical")
137+
if (hasCriticalOrHigh) {
138+
mail charset: 'UTF-8', mimeType: 'text/html', to: "${emailSecList}", body: "<br>Jenkins pipeline for ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br>Vulnerabilities: <pre><code>${SCAN_OUTPUT}</code></pre>", subject: "Critical or High Security Vulnerabilities Found: ${env.JOB_NAME} #${env.BUILD_NUMBER}"
139+
}
140+
141+
sh '''rm -f dep-image-scan.txt'''
142+
}
143+
131144
void publishTestResults() {
132145
junit allowEmptyResults:true, testResults: '**/test/test_results/*.xml'
133146
archiveArtifacts artifacts: '**/test/test_results/*.xml', allowEmptyArchive: true
@@ -156,7 +169,7 @@ pipeline {
156169
skipStagesAfterUnstable()
157170
}
158171
triggers {
159-
parameterizedCron( env.BRANCH_NAME == 'develop' ? '''00 04 * * *''' : '')
172+
parameterizedCron( env.BRANCH_NAME == 'develop' ? '''00 04 * * * % IMAGE_SCAN=true''' : '')
160173
}
161174
environment {
162175
//timeStamp = sh(returnStdout: true, script: "date +%Y%m%d -d '-5 hours'").trim()
@@ -174,6 +187,7 @@ pipeline {
174187
choice(name: 'ML_VERSION', choices: '11.2\n12.0\n10.0', description: 'MarkLogic version. used to pick appropriate docker image')
175188
booleanParam(name: 'KUBERNETES_TESTS', defaultValue: true, description: 'Run kubernetes tests')
176189
booleanParam(name: 'HC_TESTS', defaultValue: false, description: 'Run Hub Central E2E UI tests (takes about 3 hours)')
190+
booleanParam(name: 'IMAGE_SCAN', defaultValue: false, description: 'Find and scan dependent Docker images for security vulnerabilities')
177191
string(name: 'dockerReleaseVer', defaultValue: '1.1.2', description: 'Current Docker version. (e.g. 1.0.1)', trim: true)
178192
choice(name: 'PREV_ML_VERSION', choices: '10.0\n9.0\n11.2', description: 'Previous MarkLogic version for MarkLogic upgrade tests')
179193
string(name: 'prevDockerReleaseVer', defaultValue: '1.1.2', description: 'Previous Docker version for MarkLogic upgrade tests. (e.g. 1.0.1)', trim: true)
@@ -193,6 +207,15 @@ pipeline {
193207
}
194208
}
195209

210+
stage('Image-Scan') {
211+
when {
212+
expression { return params.IMAGE_SCAN }
213+
}
214+
steps {
215+
imageScan()
216+
}
217+
}
218+
196219
stage('Kubernetes-Run-Tests') {
197220
when {
198221
expression { return params.KUBERNETES_TESTS }

makefile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ kubernetesVersion?=v1.25.8
44
minikubeMemory?=10gb
55
## System requirement:
66
## - Go
7-
## - gotestsum (if you want to enable saveOutput for testing commands)
7+
## - gotestsum (if you want to enable output saving for testing commands)
88
## - golangci-lint
99
## - Helm
1010
## - Minikube
@@ -181,3 +181,19 @@ template-test: prepare
181181
## * [saveOutput] optional. Save the output to a xml file. Example: saveOutput=true
182182
.PHONY: test
183183
test: template-test e2e-test
184+
185+
#***************************************************************************
186+
# image-scan
187+
#***************************************************************************
188+
## Find and scan dependent Docker images for security vulnerabilities
189+
## Options:
190+
## * [saveOutput] optional. Save the output to a xml file. Example: saveOutput=true
191+
.PHONY: image-scan
192+
image-scan:
193+
194+
@echo "=====Scan dependent Docker images in charts/values.yaml" $(if $(saveOutput), | tee -a dep-image-scan.txt,)
195+
@for depImage in $(shell grep -E "^.*\bimage:\s+(.*)" charts/values.yaml | sed 's/image: //g' | sed 's/"//g'); do\
196+
echo " - $${depImage}" $(if $(saveOutput), | tee -a dep-image-scan.txt,) ; \
197+
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock anchore/grype:latest $${depImage} | grep 'High\|Critical' $(if $(saveOutput), | tee -a dep-image-scan.txt,);\
198+
echo $(if $(saveOutput), | tee -a dep-image-scan.txt,) ;\
199+
done

0 commit comments

Comments
 (0)