blob: ca9997b3eb01a0a686d8d6fe399c07f1604814c6 [file] [log] [blame]
Lionel Jouinc4037892022-11-16 15:53:04 +01001/*
2Copyright (c) 2022 Nordix Foundation
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
Lionel Jouin4ca06662022-09-29 11:58:10 +020017
Lionel Jouin1317f9e2022-11-17 11:20:32 +010018node('nordix-nsm-build-ubuntu2204') {
Lionel Jouin4ca06662022-09-29 11:58:10 +020019 build_number = env.BUILD_NUMBER
20 workspace = env.WORKSPACE
21 ws("${workspace}/${build_number}") {
22 def git_project = params.GIT_PROJECT
23 def current_branch = params.CURRENT_BRANCH
24 def default_branch = params.DEFAULT_BRANCH
25 def image_registry = params.IMAGE_REGISTRY
26 def version = params.IMAGE_VERSION
27 def email_recipients = EMAIL_RECIPIENTS
Lionel Jouin4b6b6f52022-10-14 16:22:23 +020028 def image_names = IMAGE_NAMES
Lionel Jouin4ca06662022-09-29 11:58:10 +020029
Lionel Jouin4b6b6f52022-10-14 16:22:23 +020030 def vulnerabilityBadge = addEmbeddableBadgeConfiguration(id: 'meridio-vulnerabilities', subject: 'vulnerabilities', color: 'peru', status: '?')
Lionel Jouin4ca06662022-09-29 11:58:10 +020031
32 timeout(30) {
33 stage('Clone/Checkout') {
34 git branch: default_branch, url: git_project
35 checkout([
36 $class: 'GitSCM',
37 branches: [[name: current_branch]],
38 extensions: [],
39 userRemoteConfigs: [[
40 refspec: '+refs/pull/*/head:refs/remotes/origin/pr/*',
41 url: git_project
42 ]]
43 ])
44 sh 'git show'
45 }
46 stage('Grype') {
Lionel Jouin4b6b6f52022-10-14 16:22:23 +020047 def command = "make grype VERSION=${version} REGISTRY=${image_registry} IMAGES='${image_names}'"
48 ExecSh(command).call()
Lionel Jouin4ca06662022-09-29 11:58:10 +020049 }
50 stage('Nancy') {
Lionel Jouin4b6b6f52022-10-14 16:22:23 +020051 def command = 'make nancy'
52 ExecSh(command).call()
Lionel Jouin4ca06662022-09-29 11:58:10 +020053 }
54 stage('Trivy') {
Lionel Jouin4b6b6f52022-10-14 16:22:23 +020055 def command = "make trivy VERSION=${version} REGISTRY=${image_registry} IMAGES='${image_names}'"
56 ExecSh(command).call()
Lionel Jouin4ca06662022-09-29 11:58:10 +020057 }
58 stage('Parse') {
Lionel Jouin4b6b6f52022-10-14 16:22:23 +020059 def command = './hack/parse_security_scan.sh'
60 ExecSh(command).call()
Lionel Jouin4ca06662022-09-29 11:58:10 +020061 }
62 stage('Report') {
Lionel Jouinc4037892022-11-16 15:53:04 +010063 if (env.DRY_RUN != 'true') {
Lionel Jouin1e04de62022-11-18 11:59:18 +010064 try {
65 archiveArtifacts artifacts: '_output/**/*.*', followSymlinks: false
66 } catch (Exception e) {
67 }
Lionel Jouin4ca06662022-09-29 11:58:10 +020068
Lionel Jouinc4037892022-11-16 15:53:04 +010069 def number_of_vulnerabilities = sh(script: 'cat _output/list.txt | grep -v "^$" | awk \'{print $1}\' | sort | uniq | wc -l', returnStdout: true).trim()
70 def list_of_vulnerabilities = sh(script: 'cat _output/list.txt | grep -v "^$" | awk \'{print $1}\' | sort | uniq | sed \':a;N;$!ba;s/\\n/ ; /g\'', returnStdout: true).trim()
71 def number_of_high_severity_vulnerabilities = sh(script: 'cat _output/list.txt | grep -v "^$" | grep -i "high" | awk \'{print $1}\' | sort | uniq | wc -l', returnStdout: true).trim()
72 def list_of_high_severity_vulnerabilities = sh(script: 'cat _output/list.txt | grep -v "^$" | grep -i "high" | awk \'{print $1}\' | sort | uniq | sed \':a;N;$!ba;s/\\n/ ; /g\'', returnStdout: true).trim()
73 def git_describe = sh(script: 'git describe --dirty --tags', returnStdout: true).trim()
74 def git_rev = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
75 def report = sh(script: 'cat _output/report.txt', returnStdout: true).trim()
Lionel Jouin4ca06662022-09-29 11:58:10 +020076
Lionel Jouinc4037892022-11-16 15:53:04 +010077 def subject = "Meridio - Security Scan - ${number_of_high_severity_vulnerabilities} high severity vulnerabilities detected"
78 def body = """
Lionel Jouin4ca06662022-09-29 11:58:10 +020079Run: ${RUN_DISPLAY_URL}
80git describe --dirty --tags: ${git_describe}
81git rev-parse HEAD: ${git_rev}
82Image registry: ${image_registry}
83Image Version: ${version}
84
Lionel Jouin4b6b6f52022-10-14 16:22:23 +020085Number of vulnerabilities: ${number_of_vulnerabilities}
86List of vulnerabilities: ${list_of_vulnerabilities}
Lionel Jouin4ca06662022-09-29 11:58:10 +020087
Lionel Jouin4b6b6f52022-10-14 16:22:23 +020088Number of vulnerabilities with high severity: ${number_of_high_severity_vulnerabilities}
89List of vulnerabilities with high severity: ${list_of_high_severity_vulnerabilities}
Lionel Jouin4ca06662022-09-29 11:58:10 +020090
91report:
92${report}
93"""
Lionel Jouinc4037892022-11-16 15:53:04 +010094 emailext body: "${body}", subject: "${subject}", to: "${email_recipients}"
Lionel Jouin4ca06662022-09-29 11:58:10 +020095
Lionel Jouinc4037892022-11-16 15:53:04 +010096 vulnerabilityBadge.setStatus("${number_of_vulnerabilities}")
97 } else {
98 Utils.markStageSkippedForConditional('Report')
99 }
Lionel Jouin4ca06662022-09-29 11:58:10 +0200100 }
101 }
102 stage('Cleanup') {
103 Cleanup()
104 }
105 }
106}
107
108// Cleanup directory
109def Cleanup() {
110 cleanWs()
111}
Lionel Jouin4b6b6f52022-10-14 16:22:23 +0200112
113// Execute command
114def ExecSh(command) {
115 return {
Lionel Jouinc4037892022-11-16 15:53:04 +0100116 if (env.DRY_RUN != 'true') {
117 sh """
118 . \${HOME}/.profile
119 ${command}
120 """
121 } else {
122 echo "${command}"
123 }
Lionel Jouin4b6b6f52022-10-14 16:22:23 +0200124 }
125}