sphinx-build使用-blinkcheck和自定义CA

时间:2018-06-01 10:12:30

标签: python python-sphinx certificate-authority local-security-authority

我们拥有自己的公司范围的证书颁发机构,用于签署SSL证书。大多数情况下,只要您拥有自己的操作系统(在我们的情况下为CentOS 7)注册该权限,这种方法就可以正常工作。它存储在这里:

/etc/pki/ca-trust/source/anchors/company_ca.pem

这允许Firefox / chrome信任通过它签名的SSL证书。

我正在使用sphinx-build -W -blinkcheck […]检查我的Python项目中的链接是否仍然有效,因为文档中的链接腐烂很糟糕。这适用于所有外部链接。

然而,当链接到我们自己的SSL版本的螳螂(一个bug追踪器)时,我得到了一个

SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:579)'),)))

错误。在我们的设置中,Mantis只能在https上运行。

如何告诉sphinx添加公司范围的权限?

我通常通过tox运行它,如此:

执行此操作的tox fragement:

[testenv:docs]
basepython=python2.7
deps=-r{toxinidir}/requirements/requirements.txt
commands=./check_docs.bash

bash脚本:

#!/bin/bash
set -eux
sphinx-apidoc --force --separate --private --module-first -o docs src/ '*/*test*'
cd docs
pytest --maxfail=1 \
    --tb=line \
    -v \
    --junitxml=junit_sphinx.xml \
    --exitfirst \
    --failed-first \
    --full-trace \
    -ra \
    --capture=no \
    check_sphinx.py

蟒蛇脚本:

import subprocess


def test_linkcheck(tmpdir):
    doctrees = tmpdir.join("doctrees")
    htmldir = tmpdir.join("html")
    subprocess.check_call([
        "sphinx-build", "-W", "-blinkcheck", "-d",
        str(doctrees), ".",
        str(htmldir)
    ])


def test_build_docs(tmpdir):
    doctrees = tmpdir.join("doctrees")
    htmldir = tmpdir.join("html")
    subprocess.check_call([
        "sphinx-build", "-W", "-bhtml", "-d",
        str(doctrees), ".",
        str(htmldir)
    ])

1 个答案:

答案 0 :(得分:0)

Sphinx使用requests使用certifi - 感谢sraw,他在评论中指出了这一点。您可以修改certifi.where()以包含您自己的证书颁发机构。

因为您可能会运行tox或重新构建虚拟环境,所以手动执行此操作非常繁琐且容易出错。夹具使这更容易处理。

Python脚本更改为以下内容。

# -*- coding: utf-8 -*-
import subprocess
import certifi
import requests
import pytest

CA = '/etc/pki/ca-trust/source/anchors/company_ca.pem'


@pytest.fixture
def certificate_authority(scope="module"):
    try:
        # Checking connection to Mantis…
        requests.get('https://mantisbt.example.com')
        # Connection to Mantis OK, thus CA should work fine.
    except requests.exceptions.SSLError:
        # SSL Error. Adding custom certs to Certifi store…
        cafile = certifi.where()
        with open(CA, 'rb') as infile:
            customca = infile.read()
        with open(cafile, 'ab') as outfile:
            outfile.write(customca)
        # That might have worked.


def test_linkcheck(certificate_authority, tmpdir):
    doctrees = tmpdir.join("doctrees")
    htmldir = tmpdir.join("html")
    subprocess.check_call([
        "sphinx-build", "-W", "-blinkcheck", "-d",
        str(doctrees), ".",
        str(htmldir)
    ])


def test_build_docs(certificate_authority, tmpdir):
    doctrees = tmpdir.join("doctrees")
    htmldir = tmpdir.join("html")
    subprocess.check_call([
        "sphinx-build", "-W", "-bhtml", "-d",
        str(doctrees), ".",
        str(htmldir)
    ])