Skip to content

Commit 7dbe521

Browse files
artemrysArtem Rys
authored andcommitted
feat: add remove_http_proxy_env_vars function
1 parent 7633ec3 commit 7dbe521

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

solnlib/utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,34 @@
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+
3964
def handle_teardown_signals(callback: Callable):
4065
"""Register handler for SIGTERM/SIGINT/SIGBREAK signal.
4166

tests/unit/test_utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
import signal
2121
import time
22+
from unittest import mock
2223

2324
import 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")

0 commit comments

Comments
 (0)