3

I would like to authenticate to server from my client using certificate that is generated from server.I have a server-ca.crt and below is the CURL command that is working.How to send similar request using python requests module .

$ curl -X GET -u sat_username:sat_password \
-H "Accept:application/json" --cacert katello-server-ca.crt \
https://satellite6.example.com/katello/api/organizations

I have tried following way and it is getting some exception, can someone help in resolving this issue.

 python requestsCert.py
Traceback (most recent call last):
  File "requestsCert.py", line 2, in <module>
    res=requests.get('https://satellite6.example.com/katello/api/organizations', cert='/certificateTests/katello-server-ca.crt', verify=True)
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 68, in get
    return request('get', url, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 464, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 431, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL] PEM lib (_ssl.c:2554)
David Z
  • 128,184
  • 27
  • 255
  • 279
Auto-learner
  • 1,411
  • 7
  • 26
  • 43
  • Closely related: https://stackoverflow.com/questions/30109449/what-does-sslerror-ssl-pem-lib-ssl-c2532-mean-using-the-python-ssl-libr but I'm not sure that's a duplicate since in this question, the error is raised in `requests` code, whereas in the other question, `requests` is not involved. – David Z Nov 18 '17 at 10:04

1 Answers1

4
res=requests.get('https://...', cert='/certificateTests/katello-server-ca.crt', verify=True)

The cert argument in requests.get is used to specify the client certificate and key which should be used for mutual authentication. It is not used to specify the trusted CA as the --cacert argument in curl does. Instead you should use the verify argument:

res=requests.get('https://...', verify='/certificateTests/katello-server-ca.crt')

For more information see SSL Cert Verification and Client Side Certificates in the documentation for requests.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172