Skip to content
Myr

Run SSLConfig cert loading in threadpool (#209)

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

authored by Can Sarıgöl · committed by Seth Michael Larson · parent bebedcb · Browse files · 3 files+30 added−38 removed
This commit

State

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

Diff stats

3 files+30 added−38 removed

  • tests:+18 added−27 removed
  • other:+12 added−11 removed

3 files+30 added−38 removed

modified:httpx/dispatch/connection.py+6 added-1 removed
64 unmodified lines
65 timeout = self.timeout if timeout is None else TimeoutConfig(timeout)6667 host = self.origin.host68 port = self.origin.port69 ssl_context = await ssl.load_ssl_context() if self.origin.is_ssl else None69 # Run the SSL loading in a threadpool, since it makes disk accesses.70 ssl_context = (71 await self.backend.run_in_threadpool(ssl.load_ssl_context)72 if self.origin.is_ssl73 else None74 )7576 if self.release_func is None:77 on_release = None78 else:
38 unmodified lines
modified:httpx/config.py+6 added-10 removed
1import asyncio1import ssl2import typing3from pathlib import Path4
58 unmodified lines
63 if (cert == self.cert) and (verify == self.verify):64 return self65 return SSLConfig(cert=cert, verify=verify)6668 async def load_ssl_context(self) -> ssl.SSLContext:67 def load_ssl_context(self) -> ssl.SSLContext:68 if self.ssl_context is None:70 if not self.verify:71 self.ssl_context = self.load_ssl_context_no_verify()72 else:73 # Run the SSL loading in a threadpool, since it makes disk accesses.74 loop = asyncio.get_event_loop()75 self.ssl_context = await loop.run_in_executor(76 None, self.load_ssl_context_verify77 )69 self.ssl_context = (70 self.load_ssl_context_verify()71 if self.verify72 else self.load_ssl_context_no_verify()73 )7475 assert self.ssl_context is not None76 return self.ssl_context77
171 unmodified lines
modified:tests/test_config.py+18 added-27 removed
3 unmodified lines
45import httpx678@pytest.mark.asyncio9async def test_load_ssl_config():8def test_load_ssl_config():9 ssl_config = httpx.SSLConfig()11 context = await ssl_config.load_ssl_context()10 context = ssl_config.load_ssl_context()11 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED12 assert context.check_hostname is True131416@pytest.mark.asyncio17async def test_load_ssl_config_verify_non_existing_path():15def test_load_ssl_config_verify_non_existing_path():16 ssl_config = httpx.SSLConfig(verify="/path/to/nowhere")17 with pytest.raises(IOError):20 await ssl_config.load_ssl_context()18 ssl_config.load_ssl_context()192023@pytest.mark.asyncio24async def test_load_ssl_config_verify_existing_file():21def test_load_ssl_config_verify_existing_file():22 ssl_config = httpx.SSLConfig(verify=httpx.config.DEFAULT_CA_BUNDLE_PATH)26 context = await ssl_config.load_ssl_context()23 context = ssl_config.load_ssl_context()24 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED25 assert context.check_hostname is True262731@pytest.mark.asyncio32async def test_load_ssl_config_verify_directory():28def test_load_ssl_config_verify_directory():29 path = httpx.config.DEFAULT_CA_BUNDLE_PATH.parent30 ssl_config = httpx.SSLConfig(verify=path)35 context = await ssl_config.load_ssl_context()31 context = ssl_config.load_ssl_context()32 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED33 assert context.check_hostname is True343540@pytest.mark.asyncio41async def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):36def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):37 ssl_config = httpx.SSLConfig(cert=(cert_pem_file, cert_private_key_file))43 context = await ssl_config.load_ssl_context()38 context = ssl_config.load_ssl_context()39 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED40 assert context.check_hostname is True414248@pytest.mark.asyncio43@pytest.mark.parametrize("password", [b"password", "password"])50async def test_load_ssl_config_cert_and_encrypted_key(44def test_load_ssl_config_cert_and_encrypted_key(45 cert_pem_file, cert_encrypted_private_key_file, password46):47 ssl_config = httpx.SSLConfig(48 cert=(cert_pem_file, cert_encrypted_private_key_file, password)49 )56 context = await ssl_config.load_ssl_context()50 context = ssl_config.load_ssl_context()51 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED52 assert context.check_hostname is True535461@pytest.mark.asyncio62async def test_load_ssl_config_cert_and_key_invalid_password(55def test_load_ssl_config_cert_and_key_invalid_password(56 cert_pem_file, cert_encrypted_private_key_file57):58 ssl_config = httpx.SSLConfig(59 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")60 )6162 with pytest.raises(ssl.SSLError):70 await ssl_config.load_ssl_context()63 ssl_config.load_ssl_context()646573@pytest.mark.asyncio74async def test_load_ssl_config_cert_without_key_raises(cert_pem_file):66def test_load_ssl_config_cert_without_key_raises(cert_pem_file):67 ssl_config = httpx.SSLConfig(cert=cert_pem_file)68 with pytest.raises(ssl.SSLError):77 await ssl_config.load_ssl_context()69 ssl_config.load_ssl_context()707180@pytest.mark.asyncio81async def test_load_ssl_config_no_verify():72def test_load_ssl_config_no_verify():73 ssl_config = httpx.SSLConfig(verify=False)83 context = await ssl_config.load_ssl_context()74 context = ssl_config.load_ssl_context()75 assert context.verify_mode == ssl.VerifyMode.CERT_NONE76 assert context.check_hostname is False7778
46 unmodified lines