Skip to content

Properly parse proxy urls with credentials in them #242

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

Merged
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
15 changes: 10 additions & 5 deletions cloudfoundry_client/doppler/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ class DopplerClient(object):
def __init__(self, doppler_endpoint: str, proxy: str, verify_ssl: bool, credentials_manager: CredentialManager):
self.proxy_host = None
self.proxy_port = None
self.proxy_auth = None
self.ws_doppler_endpoint = doppler_endpoint
self.http_doppler_endpoint = re.sub("^ws", "http", doppler_endpoint)
self.verify_ssl = verify_ssl
self.credentials_manager = credentials_manager
if proxy is not None and len(proxy) > 0:
proxy_domain = urlparse(proxy).netloc
idx = proxy_domain.find(":")
if 0 < idx < len(proxy_domain) - 2:
self.proxy_host = proxy_domain[:idx]
self.proxy_port = int(proxy_domain[idx + 1 :])
proxy_parsed = urlparse(proxy)
self.proxy_host = proxy_parsed.hostname
if proxy_parsed.port is not None:
self.proxy_port = proxy_parsed.port
else:
self.proxy_port = 443 if proxy_parsed.scheme == "https" else 80
if proxy_parsed.username is not None and proxy_parsed.password is not None:
self.proxy_auth = (proxy_parsed.username, proxy_parsed.password)

def recent_logs(self, app_guid: str) -> EnvelopeStream:
url = "%s/apps/%s/recentlogs" % (self.http_doppler_endpoint, app_guid)
Expand All @@ -46,6 +50,7 @@ def stream_logs(self, app_guid: str) -> EnvelopeStream:
verify_ssl=self.verify_ssl,
proxy_host=self.proxy_host,
proxy_port=self.proxy_port,
proxy_auth=self.proxy_auth,
) as websocket:
for message in websocket:
yield DopplerClient._parse_envelope(message)
Expand Down
5 changes: 4 additions & 1 deletion cloudfoundry_client/doppler/websocket_envelope_reader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ssl
from typing import Callable, Optional
from typing import Callable, Optional, Tuple

import websocket

Expand All @@ -12,6 +12,7 @@ def __init__(
verify_ssl: bool = True,
proxy_host: Optional[str] = None,
proxy_port: Optional[int] = None,
proxy_auth: Optional[Tuple[str, str]] = None,
):
if not verify_ssl:
self._ws = websocket.WebSocket(sslopt=dict(cert_reqs=ssl.CERT_NONE))
Expand All @@ -20,13 +21,15 @@ def __init__(
self._url = url
self._proxy_host = proxy_host
self._proxy_port = proxy_port
self._proxy_auth = proxy_auth
self._access_token_provider = access_token_provider

def connect(self):
kw_args = dict(header=dict(Authorization="Bearer %s" % self._access_token_provider()))
if self._proxy_host is not None and self._proxy_port is not None:
kw_args["http_proxy_host"] = self._proxy_host
kw_args["http_proxy_port"] = str(self._proxy_port)
kw_args["http_proxy_auth"] = self._proxy_auth
self._ws.connect(self._url, **kw_args)

def close(self):
Expand Down
12 changes: 3 additions & 9 deletions cloudfoundry_client/rlpgateway/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from urllib.parse import urlparse

import aiohttp

Expand All @@ -15,18 +14,13 @@ class RLPGatewayClient(object):
"""

def __init__(self, rlp_gateway_endpoint, proxy, verify_ssl, credentials_manager):
self.proxy_host = None
self.proxy_port = None
self.proxy = None
self.rlp_gateway_endpoint = rlp_gateway_endpoint
self.verify_ssl = verify_ssl
self.credentials_manager = credentials_manager

if proxy is not None and len(proxy) > 0:
proxy_domain = urlparse(proxy).netloc
idx = proxy_domain.find(":")
if 0 < idx < len(proxy_domain) - 2:
self.proxy_host = proxy_domain[:idx]
self.proxy_port = int(proxy_domain[idx + 1 :])
self.proxy = proxy

async def stream_logs(self, app_guid, **kwargs):
url = f"{self.rlp_gateway_endpoint}/v2/read"
Expand All @@ -40,7 +34,7 @@ async def stream_logs(self, app_guid, **kwargs):
headers.update(kwargs["headers"])
if "params" in kwargs:
params.update(kwargs["params"])
async with aiohttp.ClientSession(headers=headers) as session:
async with aiohttp.ClientSession(headers=headers, proxy=self.proxy) as session:
async with session.get(url=url, params=params) as response:
if response.status == 204:
yield {}
Expand Down
Loading