Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for no_proxy environment variable #838

Merged
merged 1 commit into from Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions sentry_sdk/transport.py
Expand Up @@ -276,6 +276,17 @@ def _get_pool_options(self, ca_certs):
"ca_certs": ca_certs or certifi.where(),
}

def _in_no_proxy(self, parsed_dsn):
# type: (Dsn) -> bool
no_proxy = getproxies().get("no")
if not no_proxy:
return False
for host in no_proxy.split(","):
host = host.strip()
if parsed_dsn.host.endswith(host) or parsed_dsn.netloc.endswith(host):
return True
return False

def _make_pool(
self,
parsed_dsn, # type: Dsn
Expand All @@ -285,14 +296,15 @@ def _make_pool(
):
# type: (...) -> Union[PoolManager, ProxyManager]
proxy = None
no_proxy = self._in_no_proxy(parsed_dsn)

# try HTTPS first
if parsed_dsn.scheme == "https" and (https_proxy != ""):
proxy = https_proxy or getproxies().get("https")
proxy = https_proxy or (not no_proxy and getproxies().get("https"))

# maybe fallback to HTTP proxy
if not proxy and (http_proxy != ""):
proxy = http_proxy or getproxies().get("http")
proxy = http_proxy or (not no_proxy and getproxies().get("http"))

opts = self._get_pool_options(ca_certs)

Expand Down
39 changes: 39 additions & 0 deletions tests/test_client.py
Expand Up @@ -187,13 +187,52 @@ def test_transport_option(monkeypatch):
"arg_https_proxy": None,
"expected_proxy_scheme": "http",
},
# NO_PROXY testcases
{
"dsn": "http://foo@sentry.io/123",
"env_http_proxy": "http://localhost/123",
"env_https_proxy": None,
"env_no_proxy": "sentry.io,example.com",
"arg_http_proxy": None,
"arg_https_proxy": None,
"expected_proxy_scheme": None,
},
{
"dsn": "https://foo@sentry.io/123",
"env_http_proxy": None,
"env_https_proxy": "https://localhost/123",
"env_no_proxy": "example.com,sentry.io",
"arg_http_proxy": None,
"arg_https_proxy": None,
"expected_proxy_scheme": None,
},
{
"dsn": "http://foo@sentry.io/123",
"env_http_proxy": None,
"env_https_proxy": None,
"env_no_proxy": "sentry.io,example.com",
"arg_http_proxy": "http://localhost/123",
"arg_https_proxy": None,
"expected_proxy_scheme": "http",
},
{
"dsn": "https://foo@sentry.io/123",
"env_http_proxy": None,
"env_https_proxy": None,
"env_no_proxy": "sentry.io,example.com",
"arg_http_proxy": None,
"arg_https_proxy": "https://localhost/123",
"expected_proxy_scheme": "https",
},
],
)
def test_proxy(monkeypatch, testcase):
if testcase["env_http_proxy"] is not None:
monkeypatch.setenv("HTTP_PROXY", testcase["env_http_proxy"])
if testcase["env_https_proxy"] is not None:
monkeypatch.setenv("HTTPS_PROXY", testcase["env_https_proxy"])
if testcase.get("env_no_proxy") is not None:
monkeypatch.setenv("NO_PROXY", testcase["env_no_proxy"])
kwargs = {}
if testcase["arg_http_proxy"] is not None:
kwargs["http_proxy"] = testcase["arg_http_proxy"]
Expand Down