Accept SSLContext into SSLConfig(verify=...) (#215)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Seth Michael Larson · committed by GitHub · parent af907e8 · Browse files · 2 files·+37 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+37 added−14 removed
- tests:+9 added−0 removed
- other:+28 added−14 removed
Provenance
- Commit
df8874b- Tree
a922d69- Author
Seth Michael Larson <sethmichaellarson@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
af907e8
2 files changed+37 added−14 removed
modified:httpx/config.py+28 added-14 removed
5 unmodified lines6 7 from .__version__ import __version__8 9 CertTypes = typing.Union[str, typing.Tuple[str, str], typing.Tuple[str, str, str]]10-VerifyTypes = typing.Union[str, bool]10+VerifyTypes = typing.Union[str, bool, ssl.SSLContext]11 TimeoutTypes = typing.Union[float, typing.Tuple[float, float, float], "TimeoutConfig"]12 13 14 USER_AGENT = f"python-httpx/{__version__}"24 unmodified lines39 """40 41 def __init__(self, *, cert: CertTypes = None, verify: VerifyTypes = True):42 self.cert = cert43- self.verify = verify43 45- self.ssl_context: typing.Optional[ssl.SSLContext] = None44+ # Allow passing in our own SSLContext object that's pre-configured.45+ # If you do this we assume that you want verify=True as well.46+ ssl_context = None47+ if isinstance(verify, ssl.SSLContext):48+ ssl_context = verify49+ verify = True50+ self._load_client_certs(ssl_context)51 52+ self.ssl_context: typing.Optional[ssl.SSLContext] = ssl_context53+ self.verify: typing.Union[str, bool] = verify54+55 def __eq__(self, other: typing.Any) -> bool:56 return (57 isinstance(other, self.__class__)58 and self.cert == other.cert69 unmodified lines128 context.load_verify_locations(cafile=str(ca_bundle_path))129 elif ca_bundle_path.is_dir():130 context.load_verify_locations(capath=str(ca_bundle_path))131 124- if self.cert is not None:125- if isinstance(self.cert, str):126- context.load_cert_chain(certfile=self.cert)127- elif isinstance(self.cert, tuple) and len(self.cert) == 2:128- context.load_cert_chain(certfile=self.cert[0], keyfile=self.cert[1])129- elif isinstance(self.cert, tuple) and len(self.cert) == 3:130- context.load_cert_chain(131- certfile=self.cert[0],132- keyfile=self.cert[1],133- password=self.cert[2], # type: ignore134- )132+ self._load_client_certs(context)133 134 return context135 136 def _create_default_ssl_context(self) -> ssl.SSLContext:15 unmodified lines152 context.set_npn_protocols(["h2", "http/1.1"])153 154 return context155 156+ def _load_client_certs(self, ssl_context: ssl.SSLContext) -> None:157+ """158+ Loads client certificates into our SSLContext object159+ """160+ if self.cert is not None:161+ if isinstance(self.cert, str):162+ ssl_context.load_cert_chain(certfile=self.cert)163+ elif isinstance(self.cert, tuple) and len(self.cert) == 2:164+ ssl_context.load_cert_chain(certfile=self.cert[0], keyfile=self.cert[1])165+ elif isinstance(self.cert, tuple) and len(self.cert) == 3:166+ ssl_context.load_cert_chain(167+ certfile=self.cert[0],168+ keyfile=self.cert[1],169+ password=self.cert[2], # type: ignore170+ )171 172+173 class TimeoutConfig:174 """175 Timeout values.176 """86 unmodified linesmodified:tests/test_config.py+9 added
74 unmodified lines75 assert context.verify_mode == ssl.VerifyMode.CERT_NONE76 assert context.check_hostname is False77 78 79+def test_load_ssl_context():80+ ssl_context = ssl.create_default_context()81+ ssl_config = httpx.SSLConfig(verify=ssl_context)82+83+ assert ssl_config.verify is True84+ assert ssl_config.ssl_context is ssl_context85+ assert repr(ssl_config) == "SSLConfig(cert=None, verify=True)"86+87+88 def test_ssl_repr():89 ssl = httpx.SSLConfig(verify=False)90 assert repr(ssl) == "SSLConfig(cert=None, verify=False)"91 42 unmodified lines