Curl works but python request fails with SSLError

Viewed 636

The following curl cmd works as is, without any issues,
curl -H "Authorization: Bearer $AUTH" --cacert "/var/lib/myapp/server-ca.crt" https://myapp.common:2567/service -X GET

Implementing this in python,

headers = {"Authorization": "Bearer {}".format(os.getenv("AUTH"))}
cacert = "/var/lib/myapp/server-ca.crt"
url = "https://myapp.common:2567/service"
response = requests.get(url=url, headers=headers, verify=cacert)

However it fails with the following error

HTTPSConnectionPool(host='myapp.common', port=2567): Max retries exceeded with url: /service
 (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)'),))

For context the cacert here has the following info:
---BEGIN CERTIFCATE---- SOMERANDOMSTRING ---END CERTIFICATE----

1 Answers

Turns out the issue was with the certificate. I was having self signed certificates which were not present in the OS trust store.

Python requests need the path to full chain cert not just intermediate cert for verify parameter. See requests documentation: SSL Cert Verification

After updating it, it worked without any issues.

Related