Skip to content
Myr

Add support for SSLKEYLOGFILE (#301)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Skip test if OpenSSL version is lower than 1.1.1 * Use bionic dist for Python 3.8 job * Pass trust_env to SSLConfig use monkeypatch in testing * Don't raise KeyError if SSLKEYLOGFILE is not set * Move trust_env after verify and cert

authored by Tomás Farías · committed by Seth Michael Larson · parent e34d280 · Browse files · 7 files+89 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+89 added−3 removed

  • tests:+31 added−0 removed
  • other:+58 added−3 removed

7 files+89 added−3 removed

modified:docs/environment_variables.md+35 added
modified:httpx/dispatch/connection_pool.py+3 added
82 unmodified lines
83 self,84 *,85 verify: VerifyTypes = True,86 cert: CertTypes = None,87 trust_env: bool = None,88 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,89 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,90 http_versions: HTTPVersionTypes = None,91 backend: ConcurrencyBackend = None,
3 unmodified lines
95 self.timeout = timeout96 self.pool_limits = pool_limits97 self.http_versions = http_versions98 self.is_closed = False99 self.trust_env = trust_env100101 self.keepalive_connections = ConnectionStore()102 self.active_connections = ConnectionStore()103
42 unmodified lines
146 timeout=self.timeout,147 http_versions=self.http_versions,148 backend=self.backend,149 release_func=self.release_connection,150 trust_env=self.trust_env,151 )152 logger.debug(f"new_connection connection={connection!r}")153 else:154 logger.debug(f"reuse_connection connection={connection!r}")
27 unmodified lines
modified:httpx/dispatch/connection.py+2 added-1 removed
31 unmodified lines
32 self,33 origin: typing.Union[str, Origin],34 verify: VerifyTypes = True,35 cert: CertTypes = None,36 trust_env: bool = None,37 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,38 http_versions: HTTPVersionTypes = None,39 backend: ConcurrencyBackend = None,40 release_func: typing.Optional[ReleaseCallback] = None,41 ):42 self.origin = Origin(origin) if isinstance(origin, str) else origin42 self.ssl = SSLConfig(cert=cert, verify=verify)43 self.ssl = SSLConfig(cert=cert, verify=verify, trust_env=trust_env)44 self.timeout = TimeoutConfig(timeout)45 self.http_versions = HTTPVersionConfig(http_versions)46 self.backend = AsyncioBackend() if backend is None else backend47 self.release_func = release_func
92 unmodified lines
modified:httpx/client.py+3 added-1 removed
80 unmodified lines
81 dispatch = WSGIDispatch(app=app)82 else:83 dispatch = ASGIDispatch(app=app)8485 self.trust_env = True if trust_env is None else trust_env8687 if dispatch is None:88 async_dispatch: AsyncDispatcher = ConnectionPool(89 verify=verify,90 cert=cert,91 timeout=timeout,92 http_versions=http_versions,93 pool_limits=pool_limits,94 backend=backend,95 trust_env=self.trust_env,96 )97 elif isinstance(dispatch, Dispatcher):98 async_dispatch = ThreadedDispatcher(dispatch, backend)99 else:
9 unmodified lines
109 self._cookies = Cookies(cookies)110 self.max_redirects = max_redirects111 self.dispatch = async_dispatch112 self.concurrency_backend = backend110 self.trust_env = True if trust_env is None else trust_env113114 @property115 def headers(self) -> Headers:116 return self._headers
894 unmodified lines
modified:httpx/config.py+14 added-1 removed
1import os2import ssl3import typing4from pathlib import Path5
37 unmodified lines
43 """44 SSL Configuration.45 """4646 def __init__(self, *, cert: CertTypes = None, verify: VerifyTypes = True):47 def __init__(48 self,49 *,50 cert: CertTypes = None,51 verify: VerifyTypes = True,52 trust_env: bool = None,53 ):54 self.cert = cert5556 # Allow passing in our own SSLContext object that's pre-configured.57 # If you do this we assume that you want verify=True as well.
4 unmodified lines
62 self._load_client_certs(ssl_context)6364 self.ssl_context: typing.Optional[ssl.SSLContext] = ssl_context65 self.verify: typing.Union[str, bool] = verify66 self.trust_env = trust_env6768 def __eq__(self, other: typing.Any) -> bool:69 return (70 isinstance(other, self.__class__)
102 unmodified lines
173 context.set_alpn_protocols(http_versions.alpn_identifiers)174 if ssl.HAS_NPN: # pragma: no cover175 context.set_npn_protocols(http_versions.alpn_identifiers)176177 if hasattr(context, "keylog_filename"):178 keylogfile = os.environ.get("SSLKEYLOGFILE")179 if keylogfile and self.trust_env:180 context.keylog_filename = keylogfile # type: ignore181182 return context183184 def _load_client_certs(self, ssl_context: ssl.SSLContext) -> None:185 """
151 unmodified lines
modified:tests/test_config.py+31 added
1import ssl2import sys34import pytest56import httpx
164 unmodified lines
171172def test_timeout_from_config_instance():173 timeout = httpx.TimeoutConfig(timeout=5.0)174 assert httpx.TimeoutConfig(timeout) == httpx.TimeoutConfig(timeout=5.0)175176177@pytest.mark.skipif(178 not hasattr(ssl.SSLContext, "keylog_filename"),179 reason="requires OpenSSL 1.1.1 or higher",180)181@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")182def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch):183 with monkeypatch.context() as m:184 m.delenv("SSLKEYLOGFILE", raising=False)185186 ssl_config = httpx.SSLConfig(trust_env=True)187 ssl_config.load_ssl_context()188189 assert ssl_config.ssl_context.keylog_filename is None190191 filename = str(tmpdir.join("test.log"))192193 with monkeypatch.context() as m:194 m.setenv("SSLKEYLOGFILE", filename)195196 ssl_config = httpx.SSLConfig(trust_env=True)197 ssl_config.load_ssl_context()198199 assert ssl_config.ssl_context.keylog_filename == filename200201 ssl_config = httpx.SSLConfig(trust_env=False)202 ssl_config.load_ssl_context()203204 assert ssl_config.ssl_context.keylog_filename is None
modified:.travis.yml+1 added