8000 Add basic support for no_proxy environment variable (#838) · adamchainz/sentry-python@4d16ef6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d16ef6

Browse files
authored
Add basic support for no_proxy environment variable (getsentry#838)
1 parent 86d14b0 commit 4d16ef6

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

sentry_sdk/transport.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,17 @@ def _get_pool_options(self, ca_certs):
276276
"ca_certs": ca_certs or certifi.where(),
277277
}
278278

279+
def _in_no_proxy(self, parsed_dsn):
280+
# type: (Dsn) -> bool
281+
no_proxy = getproxies().get("no")
282+
if not no_proxy:
283+
return False
284+
for host in no_proxy.split(","):
285+
host = host.strip()
286+
if parsed_dsn.host.endswith(host) or parsed_dsn.netloc.endswith(host):
287+
return True
288+
return False
289+
279290
def _make_pool(
280291
self,
281292
parsed_dsn, # type: Dsn
@@ -285,14 +296,15 @@ def _make_pool(
285296
):
286297
# type: (...) -> Union[PoolManager, ProxyManager]
287298
proxy = None
299+
no_proxy = self._in_no_proxy(parsed_dsn)
288300

289301
# try HTTPS first
290302
if parsed_dsn.scheme == "https" and (https_proxy != ""):
291-
proxy = https_proxy or getproxies().get("https")
303+
proxy = https_proxy or (not no_proxy and getproxies().get("https"))
292304

293305
# maybe fallback to HTTP proxy
294306
if not proxy and (http_proxy != ""):
295-
proxy = http_proxy or getproxies().get("http")
307+
proxy = http_proxy or (not no_proxy and getproxies().get("http"))
296308

297309
opts = self._get_pool_options(ca_certs)
298310

tests/test_client.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,52 @@ def test_transport_option(monkeypatch):
187187
"arg_https_proxy": None,
188188
"expected_proxy_scheme": "http",
189189
},
190+
# NO_PROXY testcases
191+
{
192+
"dsn": "http://foo@sentry.io/123",
193+
"env_http_proxy": "http://localhost/123",
194+
"env_https_proxy": None,
195+
"env_no_proxy": "sentry.io,example.com",
196+
"arg_http_proxy": None,
197+
"arg_https_proxy": None,
198+
"expected_proxy_scheme": None,
199+
},
200+
{
201+
"dsn": "https://foo@sentry.io/123",
202+
"env_http_proxy": None,
203+
"env_https_proxy": "https://localhost/123",
204+
"env_no_proxy": "example.com,sentry.io",
205+
"arg_http_proxy": None,
206+
"arg_https_proxy": None,
207+
"expected_proxy_scheme": None,
208+
},
209+
{
210+
"dsn": "http://foo@sentry.io/123",
211+
"env_http_proxy": None,
212+
"env_https_proxy": None,
213+
"env_no_proxy": "sentry.io,example.com",
214+
"arg_http_proxy": "http://localhost/123",
215+
"arg_https_proxy": None,
216+
"expected_proxy_scheme": "http",
217+
},
218+
{
219+
"dsn": "https://foo@sentry.io/123",
220+
"env_http_proxy": None,
221+
"env_https_proxy": None,
222+
"env_no_proxy": "sentry.io,example.com",
223+
"arg_http_proxy": None,
224+
"arg_https_proxy": "https://localhost/123",
225+
"expected_proxy_scheme": "https",
226+
},
190227
],
191228
)
192229
def test_proxy(monkeypatch, testcase):
193230
if testcase["env_http_proxy"] is not None:
194231
monkeypatch.setenv("HTTP_PROXY", testcase["env_http_proxy"])
195232
if testcase["env_https_proxy"] is not None:
196233
monkeypatch.setenv("HTTPS_PROXY", testcase["env_https_proxy"])
234+
if testcase.get("env_no_proxy") is not None:
235+
monkeypatch.setenv("NO_PROXY", testcase["env_no_proxy"])
197236
kwargs = {}
198237
if testcase["arg_http_proxy"] is not None:
199238
kwargs["http_proxy"] = testcase["arg_http_proxy"]

0 commit comments

Comments
 (0)
0