Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Certificate verification when using virtual environments

I have a root CA certificate installed on my machine and all is fine when issuing a requests when using the system install of the requests library:

$ python -c 'import requests; print requests.get("https://example.com")'
<Response [200]>

However if I issue the same request from within a virtual environment the certificate verification fails:

$ python -c 'import requests; print requests.get("https://example.com")'
requests.exceptions.SSLError: [Errno 1] _ssl.c:510: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

Using requests.certs.where I can see the system install uses the systems CA bundle and the virtual environment uses the CA bundle shipped with requests:

$ python -c "import requests; print requests.certs.where()"
/etc/ssl/certs/ca-certificates.crt

$ (venv) python -c "import requests; print requests.certs.where()"                                                            
.../venv/local/lib/python2.7/site-packages/requests/cacert.pem

Is there another solution to picking up the system certs without providing the path on each request when using virtualenv, i.e:

>>> requests.get("https://example.com" verify="/etc/ssl/certs/ca-certificates.crt")
like image 306
Chris Seymour Avatar asked Jan 21 '16 18:01

Chris Seymour


People also ask

What happens when you activate a virtual environment?

Activating a virtual environment Before you can start installing or using packages in your virtual environment you'll need to activate it. Activating a virtual environment will put the virtual environment-specific python and pip executables into your shell's PATH .

How do I disable SSL certificate validation in python?

Method 1: Passing verify=False to request method Along with the URL also pass the verify=False parameter to the method in order to disable the security checks.

Why is it good to use a virtual environment for a project?

A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. This is one of the most important tools that most of the Python developers use.


1 Answers

If you want to insert a specific CA certificate to your CA bundle of virtualenv, you can just append to it:

openssl x509 -in $specific_ca.crt -text >> $virtualenv/lib/python2.7/site-packages/certifi/cacert.pem
like image 172
gma Avatar answered Sep 19 '22 12:09

gma