From a9e892c2e648cdcdbb18ff706c5c8078ff4922d7 Mon Sep 17 00:00:00 2001 From: shipperizer Date: Mon, 24 Jun 2024 17:23:43 +0100 Subject: [PATCH] feat: add support for http_proxy --- charmcraft.yaml | 12 ++++++ lib/charms/lego_base_k8s/v0/lego_client.py | 46 +++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/charmcraft.yaml b/charmcraft.yaml index bc315f8..95a152b 100644 --- a/charmcraft.yaml +++ b/charmcraft.yaml @@ -89,3 +89,15 @@ config: httpreq_username: type: string description: Basic authentication username + http_proxy: + description: URL of the HTTP proxy eg http://proxy.internal:6666, it will set the HTTP_PROXY var in the workload environment + type: string + default: '' + https_proxy: + description: URL of the HTTPS proxy eg http://proxy.internal:6666, it will set the HTTPS_PROXY var in the workload environment + type: string + default: '' + no_proxy: + description: Domains that need to be excluded from proxying no_proxy="test.com,test.co.uk", it is a comma separate list + type: string + default: '' \ No newline at end of file diff --git a/lib/charms/lego_base_k8s/v0/lego_client.py b/lib/charms/lego_base_k8s/v0/lego_client.py index 299ccb6..107a0d5 100644 --- a/lib/charms/lego_base_k8s/v0/lego_client.py +++ b/lib/charms/lego_base_k8s/v0/lego_client.py @@ -95,7 +95,7 @@ def _plugin_config(self): # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 9 +LIBPATCH = 10 logger = logging.getLogger(__name__) @@ -224,7 +224,7 @@ def _push_csr_to_workload(self, csr: str) -> None: def _execute_lego_cmd(self) -> bool: """Execute lego command in workload container.""" process = self._container.exec( - self._cmd, timeout=300, working_dir="/tmp", environment=self._plugin_config + self._cmd, timeout=300, working_dir="/tmp", environment=self._common_config | self._plugin_config ) try: stdout, error = process.wait_output() @@ -316,6 +316,27 @@ def _cmd(self) -> List[str]: "run", ] + @property + def _common_config(self) -> Dict[str, str]: + """Common configuration for the command + + Used to set the environment context + + Returns: + dict[str, str]: common configuration. + """ + common_config = { + "HTTPREQ_ENDPOINT": self._httpreq_endpoint, + } + if self._http_proxy: + common_config["HTTP_PROXY"] = self._http_proxy + if self._https_proxy: + common_config["HTTPS_PROXY"] = self._https_proxy + if self._no_proxy: + common_config["NO_PROXY"] = self._no_proxy + + return common_config + @property @abstractmethod def _plugin_config(self) -> Dict[str, str]: @@ -358,3 +379,24 @@ def _server(self) -> Optional[str]: if not isinstance(server, str): return None return server + + @property + def _http_proxy(self) -> Optional[str]: + http_proxy = self.model.config.get("http_proxy", None) + if not isinstance(http_proxy, str): + return None + return http_proxy + + @property + def _https_proxy(self) -> Optional[str]: + https_proxy = self.model.config.get("https_proxy", None) + if not isinstance(https_proxy, str): + return None + return https_proxy + + @property + def _no_proxy(self) -> Optional[str]: + no_proxy = self.model.config.get("no_proxy", None) + if not isinstance(no_proxy, str): + return None + return no_proxy