Debian Codegraph @

ansible_collections/community/docker/plugins/module_utils/_api/utils/proxy.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 .utils import format_environment
17
18
19 class ProxyConfig(dict):
20 """
21 Hold the client's proxy configuration
22 """
23
24 @property
25 def http(self) -> str | None:
26 return self.get("http")
27
28 @property
29 def https(self) -> str | None:
30 return self.get("https")
31
32 @property
33 def ftp(self) -> str | None:
34 return self.get("ftp")
35
36 @property
37 def no_proxy(self) -> str | None:
38 return self.get("no_proxy")
39
40 @staticmethod
41 def from_dict(config: dict[str, str]) -> ProxyConfig:
42 """
43 Instantiate a new ProxyConfig from a dictionary that represents a
44 client configuration, as described in `the documentation`_.
45
46 .. _the documentation:
47 https://docs.docker.com/network/proxy/#configure-the-docker-client
48 """
49 return ProxyConfig(
50 http=config.get("httpProxy"),
51 https=config.get("httpsProxy"),
52 ftp=config.get("ftpProxy"),
53 no_proxy=config.get("noProxy"),
54 )
55
56 def get_environment(self) -> dict[str, str]:
57 """
58 Return a dictionary representing the environment variables used to
59 set the proxy settings.
60 """
61 env = {}
62 if self.http:
63 env["http_proxy"] = env["HTTP_PROXY"] = self.http
64 if self.https:
65 env["https_proxy"] = env["HTTPS_PROXY"] = self.https
66 if self.ftp:
67 env["ftp_proxy"] = env["FTP_PROXY"] = self.ftp
68 if self.no_proxy:
69 env["no_proxy"] = env["NO_PROXY"] = self.no_proxy
70 return env
71
72 @t.overload
73 def inject_proxy_environment(self, environment: list[str]) -> list[str]: ...
74
75 @t.overload
76 def inject_proxy_environment(
77 self, environment: list[str] | None
78 ) -> list[str] | None: ...
79
80 def inject_proxy_environment(
81 self, environment: list[str] | None
82 ) -> list[str] | None:
83 """
84 Given a list of strings representing environment variables, prepend the
85 environment variables corresponding to the proxy settings.
86 """
87 if not self:
88 return environment
89
90 proxy_env = format_environment(self.get_environment())
91 if not environment:
92 return proxy_env
93 # It is important to prepend our variables, because we want the
94 # variables defined in "environment" to take precedence.
95 return proxy_env + environment
96
97 def __str__(self) -> str:
98 return f"ProxyConfig(http={self.http}, https={self.https}, ftp={self.ftp}, no_proxy={self.no_proxy})"