Skip to content

Commit

Permalink
Improved printing of addresses and URLs
Browse files Browse the repository at this point in the history
* Print IPv6 addresses correctly
* Improved printing of proxy URL in debug mode
  • Loading branch information
jirihnidek committed Oct 16, 2023
1 parent 7028232 commit ecb8f7c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
30 changes: 25 additions & 5 deletions src/rhsm/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,12 @@ def __init__(
connection_description = ""
if proxy_description:
connection_description += proxy_description
connection_description += "host=%s port=%s handler=%s %s" % (self.host, self.ssl_port,
self.handler, auth_description)
connection_description += "host=%s port=%s handler=%s %s" % (
normalized_host(self.host),
safe_int(self.ssl_port),
self.handler,
auth_description,
)
log.debug("Connection built: %s", connection_description)


Expand Down Expand Up @@ -679,6 +683,7 @@ def _print_debug_info_about_request(self, request_type, handler, final_headers,

if 'SUBMAN_DEBUG_PRINT_REQUEST' in os.environ:
yellow_col = '\033[93m'
magenta_col = "\033[95m"
blue_col = '\033[94m'
green_col = '\033[92m'
red_col = '\033[91m'
Expand All @@ -687,9 +692,24 @@ def _print_debug_info_about_request(self, request_type, handler, final_headers,
msg = blue_col + "Making insecure request:" + end_col
else:
msg = blue_col + "Making request:" + end_col
msg += red_col + " %s:%s %s %s" % (self.host, self.ssl_port, request_type, handler) + end_col
msg += (
red_col +
" https://" +
f"{normalized_host(self.host)}:{safe_int(self.ssl_port)}{handler} {request_type}" +
end_col
)
if self.proxy_hostname and self.proxy_port:
msg += blue_col + " using proxy " + red_col + f"{self.proxy_hostname}:{self.proxy_port}" + end_col
# Note: using only https:// is not a mistake. We use only https for proxy connection.
msg += blue_col + " Using proxy: " + magenta_col + "https://"
# Print username and eventually password
if self.proxy_user:
if self.proxy_user and self.proxy_password:
msg += f"{self.proxy_user}:{self.proxy_password}@"
elif self.proxy_user and not self.proxy_password:
msg += f"{self.proxy_user}@"
# Print hostname and port
msg += f"{normalized_host(self.proxy_hostname)}:{safe_int(self.proxy_port)}"
msg += end_col
if 'SUBMAN_DEBUG_PRINT_REQUEST_HEADER' in os.environ:
msg += blue_col + " %s" % final_headers + end_col
if 'SUBMAN_DEBUG_PRINT_REQUEST_BODY' in os.environ and body is not None:
Expand Down Expand Up @@ -849,7 +869,7 @@ def _request(self, request_type, method, info=None, headers=None, cert_key_pairs
if self.cert_dir:
raise NoValidEntitlement(
"Cannot access CDN content on: %s using any of entitlement cert-key pair: %s" %
(self.host, cert_key_pairs)
(normalized_host(self.host), cert_key_pairs)
)

self._print_debug_info_about_response(result)
Expand Down
8 changes: 4 additions & 4 deletions test/rhsm/unit/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def mock_config_without_proxy_settings(section, name):
def test_https_proxy_info_allcaps(self):
with patch.dict('os.environ', {'HTTPS_PROXY': 'http://u:p@host:4444'}):
with patch.object(connection.config, 'get', self.mock_config_without_proxy_settings):
uep = UEPConnection(username="dummy", password="dummy",
uep = UEPConnection(host="dummy", username="dummy", password="dummy",
handler="/Test/", insecure=True)
self.assertEqual("u", uep.proxy_user)
self.assertEqual("p", uep.proxy_password)
Expand All @@ -177,7 +177,7 @@ def test_order(self):
with patch.dict('os.environ', {'HTTPS_PROXY': 'http://u:p@host:4444',
'http_proxy': 'http://notme:orme@host:2222'}):
with patch.object(connection.config, 'get', self.mock_config_without_proxy_settings):
uep = UEPConnection(username="dummy", password="dummy",
uep = UEPConnection(host="dummy", username="dummy", password="dummy",
handler="/Test/", insecure=True)
self.assertEqual("u", uep.proxy_user)
self.assertEqual("p", uep.proxy_password)
Expand All @@ -187,7 +187,7 @@ def test_order(self):
def test_no_port(self):
with patch.dict('os.environ', {'HTTPS_PROXY': 'http://u:p@host'}):
with patch.object(connection.config, 'get', self.mock_config_without_proxy_settings):
uep = UEPConnection(username="dummy", password="dummy",
uep = UEPConnection(host="dummy", username="dummy", password="dummy",
handler="/Test/", insecure=True)
self.assertEqual("u", uep.proxy_user)
self.assertEqual("p", uep.proxy_password)
Expand All @@ -197,7 +197,7 @@ def test_no_port(self):
def test_no_user_or_password(self):
with patch.dict('os.environ', {'HTTPS_PROXY': 'http://host:1111'}):
with patch.object(connection.config, 'get', self.mock_config_without_proxy_settings):
uep = UEPConnection(username="dummy", password="dummy",
uep = UEPConnection(host="dummy", username="dummy", password="dummy",
handler="/Test/", insecure=True)
self.assertEqual(None, uep.proxy_user)
self.assertEqual(None, uep.proxy_password)
Expand Down

0 comments on commit ecb8f7c

Please sign in to comment.