From 7dbe52135bdcd10abe245ce63acd5a3cd2defda2 Mon Sep 17 00:00:00 2001 From: Artem Rys Date: Thu, 30 Dec 2021 14:59:02 +0100 Subject: [PATCH] feat: add remove_http_proxy_env_vars function --- solnlib/utils.py | 25 +++++++++++++++++++++++++ tests/unit/test_utils.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/solnlib/utils.py b/solnlib/utils.py index f2e684c9..e21d0b73 100644 --- a/solnlib/utils.py +++ b/solnlib/utils.py @@ -33,9 +33,34 @@ "is_false", "retry", "extract_http_scheme_host_port", + "remove_http_proxy_env_vars", ] +def remove_http_proxy_env_vars() -> None: + """Removes HTTP(s) proxies from environment variables. + + Removes the following environment variables: + * http_proxy + * https_proxy + * HTTP_PROXY + * HTTPS_PROXY + + This function can be used in Splunk modular inputs code before starting the + ingestion to ensure that no proxy is going to be used when doing requests. + In case of proxy is needed, it can be defined in the modular inputs code. + """ + env_vars_to_remove = ( + "http_proxy", + "https_proxy", + "HTTP_PROXY", + "HTTPS_PROXY", + ) + for env_var in env_vars_to_remove: + if env_var in os.environ: + del os.environ[env_var] + + def handle_teardown_signals(callback: Callable): """Register handler for SIGTERM/SIGINT/SIGBREAK signal. diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 0b4f4de7..41dfaf5e 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -19,6 +19,7 @@ import os import signal import time +from unittest import mock import pytest @@ -138,3 +139,34 @@ def test_extract_http_scheme_host_port(monkeypatch): invalid = "localhost:8089" with pytest.raises(ValueError): _, _, _ = utils.extract_http_scheme_host_port(invalid) + + +@mock.patch.dict(os.environ, {"SPLUNK_HOME": "/opt/splunk"}, clear=True) +def test_remove_http_proxy_env_vars_preserves_non_http_env_vars(): + utils.remove_http_proxy_env_vars() + + assert "/opt/splunk" == os.getenv("SPLUNK_HOME") + + +@mock.patch.dict(os.environ, {"HTTP_PROXY": "proxy:80"}, clear=True) +def test_remove_http_proxy_env_vars_removes_proxy_related_env_vars(): + utils.remove_http_proxy_env_vars() + + assert None is os.getenv("HTTP_PROXY") + + +@mock.patch.dict( + os.environ, + { + "SPLUNK_HOME": "/opt/splunk", + "HTTP_PROXY": "proxy", + "https_proxy": "proxy", + }, + clear=True, +) +def test_remove_http_proxy_env_vars(): + utils.remove_http_proxy_env_vars() + + assert None is os.getenv("HTTP_PROXY") + assert None is os.getenv("https_proxy") + assert "/opt/splunk" == os.getenv("SPLUNK_HOME")