I implemented the same in the Jenkins pipeline. However, I only applied it after configuring the Context file for authentication manually (using ZAP GUI). After that, I integrated the full scan using Docker ZAP with the configured Context file. Here is the Jenkins pipeline for the Docker ZAP scan. Apart from that, I followed the video for configuring authentication (Context file):
https://www.youtube.com/watch?v=BOlalxfdLbU&authuser=0
I hope it will work for you.
-----------------------------------------------------------------------------------------------------------------
pipeline {
agent any
parameters {
string(
defaultValue: '<url_for_scan>',
description: 'Target URL to scan',
name: 'TARGET'
)
booleanParam(
defaultValue: true,
description: 'Parameter to know if you want to generate a report.',
name: 'GENERATE_REPORT'
)
}
stages {
stage('Setting up ZAP docker container') {
steps {
echo 'Starting container --> Start'
sh 'docker run -dt --name zapscan zaproxy/zap-stable /bin/bash'
echo 'Starting container --> End'
}
}
stage('Prepare wrk directory') {
when {
environment name: 'GENERATE_REPORT', value: 'true'
}
steps {
script {
// Create a Folder /zap/wrk then copy context file to Folder.
sh '''
docker exec
zapscan \
mkdir -p /zap/wrk
docker cp ${WORKSPACE}/<context file>
zapscan:/path/to/<context file>
'''
}
}
}
stage('Scanning target on ZAP container - FULL SCAN') {
steps {
script {
sh """
docker exec
zapscan \
zap-full-scan.py \
-t $target \
-n ./<context file> \
-U <name of user> \
-r ./report.html \
-I
"""
}
}
}
stage('Copy Report to Workspace') {
steps {
script {
sh '''
echo Workspace la: ${WORKSPACE}
docker cp
zapscan:/zap/wrk/report.html ${WORKSPACE}/report.html
'''
}
}
}
post {
always {
echo 'Removing container'
sh '''
docker stop
zapscan
docker rm
zapscan
'''
cleanWs() //clean workspace after using
}
}
}
}
-----------------------------------------------------------------------------------------------------------------