如何让Python使用Mac OS TrustStore中的CA证书?

时间:2016-11-18 19:20:38

标签: python macos ssl truststore

我需要在公司内部网上使用curtom root证书,并在Mac OS TrustStore(KeyChain)中加载它们确实解决了所有浏览器和GUI应用程序的问题。

它似乎适用于Mac OS X附带的curl版本,但不能与python 一起使用,即使Mac OS附带的版本也是如此10.12 Sierra(Python 2.7.10)

但是,似乎我会受到以下打击:

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

我该如何解决这个问题?

因为我在很多Python工具中遇到这个问题,如果我找到一种方法来避免它而不必修补它,我将非常感激。

我自己提供自定义CA证书不是一种选择,因为我无法修补我使用的数十种Python工具。

大多数工具都使用requests库,但有一些工具直接在Python中使用本机ssl支持。

6 个答案:

答案 0 :(得分:37)

这也是Python 3.6与MacOS Sierrra的问题。我知道你的用例不同。但在调查这个问题时,我偶然发现了这个问题。因此,如果有人也有这篇文章值得一试:

http://www.cdotson.com/2017/01/sslerror-with-python-3-6-x-on-macos-sierra/

简而言之:Python 3.6不再依赖MacOS的openSSL了。它附带了自己的openSSL捆绑,无法访问MacOS的根证书。

您有两种选择:

运行Python 3.6附带的安装命令

cd /Applications/Python\ 3.6/
./Install\ Certificates.command

使用

安装certifi package
pip install certifi

我选择了第一个选项并且有效。

答案 1 :(得分:6)

如果将其他证书放在PEM包文件中,则可以使用这两个环境变量来覆盖Python openssl和请求使用的默认证书库。

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

请注意,此文件不存在,您需要自己构建。

答案 2 :(得分:3)

运行此命令以设置适当的变量。这是这里已经给出的答案的组合。将其放在您的〜/ .bash_profile中以使其永久存在。

CERT_PATH=$(python -m certifi)
export SSL_CERT_FILE=${CERT_PATH}
export REQUESTS_CA_BUNDLE=${CERT_PATH}

答案 3 :(得分:2)

作为更新和数据点,我遇到了在macOS 10.13.4上运行Python 3.7.0的问题:

$ ipython
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import bokeh.sampledata

In [2]: bokeh.sampledata.download()
Using data directory: /Users/me/.bokeh/data

...
SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)

解决问题的说明在/Applications/Python\ 3.7/ReadMe.rtf

遵循建议并运行/Applications/Python\ 3.7/Install\ Certificates.command解决了问题:

从终端:

$ /Applications/Python\ 3.7/Install\ Certificates.command

重新启动IPython ...

$ ipython
>>> import bokeh.sampledata

>>> bokeh.sampledata.download()
Using data directory: /Users/me/.bokeh/data
Downloading: CGM.csv (1589982 bytes)
   1589982 [100.00%]
...

答案 4 :(得分:0)

对我来说/Applications/Python\ 3.6/./Install\ Certificates命令在pip certifi安装中失败。我在Mac High Sierra上并使用python3,因此pip有点失败,我必须改用pip3。

这就是我所做的:

  1. 在shell中手动运行pip3 install --update certify
  2. 从命令脚本中删除安装证书行
  3. 重新运行脚本,一切都很好。

请注意,您最终将在/Library/Frameworks/Python.framework/Versions/3.6/etc/openssl/

中获得cert.pem符号链接。

答案 5 :(得分:0)

Mac brew安装python env。

$ python3
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import certifi
>>> certifi.where()
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/certifi/cacert.pem'
>>> 

或从命令行:

$ python -m certifi

然后需要将cacert.pem链接为cert.pem

$ ln -s /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/certifi/cacert.pem cert.pem
$ pwd
/Library/Frameworks/Python.framework/Versions/3.7/etc/openssl

rehash

然后工作正常。

相关问题