Debian Codegraph @

ansible_collections/community/docker/plugins/module_utils/_api/transport/ssladapter.py

raw
1 # This code is part of the Ansible collection community.docker, but is an independent component.
2 # This particular file, and this file only, is based on the Docker SDK for Python (https://github.com/docker/docker-py/)
3 #
4 # Copyright (c) 2016-2022 Docker, Inc.
5 #
6 # It is licensed under the Apache 2.0 license (see LICENSES/Apache-2.0.txt in this collection)
7 # SPDX-License-Identifier: Apache-2.0
8
9 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
10 # Do not use this from other collections or standalone plugins/modules!
11
12 from __future__ import annotations
13
14 import typing as t
15
16 from .._import_helper import HTTPAdapter, urllib3
17 from .basehttpadapter import BaseHTTPAdapter
18
19 # Resolves OpenSSL issues in some servers:
20 # https://lukasa.co.uk/2013/01/Choosing_SSL_Version_In_Requests/
21 # https://github.com/kennethreitz/requests/pull/799
22
23
24 PoolManager = urllib3.poolmanager.PoolManager
25
26
27 class SSLHTTPAdapter(BaseHTTPAdapter):
28 """An HTTPS Transport Adapter that uses an arbitrary SSL version."""
29
30 __attrs__ = HTTPAdapter.__attrs__ + ["assert_hostname"]
31
32 def __init__(
33 self,
34 assert_hostname: bool | None = None,
35 **kwargs: t.Any,
36 ) -> None:
37 self.assert_hostname = assert_hostname
38 super().__init__(**kwargs)
39
40 def init_poolmanager(
41 self, connections: int, maxsize: int, block: bool = False, **kwargs: t.Any
42 ) -> None:
43 kwargs = {
44 "num_pools": connections,
45 "maxsize": maxsize,
46 "block": block,
47 }
48 if self.assert_hostname is not None:
49 kwargs["assert_hostname"] = self.assert_hostname
50
51 self.poolmanager = PoolManager(**kwargs)
52
53 def get_connection(self, *args: t.Any, **kwargs: t.Any) -> urllib3.ConnectionPool:
54 """
55 Ensure assert_hostname is set correctly on our pool
56
57 We already take care of a normal poolmanager via init_poolmanager
58
59 But we still need to take care of when there is a proxy poolmanager
60
61 Note that this method is no longer called for newer requests versions.
62 """
63 # pylint finds our HTTPAdapter stub instead of requests.adapters.HTTPAdapter:
64 # pylint: disable-next=no-member
65 conn = super().get_connection(*args, **kwargs)
66 if (
67 self.assert_hostname is not None
68 and conn.assert_hostname != self.assert_hostname # type: ignore
69 ):
70 conn.assert_hostname = self.assert_hostname # type: ignore
71 return conn