Allow for overriding of specific pool key params
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This re-enables the use case of providing a custom SSLContext via a Transport Adapter as broken in #6655 and reported in #6715 Closes #6715
authored by Ian Stapleton Cordasco · committed by Ian Stapleton Cordasco · parent 88dce9d · Browse files · 2 files·+76 added−9 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+76 added−9 removed
- other:+76 added−9 removed
Provenance
- Commit
a62a2d3- Tree
e092aa0- Author
Ian Stapleton Cordasco <graffatcolmingov@gmail.com>- Committer
Ian Stapleton Cordasco <graffatcolmingov@gmail.com>- Parent
88dce9d
2 files changed+76 added−9 removed
modified:src/requests/adapters.py+69 added-9 removed
373 unmodified lines374 response.connection = self375 376 return response377 378+ def build_connection_pool_key_attributes(self, request, verify, cert=None):379+ """Build the PoolKey attributes used by urllib3 to return a connection.380+381+ This looks at the PreparedRequest, the user-specified verify value,382+ and the value of the cert parameter to determine what PoolKey values383+ to use to select a connection from a given urllib3 Connection Pool.384+385+ The SSL related pool key arguments are not consistently set. As of386+ this writing, use the following to determine what keys may be in that387+ dictionary:388+389+ * If ``verify`` is ``True``, ``"ssl_context"`` will be set and will be the390+ default Requests SSL Context391+ * If ``verify`` is ``False``, ``"ssl_context"`` will not be set but392+ ``"cert_reqs"`` will be set393+ * If ``verify`` is a string, (i.e., it is a user-specified trust bundle)394+ ``"ca_certs"`` will be set if the string is not a directory recognized395+ by :py:func:`os.path.isdir`, otherwise ``"ca_certs_dir"`` will be396+ set.397+ * If ``"cert"`` is specified, ``"cert_file"`` will always be set. If398+ ``"cert"`` is a tuple with a second item, ``"key_file"`` will also399+ be present400+401+ To override these settings, one may subclass this class, call this402+ method and use the above logic to change parameters as desired. For403+ example, if one wishes to use a custom :py:class:`ssl.SSLContext` one404+ must both set ``"ssl_context"`` and based on what else they require,405+ alter the other keys to ensure the desired behaviour.406+407+ :param request:408+ The PreparedReqest being sent over the connection.409+ :type request:410+ :class:`~requests.models.PreparedRequest`411+ :param verify:412+ Either a boolean, in which case it controls whether413+ we verify the server's TLS certificate, or a string, in which case it414+ must be a path to a CA bundle to use.415+ :param cert:416+ (optional) Any user-provided SSL certificate for client417+ authentication (a.k.a., mTLS). This may be a string (i.e., just418+ the path to a file which holds both certificate and key) or a419+ tuple of length 2 with the certificate file path and key file420+ path.421+ :returns:422+ A tuple of two dictionaries. The first is the "host parameters"423+ portion of the Pool Key including scheme, hostname, and port. The424+ second is a dictionary of SSLContext related parameters.425+ """426+ return _urllib3_request_context(request, verify, cert)427+428 def get_connection_with_tls_context(self, request, verify, proxies=None, cert=None):429 """Returns a urllib3 connection for the given request and TLS settings.430 This should not be called from user code, and is only exposed for use431 when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.432 383- :param request: The :class:`PreparedRequest <PreparedRequest>` object384- to be sent over the connection.385- :param verify: Either a boolean, in which case it controls whether386- we verify the server's TLS certificate, or a string, in which case it387- must be a path to a CA bundle to use.388- :param proxies: (optional) The proxies dictionary to apply to the request.389- :param cert: (optional) Any user-provided SSL certificate to be trusted.390- :rtype: urllib3.ConnectionPool433+ :param request:434+ The :class:`PreparedRequest <PreparedRequest>` object to be sent435+ over the connection.436+ :param verify:437+ Either a boolean, in which case it controls whether we verify the438+ server's TLS certificate, or a string, in which case it must be a439+ path to a CA bundle to use.440+ :param proxies:441+ (optional) The proxies dictionary to apply to the request.442+ :param cert:443+ (optional) Any user-provided SSL certificate to be used for client444+ authentication (a.k.a., mTLS).445+ :rtype:446+ urllib3.ConnectionPool447 """448 proxy = select_proxy(request.url, proxies)449 try:394- host_params, pool_kwargs = _urllib3_request_context(request, verify, cert)450+ host_params, pool_kwargs = self.build_connection_pool_key_attributes(451+ request,452+ verify,453+ cert,454+ )455 except ValueError as e:456 raise InvalidURL(e, request=request)457 if proxy:458 proxy = prepend_scheme_if_needed(proxy, "http")243 unmodified linesmodified:HISTORY.md+7 added
4 unmodified lines5 ---6 7 - \[Short description of non-trivial change.\]8 9+2.32.3 (2024-??-??)10+-------------------11+12+**Bugfixes**13+- Fix bug breaking the ability to specify custom SSLContexts in sub-classes of14+ HTTPAdapter. (#6655)15+16 2.32.2 (2024-05-21)17 -------------------18 19 **Deprecations**1961 unmodified lines