1

I am new to python and locust, trying to run my first locust perf script to test an API However, I am getting error as ssl.SSLError: [SSL] PEM lib (_ssl.c:4065) for following code.

class MyHttpTest(HttpUser):
wait_time = constant(5)
host = "https://abc.myorg.net"
headers = {
    'Content-Type': "application/json",
    'Ocp-Apim-Trace': "true",
    'Ocp-Apim-Subscription-Key': "<APIM subscription Key>",
    'Cache-Control': "no-cache"
}
payload = "{\n\t\"name\": \"abc\"\n}"

@task
def get_status(self):

    self.client.headers = self.headers
    self.client.verify = False

    self.client.post("/uam/Link/Get/v1", data=self.payload,
                     cert=("<path to pfx certificate>",
                           "<password of pfx certificate"))
Ajay
  • 43
  • 1
  • 7

2 Answers2

3

Check these out:

How to configure Locust to use https?

What does "SSLError: [SSL] PEM lib (_ssl.c:2532)" mean using the Python ssl library?

Pretty sure the problem is the .pfx file you're trying to use as your cert. Locust's HttpUser's client is based on Requests. I'm currently unable to find anything that says Requests can work with .pfx files directly. You need to have a .cert or .pem format. I haven't tried it, but I found a gist that says it lets you use .pfx with Requests so it ought to work with Locust, I'd imagine.

https://gist.github.com/erikbern/756b1d8df2d1487497d29b90e81f8068

Solowalker
  • 2,431
  • 8
  • 13
  • This helps and yes I was trying to use pfx as cert. I was actually able to solve this problem by extracting cert and key from pfx using openssl command but I think this solution you shared is better. – Ajay Oct 19 '21 at 05:20
0

I overcame this by adding certificate to cacert.pem storage, that is a part of certifi lib which goes with requests library

import certifi
import requests

try:
    print('Checking connection to Github...')
    test = requests.get('https://api.github.com')
    print('Connection to Github OK.')
except requests.exceptions.SSLError as err:
    print('SSL Error. Adding custom certs to Certifi store...')
    cafile = certifi.where()
    with open('certicate.pem', 'rb') as infile:
        customca = infile.read()
    with open(cafile, 'ab') as outfile:
        outfile.write(customca)
    print('That might have worked.')

The exapmple is taken from here

  • 1
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](https://meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. Answers that are little more than a link may be deleted – cursorrux Apr 15 '22 at 19:00