Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
outgoing_http: Put the X-Smokescreen-Role in the proxy headers.
Alex Vandiver6339e7fd4743
diff --git a/zerver/lib/outgoing_http.py b/zerver/lib/outgoing_http.pyindex 52c746a5f8c..b2b4f7abdbe 100644--- a/zerver/lib/outgoing_http.py+++ b/zerver/lib/outgoing_http.py@@ -5,17 +5,19 @@ from urllib3 import HTTPResponseclass OutgoingSession(requests.Session):- def __init__(self, timeout: int, headers: Optional[Dict[str, str]] = None) -> None:+ def __init__(self, role: str, timeout: int) -> None:super().__init__()- outgoing_adapter = OutgoingHTTPAdapter(timeout=timeout)+ outgoing_adapter = OutgoingHTTPAdapter(role=role, timeout=timeout)self.mount("http://", outgoing_adapter)self.mount("https://", outgoing_adapter)class OutgoingHTTPAdapter(requests.adapters.HTTPAdapter):+ role: strtimeout: int- def __init__(self, timeout: int, *args: Any, **kwargs: Any) -> None:+ def __init__(self, role: str, timeout: int, *args: Any, **kwargs: Any) -> None:+ self.role = roleself.timeout = timeoutsuper().__init__(*args, *kwargs)@@ -23,3 +25,6 @@ class OutgoingHTTPAdapter(requests.adapters.HTTPAdapter):if kwargs.get("timeout") is None:kwargs["timeout"] = self.timeoutreturn super().send(*args, **kwargs)++ def proxy_headers(self, proxy: str) -> Dict[str, str]:+ return {"X-Smokescreen-Role": self.role}diff --git a/zerver/lib/outgoing_webhook.py b/zerver/lib/outgoing_webhook.pyindex e673ec26379..02751830858 100644--- a/zerver/lib/outgoing_webhook.py+++ b/zerver/lib/outgoing_webhook.py@@ -34,11 +34,11 @@ class OutgoingWebhookServiceInterface(metaclass=abc.ABCMeta):self.user_profile: UserProfile = user_profileself.service_name: str = service_nameself.session: requests.Session = OutgoingSession(+ role="webhook",timeout=10,)self.session.headers.update({- "X-Smokescreen-Role": "webhook","User-Agent": "ZulipOutgoingWebhook/" + ZULIP_VERSION,})diff --git a/zerver/tests/test_outgoing_http.py b/zerver/tests/test_outgoing_http.pyindex a362736a881..dcd13ba5613 100644--- a/zerver/tests/test_outgoing_http.py+++ b/zerver/tests/test_outgoing_http.py@@ -1,4 +1,6 @@+import osfrom typing import Any+from unittest import mockimport requestsimport responses@@ -7,6 +9,28 @@ from zerver.lib.outgoing_http import OutgoingSessionfrom zerver.lib.test_classes import ZulipTestCase+class RequestMockWithProxySupport(responses.RequestsMock):+ def _on_request(+ self,+ adapter: requests.adapters.HTTPAdapter,+ request: requests.PreparedRequest,+ **kwargs: Any,+ ) -> requests.Response:+ if "proxies" in kwargs and request.url:+ proxy_uri = requests.utils.select_proxy(request.url, kwargs["proxies"])+ if proxy_uri is not None:+ request = requests.Request(+ method="GET",+ url="{}/".format(proxy_uri),+ headers=adapter.proxy_headers(proxy_uri),+ ).prepare()+ return super()._on_request( # type: ignore[misc] # This is an undocumented internal API+ adapter,+ request,+ **kwargs,+ )++class RequestMockWithTimeoutAsHeader(responses.RequestsMock):def _on_request(self,@@ -24,15 +48,24 @@ class RequestMockWithTimeoutAsHeader(responses.RequestsMock):class TestOutgoingHttp(ZulipTestCase):+ @mock.patch.dict(os.environ, {"http_proxy": "http://localhost:4242"})+ def test_proxy_headers(self) -> None:+ with RequestMockWithProxySupport() as mock_requests:+ mock_requests.add(responses.GET, "http://localhost:4242/")+ OutgoingSession(role="testing", timeout=1).get("http://example.com/")+ self.assertEqual(len(mock_requests.calls), 1)+ headers = mock_requests.calls[0].request.headers+ self.assertEqual(headers["X-Smokescreen-Role"], "testing")+def test_timeouts(self) -> None:with RequestMockWithTimeoutAsHeader() as mock_requests:mock_requests.add(responses.GET, "http://example.com/")- OutgoingSession(timeout=17).get("http://example.com/")+ OutgoingSession(role="testing", timeout=17).get("http://example.com/")self.assertEqual(len(mock_requests.calls), 1)self.assertEqual(mock_requests.calls[0].request.headers["X-Timeout"], 17)with RequestMockWithTimeoutAsHeader() as mock_requests:mock_requests.add(responses.GET, "http://example.com/")- OutgoingSession(timeout=17).get("http://example.com/", timeout=42)+ OutgoingSession(role="testing", timeout=17).get("http://example.com/", timeout=42)self.assertEqual(len(mock_requests.calls), 1)self.assertEqual(mock_requests.calls[0].request.headers["X-Timeout"], 42)diff --git a/zerver/tests/test_outgoing_webhook_system.py b/zerver/tests/test_outgoing_webhook_system.pyindex 81ae52e28f0..a8556215b7c 100644--- a/zerver/tests/test_outgoing_webhook_system.py+++ b/zerver/tests/test_outgoing_webhook_system.py@@ -185,7 +185,6 @@ The webhook got a response with status code *400*.""",headers = {"Content-Type": "application/json","User-Agent": user_agent,- "X-Smokescreen-Role": "webhook",}self.assertLessEqual(headers.items(), prepared_request.headers.items())