Search code examples
pythonmacosssltruststore

How to make Python use CA certificates from Mac OS TrustStore?


I need to use custom root certificates on the company intranet and loading them in the Mac OS TrustStore (KeyChain) does solve the problem for all browsers and GUI apps.

It seems that it works even with the version of curl that ships with Mac OS X but it doesn't work with python, even the version that ships with Mac OS 10.12 Sierra (Python 2.7.10)

Still, it seems that I would be hit by:

urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>

How can I solve this?

Because I encounter this issue in lots and lots of Python tools. I would really appreciate if I find a way to avoid it without having to patch them.

Providing the custom CA certificate myself is not an option because I cannot patch tens of Python tools that I use.

Most of the tools are using the requests library but, there are a few that are using the native ssl support in Python directly.


Solution

  • If you put the additional certificates in a PEM bundle file you can use these two environment variables to overwrite the default cert stores used by Python openssl and requests.

    SSL_CERT_FILE=/System/Library/OpenSSL/cert.pem
    REQUESTS_CA_BUNDLE=/System/Library/OpenSSL/cert.pem
    

    Please note that this file does not exist, you need to build it yourself.