Skip to content

PYTHON-4572 - Fix type errors caused by new PyOpenSSL type hints #1743

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

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions pymongo/pyopenssl_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def load_cert_chain(
# Password callback MUST be set first or it will be ignored.
if password:

def _pwcb(_max_length: int, _prompt_twice: bool, _user_data: bytes) -> bytes:
def _pwcb(_max_length: int, _prompt_twice: bool, _user_data: Optional[bytes]) -> bytes:
# XXX:We could check the password length against what OpenSSL
# tells us is the max, but we can't raise an exception, so...
# warn?
Expand Down Expand Up @@ -332,14 +332,17 @@ def _load_certifi(self) -> None:
def _load_wincerts(self, store: str) -> None:
"""Attempt to load CA certs from Windows trust store."""
cert_store = self._ctx.get_cert_store()
oid = _stdlibssl.Purpose.SERVER_AUTH.oid

for cert, encoding, trust in _stdlibssl.enum_certificates(store): # type: ignore
if encoding == "x509_asn":
if trust is True or oid in trust:
cert_store.add_cert(
_crypto.X509.from_cryptography(x509.load_der_x509_certificate(cert))
)
if cert_store is not None:
Copy link
Member

Choose a reason for hiding this comment

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

alternatively we could do this avoid avoid a large code change:

assert cert_store, '...'

oid = _stdlibssl.Purpose.SERVER_AUTH.oid

for cert, encoding, trust in _stdlibssl.enum_certificates(store): # type: ignore
if encoding == "x509_asn":
if trust is True or oid in trust:
cert_store.add_cert(
_crypto.X509.from_cryptography(x509.load_der_x509_certificate(cert))
)
else:
raise _ConfigurationError("The current CA context does not have a X509Store object.")

def load_default_certs(self) -> None:
"""A PyOpenSSL version of load_default_certs from CPython."""
Expand Down
Loading