Skip to content
Myr

Push SSL context loading into config.py

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

authored by Tom Christie · committed by Tom Christie · parent 30a03f4 · Browse files · 2 files+58 added−58 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+58 added−58 removed

  • other:+58 added−58 removed

2 files+58 added−58 removed

modified:httpcore/config.py+53 added
1import ssl2import typing34import certifi5
26 unmodified lines
32 def __repr__(self) -> str:33 class_name = self.__class__.__name__34 return f"{class_name}(cert={self.cert}, verify={self.verify})"3536 async def load_ssl_context(self) -> ssl.SSLContext:37 if not hasattr(self, "ssl_context"):38 if not self.verify:39 self.ssl_context = self.load_ssl_context_no_verify()40 else:41 # Run the SSL loading in a threadpool, since it makes disk accesses.42 loop = asyncio.get_event_loop()43 self.ssl_context = await loop.run_in_executor(44 None, self.load_ssl_context_verify45 )4647 return self.ssl_context4849 def load_ssl_context_no_verify(self) -> ssl.SSLContext:50 """51 Return an SSL context for unverified connections.52 """53 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)54 context.options |= ssl.OP_NO_SSLv255 context.options |= ssl.OP_NO_SSLv356 context.options |= ssl.OP_NO_COMPRESSION57 context.set_default_verify_paths()58 return context5960 def load_ssl_context_verify(self) -> ssl.SSLContext:61 """62 Return an SSL context for verified connections.63 """64 if isinstance(self.verify, bool):65 ca_bundle_path = DEFAULT_CA_BUNDLE_PATH66 elif os.path.exists(self.verify):67 ca_bundle_path = self.verify68 else:69 raise IOError(70 "Could not find a suitable TLS CA certificate bundle, "71 "invalid path: {}".format(self.verify)72 )7374 context = ssl.create_default_context()75 if os.path.isfile(ca_bundle_path):76 context.load_verify_locations(cafile=ca_bundle_path)77 elif os.path.isdir(ca_bundle_path):78 context.load_verify_locations(capath=ca_bundle_path)7980 if self.cert is not None:81 if isinstance(self.cert, str):82 context.load_cert_chain(certfile=self.cert)83 else:84 context.load_cert_chain(certfile=self.cert[0], keyfile=self.cert[1])8586 return context878889class TimeoutConfig:90 """91 Timeout values.92 """
78 unmodified lines
modified:httpcore/pool.py+5 added-58 removed
97 unmodified lines
98 self.num_keepalive_connections -= 199 self.num_active_connections += 1100101 except (KeyError, IndexError):102 ssl_context = await self.get_ssl_context(url, ssl)102 if url.is_secure:103 ssl_context = await ssl.load_ssl_context()104 else:105 ssl_context = None106107 try:108 await asyncio.wait_for(109 self._max_connections.acquire(), timeout.pool_timeout110 )
26 unmodified lines
137 self._keepalive_connections[key].append(connection)138 except KeyError:139 self._keepalive_connections[key] = [connection]140137 async def get_ssl_context(138 self, url: URL, config: SSLConfig139 ) -> typing.Optional[ssl.SSLContext]:140 if not url.is_secure:141 return None142143 if not hasattr(self, "ssl_context"):144 if not config.verify:145 self.ssl_context = self.get_ssl_context_no_verify()146 else:147 # Run the SSL loading in a threadpool, since it makes disk accesses.148 loop = asyncio.get_event_loop()149 self.ssl_context = await loop.run_in_executor(150 None, self.get_ssl_context_verify151 )152153 return self.ssl_context154155 def get_ssl_context_no_verify(self) -> ssl.SSLContext:156 """157 Return an SSL context for unverified connections.158 """159 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)160 context.options |= ssl.OP_NO_SSLv2161 context.options |= ssl.OP_NO_SSLv3162 context.options |= ssl.OP_NO_COMPRESSION163 context.set_default_verify_paths()164 return context165166 def get_ssl_context_verify(self, config: SSLConfig) -> ssl.SSLContext:167 """168 Return an SSL context for verified connections.169 """170 if isinstance(config.verify, bool):171 ca_bundle_path = DEFAULT_CA_BUNDLE_PATH172 elif os.path.exists(config.verify):173 ca_bundle_path = config.verify174 else:175 raise IOError(176 "Could not find a suitable TLS CA certificate bundle, "177 "invalid path: {}".format(config.verify)178 )179180 context = ssl.create_default_context()181 if os.path.isfile(ca_bundle_path):182 context.load_verify_locations(cafile=ca_bundle_path)183 elif os.path.isdir(ca_bundle_path):184 context.load_verify_locations(capath=ca_bundle_path)185186 if config.cert is not None:187 if isinstance(config.cert, str):188 context.load_cert_chain(certfile=config.cert)189 else:190 context.load_cert_chain(certfile=config.cert[0], keyfile=config.cert[1])191192 return context193141 async def close(self) -> None:142 self.is_closed = True143144 async def __aenter__(self) -> "ConnectionPool":
9 unmodified lines