首页 > 解决方案 > pip install with cert 工作,但全局配置文件中的引用证书失败

问题描述

我在公司网络后面,目前可以Python通过两种方式安装包Dockerfile

(1) 参考证书

RUN pip3 install --cert=/usr/local/share/ca-certificates/zscaler-root.crt <SOME-PYTHON-PACKAGE>

(2) 通过将 repos/URLs 添加到受信任的主机

RUN touch /etc/pip.conf

RUN echo "[global] \n trusted-host = pypi.python.org pypi.org files.pythonhosted.org" >> /etc/pip.conf

RUN pip3 install <SOME-PYTHON-PACKAGE>

而不是选项#2,我宁愿在全球范围内设置证书。

RUN touch /etc/pip.conf

RUN echo "[global] \n cert=/usr/local/share/ca-certificates/zscaler-root.crt" >> /etc/pip.conf

但这不起作用。

导致 SSLError(SSLCertVerificationError.

WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1091)'))': /simple/flask/

为什么选项 #1 可以引用证书但将其添加到全局配置文件失败?

编辑:

根据@phd 的建议,尝试了以下方法。

RUN pip3 config --global set global.cert /usr/local/share/ca-certificates/zscaler-root.crt

RUN pip3 config set global.cert /usr/local/share/ca-certificates/zscaler-root.crt

但没有喜悦。

还尝试SSL_CERT_DIR按照https://stackoverflow.com/a/24353642/6265370进行设置。

ENV SSL_CERT_DIR="/usr/local/share/ca-certificates/"

再次没有雪茄。

这里有更多信息

pip 21.1.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
        PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
        NAME="Debian GNU/Linux"
        VERSION_ID="9"
        VERSION="9 (stretch)"
        VERSION_CODENAME=stretch
        ID=debian
        HOME_URL="https://www.debian.org/"
        SUPPORT_URL="https://www.debian.org/support"
        BUG_REPORT_URL="https://bugs.debian.org/"
    
        pip3 config list -v
        For variant 'global', will try loading '/etc/xdg/pip/pip.conf'
        For variant 'global', will try loading '/etc/pip.conf'
        For variant 'user', will try loading '/root/.pip/pip.conf'
        For variant 'user', will try loading '/root/.config/pip/pip.conf'
        For variant 'site', will try loading '/usr/local/pip.conf'



标签: python-3.xpip

解决方案


最好在系统范围内安装 ZScaler 证书,而不是限制使用 pip。在 openssl 上的 Linx 发行版中继。首先检查openssl版本和certs目录:

openssl version -d

你会得到类似的东西:

OPENSSLDIR: "/usr/lib/ssl"

然后将证书复制到其中的 certs 目录中。

在这种情况下,它将是:

/usr/lib/ssl/certs

或者,您可以通过 ca trust authority 来完成。在 Ubuntu 中:

sudo mkdir /usr/local/share/ca-certificates/zscaler
sudo cp <certfile.crt> /usr/local/share/ca-certificates/zscaler/
sudo chmod 755 /usr/local/share/ca-certificates/zscaler/
sudo chmod 644 /usr/local/share/ca-certificates/zscaler/<certfile.crt>
sudo update-ca-certificates

推荐阅读