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

feat: Add support for NO_PROXY for websocket connections #16538

Merged
merged 4 commits into from
Dec 31, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/prefect/events/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
cast,
)
from urllib.parse import urlparse
from urllib.request import proxy_bypass
from uuid import UUID

import orjson
Expand Down Expand Up @@ -95,6 +96,9 @@ def __init__(self: Self, uri: str, **kwargs: Any):
u = urlparse(uri)
host = u.hostname

if not host:
raise ValueError(f"Invalid URI {uri}, no hostname found")

if u.scheme == "ws":
port = u.port or 80
proxy_url = os.environ.get("HTTP_PROXY")
Expand All @@ -107,7 +111,9 @@ def __init__(self: Self, uri: str, **kwargs: Any):
"Unsupported scheme %s. Expected 'ws' or 'wss'. " % u.scheme
)

self._proxy = Proxy.from_url(proxy_url) if proxy_url else None
self._proxy = (
Proxy.from_url(proxy_url) if proxy_url and not proxy_bypass(host) else None
)
self._host = host
self._port = port

Expand Down
36 changes: 34 additions & 2 deletions tests/utilities/test_proxy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from unittest.mock import Mock

import pytest

from prefect.events.clients import WebsocketProxyConnect


Expand Down Expand Up @@ -32,11 +34,41 @@ def test_init_ws_with_proxy(monkeypatch):


def test_init_wss_with_proxy(monkeypatch):
monkeypatch.setenv("HTTPS_PROXY", "https://proxy:3128")
monkeypatch.setenv("HTTPS_PROXY", "http://proxy:3128")
jbw-vtl marked this conversation as resolved.
Show resolved Hide resolved
mock_proxy = Mock()
monkeypatch.setattr("prefect.events.clients.Proxy", mock_proxy)

client = WebsocketProxyConnect("wss://example.com")

mock_proxy.from_url.assert_called_once_with("https://proxy:3128")
mock_proxy.from_url.assert_called_once_with("http://proxy:3128")
assert client._proxy is not None


@pytest.mark.parametrize(
"no_proxy", ["example.com", ".com", "example.com,example2.com"]
)
def test_init_ws_with_no_proxy(monkeypatch, no_proxy: str):
# Validate http proxy is set
monkeypatch.setenv("HTTP_PROXY", "http://proxy:3128")
client = WebsocketProxyConnect("ws://example.com")
assert client._proxy is not None

# Keeping existing proxy configuration, validate we can disable it with NO_PROXY
monkeypatch.setenv("NO_PROXY", no_proxy)
client = WebsocketProxyConnect("ws://example.com")
assert client._proxy is None


@pytest.mark.parametrize(
"no_proxy", ["example.com", ".com", "example.com,example2.com"]
)
def test_init_wss_with_no_proxy(monkeypatch, no_proxy: str):
# Validate https proxy is set
monkeypatch.setenv("HTTPS_PROXY", "http://proxy:3128")
client = WebsocketProxyConnect("wss://example.com")
assert client._proxy is not None

# Keeping existing proxy configuration, validate we can disable it with NO_PROXY
monkeypatch.setenv("NO_PROXY", no_proxy)
client = WebsocketProxyConnect("wss://example.com")
assert client._proxy is None
Loading