2
votes

I want to run ZAP as a proxy in my pipeline, and run my selenium tests through the proxy. Im just using curl in a container in place of selenium for my testing and was able to make this work locally using docker.

In my pipeline, zap starts up, but the pipeline just sits in the zap container after that, never progressing to the second container. I understand why, Ive launched a process as a daemon, its never going to finish, so the step never finished. I just dont understand how to accomplish what I need in jenkins.

stage('Run Zap Proxy'){
        docker.image('owasp/zap2docker-weekly').withRun('-p 8090:8090') { c ->
            docker.image('owasp/zap2docker-weekly').inside("-v $WORKSPACE:/zap/wrk:rw") {
                /* Wait until mysql service is up */
                sh """
                   zap.sh -daemon -port 8090 -host 0.0.0.0 -newsession testing -config api.addrs.addr.name=.* -config api.addrs.addr.regex=true -config api.disablekey=true
               """
            }
            docker.image('cfmanteiga/alpine-bash-curl-jq').inside("--link ${c.id}:proxy") {
                sh 'curl -k -x http://proxy:8090 https://my.fqdn.net'
                sh """
                   curl -k -x http://proxy:8090 \
                       -X POST https://my.fqdn.net/api/rest/sessions \
                       -H 'Content-Type: application/json' \
                       -H 'Accept: application/json' \
                       -d '{"username":"username","password":"password"}'
               """
                sh 'sleep 2m'
                sh 'curl -o report.html http://zap/UI/core/other/htmlreport'
                stash includes: 'report.html', name: 'report'
            }
        }
}

I essentially need to start zap with the command im using in the 'inside', and only kill the container when the second containers stages are complete.

1

1 Answers

0
votes

You could directly pass the zap command in the withRun part:

stage('Run Zap Proxy'){
    docker.image('owasp/zap2docker-weekly').withRun('-p 8090:8090 -v $WORKSPACE:/zap/wrk:rw', 'zap.sh -daemon -port 8090 -host 0.0.0.0 -newsession testing -config api.addrs.addr.name=.* -config api.addrs.addr.regex=true -config api.disablekey=true') { c ->
        docker.image('cfmanteiga/alpine-bash-curl-jq').inside("--link ${c.id}:proxy") {
            sh 'curl -k -x http://proxy:8090 https://my.fqdn.net'
            sh """
               curl -k -x http://proxy:8090 \
                   -X POST https://my.fqdn.net/api/rest/sessions \
                   -H 'Content-Type: application/json' \
                   -H 'Accept: application/json' \
                   -d '{"username":"username","password":"password"}'
           """
            sh 'sleep 2m'
            sh 'curl -o report.html http://zap/UI/core/other/htmlreport'
            stash includes: 'report.html', name: 'report'
        }
    }
}

withRun allows you to overwrite the CMD of the zap-container. Check this API-documentation.