From f69c2569d65c5d9512d8fda0af4e58820faa4498 Mon Sep 17 00:00:00 2001 From: Aaron Tan Date: Fri, 17 Mar 2017 17:54:29 -0400 Subject: [PATCH 1/2] Smarter log aggregator host name generation. --- awx/main/utils/handlers.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/awx/main/utils/handlers.py b/awx/main/utils/handlers.py index b1b96b492f..2346ee5c30 100644 --- a/awx/main/utils/handlers.py +++ b/awx/main/utils/handlers.py @@ -6,6 +6,7 @@ import logging import json import requests import time +import re from concurrent.futures import ThreadPoolExecutor from copy import copy @@ -118,10 +119,18 @@ class BaseHTTPSHandler(logging.Handler): def get_http_host(self): host = self.host or '' - if not host.startswith('http'): - host = 'http://%s' % self.host - if self.port != 80 and self.port is not None: - host = '%s:%s' % (host, str(self.port)) + # Force using http(s) protocol + if re.match(r'https?://', host) is None: + if re.match(r'[a-zA-Z]+://', host) is None: + host = 'http://%s' % self.host + else: + host = re.sub(r'[a-zA-Z]+://', r'http://', host, count=1) + # Insert self.port if its special and port number not given in host + if (self.port != 80 and self.port is not None and + re.match(r'https?://[^:/]+:[0-9]+', host) is None): + host = re.sub(r'https?://[^/]+', + lambda m: '%s:%s' % (m.group(0), str(self.port), + host, count=1) return host def get_post_kwargs(self, payload_input): From 07e7e4cfd04330d477b0a94753ca7b479f70badf Mon Sep 17 00:00:00 2001 From: Aaron Tan Date: Mon, 20 Mar 2017 14:27:02 -0400 Subject: [PATCH 2/2] Modify according to review feedback. --- awx/main/tests/unit/utils/test_handlers.py | 12 ++++++++- awx/main/utils/handlers.py | 29 ++++++++++++---------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/awx/main/tests/unit/utils/test_handlers.py b/awx/main/tests/unit/utils/test_handlers.py index d7b7f5aed9..518a213669 100644 --- a/awx/main/tests/unit/utils/test_handlers.py +++ b/awx/main/tests/unit/utils/test_handlers.py @@ -107,7 +107,17 @@ def test_https_logging_handler_splunk_auth_info(): ('http://localhost', None, 'http://localhost'), ('http://localhost', 80, 'http://localhost'), ('http://localhost', 8080, 'http://localhost:8080'), - ('https://localhost', 443, 'https://localhost:443') + ('https://localhost', 443, 'https://localhost:443'), + ('ftp://localhost', 443, 'ftp://localhost:443'), + ('https://localhost:550', 443, 'https://localhost:550'), + ('https://localhost:yoho/foobar', 443, 'https://localhost:443/foobar'), + ('https://localhost:yoho/foobar', None, 'https://localhost:yoho/foobar'), + ('http://splunk.server:8088/services/collector/event', 80, + 'http://splunk.server:8088/services/collector/event'), + ('http://splunk.server/services/collector/event', 80, + 'http://splunk.server/services/collector/event'), + ('http://splunk.server/services/collector/event', 8088, + 'http://splunk.server:8088/services/collector/event'), ]) def test_https_logging_handler_http_host_format(host, port, normalized): handler = HTTPSHandler(host=host, port=port) diff --git a/awx/main/utils/handlers.py b/awx/main/utils/handlers.py index 2346ee5c30..3eb9852472 100644 --- a/awx/main/utils/handlers.py +++ b/awx/main/utils/handlers.py @@ -6,7 +6,7 @@ import logging import json import requests import time -import re +import urlparse from concurrent.futures import ThreadPoolExecutor from copy import copy @@ -119,18 +119,21 @@ class BaseHTTPSHandler(logging.Handler): def get_http_host(self): host = self.host or '' - # Force using http(s) protocol - if re.match(r'https?://', host) is None: - if re.match(r'[a-zA-Z]+://', host) is None: - host = 'http://%s' % self.host - else: - host = re.sub(r'[a-zA-Z]+://', r'http://', host, count=1) - # Insert self.port if its special and port number not given in host - if (self.port != 80 and self.port is not None and - re.match(r'https?://[^:/]+:[0-9]+', host) is None): - host = re.sub(r'https?://[^/]+', - lambda m: '%s:%s' % (m.group(0), str(self.port), - host, count=1) + # urlparse requires scheme to be provided, default to use http if + # missing + if not urlparse.urlsplit(host).scheme: + host = 'http://%s' % host + parsed = urlparse.urlsplit(host) + # Insert self.port if its special and port number is either not + # given in host or given as non-numerical + try: + port = parsed.port or self.port + except ValueError: + port = self.port + if port not in (80, None): + new_netloc = '%s:%s' % (parsed.hostname, port) + return urlparse.urlunsplit((parsed.scheme, new_netloc, parsed.path, + parsed.query, parsed.fragment)) return host def get_post_kwargs(self, payload_input):