Skip to content

Commit 043c8e8

Browse files
committed
PYTHON-2140 Test PyOpenSSL on macOS
Handle the case where the peer omits the self-signed issuer cert and OCSP is not requested by delaying issuer check. Properly set PYMONGO_MUST_CONNECT in PyOpenSSL tests. Properly set PYTHON_BINARY in OCSP test.
1 parent e26dc96 commit 043c8e8

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

.evergreen/config.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ functions:
347347
script: |
348348
set -o xtrace
349349
${PREPARE_SHELL}
350+
if [ -n "${MONGODB_STARTED}" ]; then
351+
export PYMONGO_MUST_CONNECT=1
352+
fi
350353
PYTHON_BINARY=${PYTHON_BINARY} sh ${PROJECT_DIRECTORY}/.evergreen/run-pyopenssl-tests.sh
351354
352355
"run doctests":
@@ -662,6 +665,7 @@ functions:
662665
working_dir: "src"
663666
script: |
664667
${PREPARE_SHELL}
668+
PYTHON_BINARY=${PYTHON_BINARY} \
665669
CA_FILE="$DRIVERS_TOOLS/.evergreen/ocsp/rsa/ca.pem" \
666670
OCSP_TLS_SHOULD_SUCCEED="${OCSP_TLS_SHOULD_SUCCEED}" \
667671
sh ${PROJECT_DIRECTORY}/.evergreen/run-ocsp-tests.sh
@@ -1740,6 +1744,15 @@ buildvariants:
17401744
tasks:
17411745
- "pyopenssl"
17421746

1747+
- matrix_name: "tests-pyopenssl-macOS"
1748+
matrix_spec:
1749+
platform: macos-1014
1750+
auth: "*"
1751+
ssl: "ssl"
1752+
display_name: "PyOpenSSL ${platform} ${auth}"
1753+
tasks:
1754+
- "pyopenssl"
1755+
17431756
- matrix_name: "tests-python-version-rhel62-test-encryption"
17441757
matrix_spec:
17451758
platform: rhel62

.evergreen/run-ocsp-tests.sh

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ if [ -z "$PYTHON_BINARY" ]; then
1313
fi
1414

1515
$PYTHON_BINARY -m virtualenv --never-download --no-wheel ocsptest
16-
. ocsptest/bin/activate
17-
trap "deactivate; rm -rf ocsptest" EXIT HUP
18-
pip install pyopenssl requests service_identity
19-
PYTHON=python
16+
. ocsptest/bin/activate
17+
trap "deactivate; rm -rf ocsptest" EXIT HUP
2018

21-
OCSP_TLS_SHOULD_SUCCEED=${OCSP_TLS_SHOULD_SUCCEED} CA_FILE=${CA_FILE} $PYTHON test/ocsp/test_ocsp.py
19+
IS_PYTHON_2=$(python -c "import sys; sys.stdout.write('1' if sys.version_info < (3,) else '0')")
20+
if [ $IS_PYTHON_2 = "1" ]; then
21+
echo "Using a Python 2"
22+
pip install --upgrade 'setuptools<45'
23+
fi
24+
25+
pip install pyopenssl requests service_identity
26+
27+
OCSP_TLS_SHOULD_SUCCEED=${OCSP_TLS_SHOULD_SUCCEED} CA_FILE=${CA_FILE} python test/ocsp/test_ocsp.py

.evergreen/run-pyopenssl-tests.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ else
1919
fi
2020

2121
$PYTHON -m virtualenv pyopenssltest
22-
trap "deactivate; rm -rf pyopenssltest" EXIT HUP
2322
. pyopenssltest/bin/activate
24-
pip install pyopenssl>=17.2.0 "requests<3.0.0" service_identity>=18.1.0
25-
pip list
23+
trap "deactivate; rm -rf pyopenssltest" EXIT HUP
24+
25+
IS_PYTHON_2=$(python -c "import sys; sys.stdout.write('1' if sys.version_info < (3,) else '0')")
26+
if [ $IS_PYTHON_2 = "1" ]; then
27+
echo "Using a Python 2"
28+
pip install --upgrade 'setuptools<45'
29+
fi
30+
31+
pip install pyopenssl requests service_identity
2632
python -c 'import sys; print(sys.version)'
2733
python setup.py test

pymongo/ocsp_support.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,6 @@ def ocsp_callback(conn, ocsp_bytes, user_data):
237237
cert = conn.get_peer_certificate().to_cryptography()
238238
chain = [cer.to_cryptography() for cer in conn.get_peer_cert_chain()]
239239
issuer = _get_issuer_cert(cert, chain)
240-
if issuer is None:
241-
_LOGGER.debug("No issuer cert?")
242-
return 0
243240
must_staple = False
244241
# https://tools.ietf.org/html/rfc7633#section-4.2.3.1
245242
ext = _get_extension(cert, _TLSFeature)
@@ -268,6 +265,9 @@ def ocsp_callback(conn, ocsp_bytes, user_data):
268265
_LOGGER.debug("No OCSP URI, soft fail")
269266
# No responder URI, soft fail.
270267
return 1
268+
if issuer is None:
269+
_LOGGER.debug("No issuer cert?")
270+
return 0
271271
_LOGGER.debug("Requesting OCSP data")
272272
# When requesting data from an OCSP endpoint we only fail on
273273
# successful, valid responses with a certificate status of REVOKED.
@@ -291,6 +291,9 @@ def ocsp_callback(conn, ocsp_bytes, user_data):
291291
return 1
292292

293293
_LOGGER.debug("Peer stapled an OCSP response")
294+
if issuer is None:
295+
_LOGGER.debug("No issuer cert?")
296+
return 0
294297
response = _load_der_ocsp_response(ocsp_bytes)
295298
_LOGGER.debug(
296299
"OCSP response status: %r", response.response_status)

test/test_ssl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def test_validation_with_system_ca_certs(self):
442442
raise SkipTest("Can't load system CA certificates.")
443443

444444
if (ssl.OPENSSL_VERSION.lower().startswith('libressl') and
445-
sys.platform == 'darwin'):
445+
sys.platform == 'darwin' and not _ssl.IS_PYOPENSSL):
446446
raise SkipTest(
447447
"LibreSSL on OSX doesn't support setting CA certificates "
448448
"using SSL_CERT_FILE environment variable.")

0 commit comments

Comments
 (0)