Search code examples
restgroovyjenkins-pipelinejenkins-groovymutual-authentication

In Jenkins pipeline, what is the correct way to perform mutual-authentication REST requests?


I am calling a REST service from a Jenkins Pipeline. This REST service uses mutual authentication so I must provide a client certificate to authenticate.

I could store the certificate with my Jenkinsfile in Git - could I then pass this cert to the REST call?

This is performed in cURL with the --cert param:

curl -H "Content-Type: application/json" -H "Accept: application/json" \
--cert ens.p12 \
https://hostname:9000/api/bakery -k

How do we get it so that the cert will be used in the Groovy request?

def get = new URL("https://hostname:9000/api/bakery").openConnection();
get.setRequestMethod("GET")
get.setRequestProperty("Content-Type", "application/json")
get.getInputStream().getText()

Solution

  • Ended up using cURL with sh provided by the Jenkins Pipeline DSL as this seems to handle the requirement in the most succinct way.

    JSON = sh (
      script: 'curl -H "Content-Type: application/json" --cert ens.p12 https://hostname:9000/api/bakery -k',
      returnStdout: true
    ).trim()