From ecb8f7cdb6c7b088f6f749579f4d60c3286956bd Mon Sep 17 00:00:00 2001 From: Jiri Hnidek Date: Wed, 26 Jul 2023 16:57:24 +0200 Subject: [PATCH] Improved printing of addresses and URLs * Print IPv6 addresses correctly * Improved printing of proxy URL in debug mode --- src/rhsm/connection.py | 30 +++++++++++++++++++++++++----- test/rhsm/unit/test_connection.py | 8 ++++---- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/rhsm/connection.py b/src/rhsm/connection.py index 6814c45261..3fdc214640 100644 --- a/src/rhsm/connection.py +++ b/src/rhsm/connection.py @@ -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) @@ -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' @@ -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: @@ -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) diff --git a/test/rhsm/unit/test_connection.py b/test/rhsm/unit/test_connection.py index 01bf20866a..a304525ca5 100644 --- a/test/rhsm/unit/test_connection.py +++ b/test/rhsm/unit/test_connection.py @@ -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) @@ -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) @@ -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) @@ -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)