2018-09-21 16:55:31 +02:00
|
|
|
import static com.sap.piper.Prerequisites.checkScript
|
|
|
|
|
2018-06-25 13:14:46 +02:00
|
|
|
import com.sap.piper.ConfigurationHelper
|
|
|
|
import com.sap.piper.Utils
|
|
|
|
import com.sap.piper.mta.MtaMultiplexer
|
|
|
|
|
|
|
|
import groovy.transform.Field
|
|
|
|
|
2018-11-29 10:54:05 +02:00
|
|
|
@Field def STEP_NAME = getClass().getName()
|
2018-06-25 13:14:46 +02:00
|
|
|
|
|
|
|
@Field Set GENERAL_CONFIG_KEYS = ['snykCredentialsId']
|
|
|
|
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus([
|
|
|
|
'buildDescriptorFile',
|
|
|
|
'dockerImage',
|
2018-06-26 16:08:03 +02:00
|
|
|
'exclude',
|
2018-06-25 13:14:46 +02:00
|
|
|
'monitor',
|
|
|
|
'scanType',
|
|
|
|
'snykOrg',
|
2018-06-26 15:47:46 +02:00
|
|
|
'toJson',
|
|
|
|
'toHtml'
|
2018-06-25 13:14:46 +02:00
|
|
|
])
|
|
|
|
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
|
|
|
|
|
2018-08-30 16:33:07 +02:00
|
|
|
void call(Map parameters = [:]) {
|
2018-06-26 13:17:12 +02:00
|
|
|
handlePipelineStepErrors(stepName: STEP_NAME, stepParameters: parameters) {
|
2018-06-25 13:14:46 +02:00
|
|
|
def utils = parameters.juStabUtils ?: new Utils()
|
2018-09-21 16:55:31 +02:00
|
|
|
|
2018-10-31 09:40:12 +02:00
|
|
|
def script = checkScript(this, parameters) ?: this
|
2018-06-25 13:14:46 +02:00
|
|
|
|
2018-10-17 11:05:20 +02:00
|
|
|
Map config = ConfigurationHelper.newInstance(this)
|
2018-09-07 10:08:16 +02:00
|
|
|
.loadStepDefaults()
|
2018-06-25 13:14:46 +02:00
|
|
|
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
|
|
|
|
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
|
2018-08-15 10:37:34 +02:00
|
|
|
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, STEP_CONFIG_KEYS)
|
2018-06-25 13:14:46 +02:00
|
|
|
.mixin(parameters, PARAMETER_KEYS)
|
|
|
|
// check mandatory paramerers
|
2018-06-26 13:18:21 +02:00
|
|
|
.withMandatoryProperty('dockerImage')
|
|
|
|
.withMandatoryProperty('snykCredentialsId')
|
2018-06-25 13:14:46 +02:00
|
|
|
.use()
|
|
|
|
|
2019-01-21 09:47:34 +02:00
|
|
|
new Utils().pushToSWA([
|
|
|
|
step: STEP_NAME,
|
|
|
|
stepParamKey1: 'scriptMissing',
|
|
|
|
stepParam1: parameters?.script == null
|
|
|
|
], config)
|
2018-08-09 11:35:33 +02:00
|
|
|
|
2018-06-25 13:14:46 +02:00
|
|
|
utils.unstashAll(config.stashContent)
|
|
|
|
|
|
|
|
switch(config.scanType) {
|
|
|
|
case 'mta':
|
|
|
|
def scanJobs = [failFast: false]
|
|
|
|
// create job for each package.json with scanType: 'npm'
|
|
|
|
scanJobs.putAll(MtaMultiplexer.createJobs(
|
2018-06-26 16:08:03 +02:00
|
|
|
this, parameters, config.exclude, 'Snyk', 'package.json', 'npm'
|
2018-06-26 15:34:32 +02:00
|
|
|
){options -> snykExecute(options)})
|
2018-06-25 13:14:46 +02:00
|
|
|
// execute scan jobs in parallel
|
|
|
|
parallel scanJobs
|
|
|
|
break
|
|
|
|
case 'npm':
|
|
|
|
// set default file for scanType
|
|
|
|
def path = config.buildDescriptorFile.replace('package.json', '')
|
|
|
|
try{
|
|
|
|
withCredentials([string(
|
|
|
|
credentialsId: config.snykCredentialsId,
|
|
|
|
variable: 'token'
|
|
|
|
)]) {
|
|
|
|
dockerExecute(
|
2018-11-28 11:46:47 +02:00
|
|
|
script: script,
|
2018-06-25 13:14:46 +02:00
|
|
|
dockerImage: config.dockerImage,
|
|
|
|
stashContent: config.stashContent,
|
|
|
|
dockerEnvVars: ['SNYK_TOKEN': token]
|
|
|
|
) {
|
|
|
|
// install Snyk
|
2018-06-26 15:08:46 +02:00
|
|
|
sh 'npm install snyk --global --quiet'
|
2018-06-26 15:47:46 +02:00
|
|
|
if(config.toHtml){
|
|
|
|
config.toJson = true
|
|
|
|
sh 'npm install snyk-to-html --global --quiet'
|
|
|
|
}
|
2018-06-25 13:14:46 +02:00
|
|
|
// install NPM dependencies
|
|
|
|
sh "cd '${path}' && npm install --quiet"
|
|
|
|
// execute Snyk scan
|
|
|
|
def cmd = []
|
|
|
|
cmd.push("cd '${path}'")
|
|
|
|
if(config.monitor) {
|
|
|
|
cmd.push('&& snyk monitor')
|
|
|
|
if(config.snykOrg)
|
|
|
|
cmd.push("--org=${config.snykOrg}")
|
|
|
|
}
|
|
|
|
cmd.push('&& snyk test')
|
|
|
|
if(config.toJson)
|
2018-06-26 16:05:43 +02:00
|
|
|
cmd.push("--json > snyk.json")
|
2018-06-26 16:00:26 +02:00
|
|
|
try{
|
|
|
|
sh cmd.join(' ')
|
|
|
|
}finally{
|
2018-06-26 16:05:43 +02:00
|
|
|
if(config.toHtml) sh "snyk-to-html -i ${path}snyk.json -o ${path}snyk.html"
|
2018-06-26 16:00:26 +02:00
|
|
|
}
|
2018-06-25 13:14:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}finally{
|
2018-06-26 16:05:43 +02:00
|
|
|
if(config.toJson) archiveArtifacts "${path.replaceAll('\\./', '')}snyk.json"
|
|
|
|
if(config.toHtml) archiveArtifacts "${path.replaceAll('\\./', '')}snyk.html"
|
2018-06-25 13:14:46 +02:00
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
error "[ERROR][${STEP_NAME}] ScanType '${config.scanType}' not supported!"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|