File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 3333 "is_false" ,
3434 "retry" ,
3535 "extract_http_scheme_host_port" ,
36+ "remove_http_proxy_env_vars" ,
3637]
3738
3839
40+ def remove_http_proxy_env_vars () -> None :
41+ """Removes HTTP(s) proxies from environment variables.
42+
43+ Removes the following environment variables:
44+ * http_proxy
45+ * https_proxy
46+ * HTTP_PROXY
47+ * HTTPS_PROXY
48+
49+ This function can be used in Splunk modular inputs code before starting the
50+ ingestion to ensure that no proxy is going to be used when doing requests.
51+ In case of proxy is needed, it can be defined in the modular inputs code.
52+ """
53+ env_vars_to_remove = (
54+ "http_proxy" ,
55+ "https_proxy" ,
56+ "HTTP_PROXY" ,
57+ "HTTPS_PROXY" ,
58+ )
59+ for env_var in env_vars_to_remove :
60+ if env_var in os .environ :
61+ del os .environ [env_var ]
62+
63+
3964def handle_teardown_signals (callback : Callable ):
4065 """Register handler for SIGTERM/SIGINT/SIGBREAK signal.
4166
Original file line number Diff line number Diff line change 1919import os
2020import signal
2121import time
22+ from unittest import mock
2223
2324import pytest
2425
@@ -138,3 +139,34 @@ def test_extract_http_scheme_host_port(monkeypatch):
138139 invalid = "localhost:8089"
139140 with pytest .raises (ValueError ):
140141 _ , _ , _ = utils .extract_http_scheme_host_port (invalid )
142+
143+
144+ @mock .patch .dict (os .environ , {"SPLUNK_HOME" : "/opt/splunk" }, clear = True )
145+ def test_remove_http_proxy_env_vars_preserves_non_http_env_vars ():
146+ utils .remove_http_proxy_env_vars ()
147+
148+ assert "/opt/splunk" == os .getenv ("SPLUNK_HOME" )
149+
150+
151+ @mock .patch .dict (os .environ , {"HTTP_PROXY" : "proxy:80" }, clear = True )
152+ def test_remove_http_proxy_env_vars_removes_proxy_related_env_vars ():
153+ utils .remove_http_proxy_env_vars ()
154+
155+ assert None is os .getenv ("HTTP_PROXY" )
156+
157+
158+ @mock .patch .dict (
159+ os .environ ,
160+ {
161+ "SPLUNK_HOME" : "/opt/splunk" ,
162+ "HTTP_PROXY" : "proxy" ,
163+ "https_proxy" : "proxy" ,
164+ },
165+ clear = True ,
166+ )
167+ def test_remove_http_proxy_env_vars ():
168+ utils .remove_http_proxy_env_vars ()
169+
170+ assert None is os .getenv ("HTTP_PROXY" )
171+ assert None is os .getenv ("https_proxy" )
172+ assert "/opt/splunk" == os .getenv ("SPLUNK_HOME" )
You can’t perform that action at this time.
0 commit comments