karatepem

Karate API call with .pem file


I have an api request which needs a .pem file to be sent with it to get the response. I have configured the same in the Insomnia tool and i am able to get response for that. When i try to setup the same request in karate it fails with the response bad certificate error. Insomnia curl :

curl --request POST \
  --url https://tokengenerator.com:8443/as/token.oauth2 \
  --header 'Cookie: PF=TmfprifldGj2sxCzWs3qYd; PF=TmfprifldGj2sxCzWs3qYd' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --cookie PF=TmfprifldGj2sxCzWs3qYd \
  --data username=username \
  --data password=password \
  --data = \
  --data grant_type=password \
  --data client_id=TOKENCLIENT

and I have added the .pem file in insomnia request insomnia .pem file addition

in karate feature file the request is below:

Background:

    * configure ssl = true

  Scenario: Token Generation - Success Response

    Given url 'https://tokengenerator.com:8443/as/token.oauth2'

    And header Content-Type = 'application/x-www-form-urlencoded'

    And form field client_id = 'TOKENCLIENT'

    And form field grant_type = 'password'

    And form field username = 'username'

    And form field password = 'password'

    When method POST

    Then status 200

In karate config i have added the details for .pem file as:

var config = {
    env: env,

    CERT_FILE: 'src/test/resource/Client.pem',
};

karate.configure('ssl', {
    keyStore: config.CERT_FILE,
    keyStorePassword: 'password',
    keyStoreHost: 'hostname.intranet:8443',
    keyStoreType: 'pkcs12',
    trustAll: true,
    algorithm: 'TLS'
})

Error i am getting in karate is :

ERROR com.intuit.karate - javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate, http call failed after 639 milliseconds for url: https://tokengenerator.com:8443/as/token.oauth2


Solution

  • I changed the .pem file to .p12 and it worked

    var config = {
            env: env,
            TRUST_STORE: 'classpath:keystore.jks',
            CERT_FILE: 'classpath:Client.p12',
        };
    
    karate.configure('ssl', {
        keyStore: config.CERT_FILE,
        keyStorePassword: 'password',
        keyStoreType: 'pkcs12',
        trustAll: true,
        algorithm: 'TLS'
    })