Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PYTHON-4492 Fallback to stdlib ssl when pyopenssl import fails with AttributeError #1669

Merged
merged 1 commit into from
Jun 12, 2024
Merged

PYTHON-4492 Fallback to stdlib ssl when pyopenssl import fails with AttributeError #1669

merged 1 commit into from
Jun 12, 2024

Conversation

oh2fih
Copy link
Contributor

@oh2fih oh2fih commented Jun 11, 2024

Catch AttributeError caused by incompatibility between PyOpenSSL < 23.2.0 & cryptography >= 42.0.0.

In a situation where incompatible versions are already installed on the system (regardless pip install "pymongo[ocsp]" as noticed in https://jira.mongodb.org/browse/PYTHON-4491), the user now gets an unhandled exception with a stack trace:

  File "/home/user/.local/lib/python3.10/site-packages/pymongo/ssl_support.py", line 24, in <module>
    import pymongo.pyopenssl_context as _ssl
  File "/home/user/.local/lib/python3.10/site-packages/pymongo/pyopenssl_context.py", line 29, in <module>
    from OpenSSL import SSL as _SSL
  File "/usr/lib/python3/dist-packages/OpenSSL/__init__.py", line 8, in <module>
    from OpenSSL import crypto, SSL
  File "/usr/lib/python3/dist-packages/OpenSSL/crypto.py", line 1579, in <module>
    class X509StoreFlags(object):
  File "/usr/lib/python3/dist-packages/OpenSSL/crypto.py", line 1598, in X509StoreFlags
    NOTIFY_POLICY = _lib.X509_V_FLAG_NOTIFY_POLICY
AttributeError: module 'lib' has no attribute 'X509_V_FLAG_NOTIFY_POLICY'. Did you mean: 'X509_V_FLAG_EXPLICIT_POLICY'?

The added exception handling tries to fix this problem by falling back to stdlib SSL as would happen if the PyOpenSSL was not installed at all. Additionally, the stack trace is replaced with a user-friendly warning suggesting a possible solution in case the OCSP support is desired.

Ref. cve-search/cve-search#1099 (comment), conda/conda#13619 (comment) & #1666.

@ShaneHarvey ShaneHarvey changed the title Handle incompatible PyOpenSSL & cryptography versions PYTHON-4492 Fallback to stdlib ssl when pyopenssl import fails with AttributeError Jun 11, 2024
pymongo/ssl_support.py Outdated Show resolved Hide resolved
@oh2fih oh2fih requested a review from ShaneHarvey June 11, 2024 18:17
pymongo/ssl_support.py Outdated Show resolved Hide resolved
@oh2fih oh2fih requested a review from ShaneHarvey June 11, 2024 18:37
@ShaneHarvey
Copy link
Member

Thanks @oh2fih! I scheduled the CI checks now.

pymongo/ssl_support.py Outdated Show resolved Hide resolved
AttributeError caused by incompatibility between PyOpenSSL < 23.2.0 & cryptography >= 42.0.0.
@ShaneHarvey ShaneHarvey merged commit 4ec79fb into mongodb:master Jun 12, 2024
32 of 34 checks passed
ShaneHarvey pushed a commit to ShaneHarvey/mongo-python-driver that referenced this pull request Jun 12, 2024
@oh2fih oh2fih deleted the catch-pyopenssl-attribute-error branch June 13, 2024 03:51
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