Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

outgoing_http: Default all OutgoingSession requests through Smokescreen.

Alex Vandiver6d643438aed1
diff --git a/zerver/lib/outgoing_http.py b/zerver/lib/outgoing_http.py
index b3899a79fe8..62ebcd8339b 100644
--- a/zerver/lib/outgoing_http.py
+++ b/zerver/lib/outgoing_http.py
@@ -4,6 +4,8 @@ import requests
from typing_extensions import override
from urllib3.util import Retry
+from zproject.config import get_config
+
class OutgoingSession(requests.Session):
def __init__(
@@ -12,6 +14,7 @@ class OutgoingSession(requests.Session):
timeout: float,
headers: dict[str, str] | None = None,
max_retries: int | Retry | None = None,
+ proxies: dict[str, str] | None = None,
) -> None:
super().__init__()
retry: Retry | None = Retry(total=0)
@@ -26,6 +29,21 @@ class OutgoingSession(requests.Session):
if headers:
self.headers.update(headers)
+ if proxies is None:
+ proxy_host = get_config("http_proxy", "host", "localhost")
+ proxy_port = get_config("http_proxy", "port", "4750")
+ proxy = ""
+ if proxy_host != "" and proxy_port != "":
+ proxy = f"http://{proxy_host}:{proxy_port}"
+ self.proxies.update(
+ {
+ "http": proxy,
+ "https": proxy,
+ }
+ )
+ else:
+ self.proxies.update(proxies)
+
class OutgoingHTTPAdapter(requests.adapters.HTTPAdapter):
role: str
diff --git a/zerver/tests/test_outgoing_http.py b/zerver/tests/test_outgoing_http.py
index fad439ed36c..89bcaf274ec 100644
--- a/zerver/tests/test_outgoing_http.py
+++ b/zerver/tests/test_outgoing_http.py
@@ -48,7 +48,7 @@ class TestOutgoingHttp(ZulipTestCase):
def test_headers(self) -> None:
with RequestMockWithProxySupport() as mock_requests:
mock_requests.add(responses.GET, "http://example.com/")
- OutgoingSession(role="testing", timeout=1, headers={"X-Foo": "bar"}).get(
+ OutgoingSession(role="testing", timeout=1, headers={"X-Foo": "bar"}, proxies={}).get(
"http://example.com/"
)
self.assert_length(mock_requests.calls, 1)
@@ -57,8 +57,42 @@ class TestOutgoingHttp(ZulipTestCase):
self.assertFalse("X-Smokescreen-Role" in headers)
self.assertEqual(headers["X-Foo"], "bar")
+ def test_proxy_headers_config(self) -> None:
+ with (
+ RequestMockWithProxySupport() as mock_requests,
+ mock.patch("zerver.lib.outgoing_http.get_config") as get_config,
+ ):
+ get_config.side_effect = ("localhost", "4242")
+ mock_requests.add(responses.GET, "http://localhost:4242/")
+ OutgoingSession(role="testing", timeout=1, headers={"X-Foo": "bar"}).get(
+ "http://example.com/"
+ )
+ self.assert_length(mock_requests.calls, 1)
+ headers = mock_requests.calls[0].request.headers
+ self.assertEqual(headers["X-Smokescreen-Role"], "testing")
+
+ def test_proxy_headers_no_config(self) -> None:
+ with (
+ RequestMockWithProxySupport() as mock_requests,
+ mock.patch("zerver.lib.outgoing_http.get_config") as get_config,
+ ):
+ get_config.side_effect = ("", "")
+ mock_requests.add(responses.GET, "http://example.com/")
+ OutgoingSession(role="testing", timeout=1).get("http://example.com/")
+
+ def test_proxy_headers_proxy_override(self) -> None:
+ with (
+ RequestMockWithProxySupport() as mock_requests,
+ mock.patch("zerver.lib.outgoing_http.get_config") as get_config,
+ ):
+ get_config.side_effect = ("localhost", "4242")
+ mock_requests.add(responses.GET, "http://proxy-host:4343/")
+ OutgoingSession(
+ role="testing", timeout=1, proxies={"http": "http://proxy-host:4343"}
+ ).get("http://example.com/")
+
@mock.patch.dict(os.environ, {"http_proxy": "http://localhost:4242"})
- def test_proxy_headers(self) -> None:
+ def test_proxy_headers_env(self) -> None:
with RequestMockWithProxySupport() as mock_requests:
mock_requests.add(responses.GET, "http://localhost:4242/")
OutgoingSession(role="testing", timeout=1, headers={"X-Foo": "bar"}).get(