Skip to content

PYTHON-5849 Fix OCSP and pyOpenSSL context compatibility with pyOpenSSL 26.2.0#2832

Merged
blink1073 merged 6 commits into
mongodb:masterfrom
blink1073:PYTHON-5849
Jun 2, 2026
Merged

PYTHON-5849 Fix OCSP and pyOpenSSL context compatibility with pyOpenSSL 26.2.0#2832
blink1073 merged 6 commits into
mongodb:masterfrom
blink1073:PYTHON-5849

Conversation

@blink1073
Copy link
Copy Markdown
Member

@blink1073 blink1073 commented Jun 1, 2026

PYTHON-5849

Changes in this PR

Update pymongo/ocsp_support.py and pymongo/pyopenssl_context.py to be compatible with pyOpenSSL 26.2.0, which removed X509 extension APIs and added a restriction against mutating an SSL.Context after a Connection has been created from it.

Test Plan

  • Existing test/test_pyopenssl_context.py unit tests all pass.
  • just typing (mypy + pyright) reports no issues.
  • Full validation requires the Evergreen OCSP test suite.

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s).

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

…SL 26.2.0

- In _ocsp_callback, use get_peer_certificate(as_cryptography=True) and
  get_verified_chain(as_cryptography=True) to work directly with
  cryptography.x509.Certificate objects, avoiding pyOpenSSL X509 APIs
  removed in 26.2.0 (get_extension, get_extensions, X509Extension).
- In SSLContext, cache options in self._options to avoid calling
  set_options() after a Connection is created; pyOpenSSL 26.2.0 raises
  ValueError for any Context mutation after first Connection creation.
- Remove the now-unnecessary verify callback from __set_verify_mode;
  it has been optional since pyOpenSSL 20.0 and our minimum is 26.0.0.
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pymongo/ocsp_support.py 60.00% 0 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@blink1073
Copy link
Copy Markdown
Member Author

Linkcheck failure is unrelated.

@blink1073 blink1073 marked this pull request as ready for review June 1, 2026 19:45
@blink1073 blink1073 requested a review from a team as a code owner June 1, 2026 19:45
@blink1073 blink1073 requested a review from sleepyStick June 1, 2026 19:45
Comment thread test/test_pyopenssl_context.py Outdated
@blink1073 blink1073 requested a review from sleepyStick June 1, 2026 19:58
Comment thread test/test_pyopenssl_context.py Outdated
"""Unit tests for _ocsp_callback using a mocked SSL Connection."""

def _make_callback_data(self):
from pymongo.pyopenssl_context import _CallbackData
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(sorry to be pedantic but i think this could also be merged with the import at the top of the file?)

Comment thread test/test_pyopenssl_context.py Outdated

@unittest.skipUnless(_HAVE_PYOPENSSL, "PyOpenSSL is not available.")
def test_returns_false_when_peer_cert_is_none(self):
from pymongo.ocsp_support import _ocsp_callback
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait so why is this down here? (given that its still here i'm guessing there's a genuine reason and i'm being silly for not knowing it atm)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're all at the top now

@blink1073 blink1073 requested a review from sleepyStick June 1, 2026 20:48
sleepyStick
sleepyStick previously approved these changes Jun 1, 2026
@blink1073
Copy link
Copy Markdown
Member Author

The test is still failing, putting this back into draft until Evergreen passes.

@blink1073 blink1073 marked this pull request as draft June 1, 2026 21:20
…6.2.0 compat

service_identity < 24.2.0 uses X509.get_extension(i) to iterate certificate
extensions, which was removed in pyOpenSSL 26.2.0. The min_deps variant
installed service_identity==23.1.0 alongside pyopenssl==26.2.0, causing
AttributeError during verify_hostname after the TLS handshake.
@blink1073 blink1073 marked this pull request as ready for review June 1, 2026 22:21
@blink1073 blink1073 requested a review from sleepyStick June 1, 2026 22:21
Copy link
Copy Markdown
Contributor

@sleepyStick sleepyStick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay!

@blink1073 blink1073 merged commit fe2254e into mongodb:master Jun 2, 2026
89 checks passed
@blink1073 blink1073 deleted the PYTHON-5849 branch June 2, 2026 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants