diff --git a/doc/source/logs.rst b/doc/source/logs.rst index e70de1f8ed..d7a64a802a 100644 --- a/doc/source/logs.rst +++ b/doc/source/logs.rst @@ -59,6 +59,7 @@ remote_addr The IP address of the other end of the TCP connection. (anonymizable) end_time Timestamp of the request. (timestamp) method The HTTP verb in the request. +domain The domain in the request. (anonymizable) path The path portion of the request. (anonymizable) protocol The transport protocol used (currently one of http or https). diff --git a/swift/common/middleware/proxy_logging.py b/swift/common/middleware/proxy_logging.py index 29920aaac4..d89343479b 100644 --- a/swift/common/middleware/proxy_logging.py +++ b/swift/common/middleware/proxy_logging.py @@ -151,6 +151,8 @@ class ProxyLoggingMiddleware(object): self.anonymization_salt), 'remote_addr': StrAnonymizer('4.3.2.1', self.anonymization_method, self.anonymization_salt), + 'domain': StrAnonymizer('', self.anonymization_method, + self.anonymization_salt), 'path': StrAnonymizer('/', self.anonymization_method, self.anonymization_salt), 'referer': StrAnonymizer('ref', self.anonymization_method, @@ -236,6 +238,10 @@ class ProxyLoggingMiddleware(object): :param wire_status_int: the on the wire status int """ self.obscure_req(req) + domain = req.environ.get('HTTP_HOST', + req.environ.get('SERVER_NAME', None)) + if ':' in domain: + domain, port = domain.rsplit(':', 1) resp_headers = resp_headers or {} logged_headers = None if self.log_hdrs: @@ -267,6 +273,8 @@ class ProxyLoggingMiddleware(object): 'remote_addr': StrAnonymizer(req.remote_addr, self.anonymization_method, self.anonymization_salt), + 'domain': StrAnonymizer(domain, self.anonymization_method, + self.anonymization_salt), 'path': StrAnonymizer(req.path_qs, self.anonymization_method, self.anonymization_salt), 'referer': StrAnonymizer(req.referer, self.anonymization_method, diff --git a/test/unit/common/middleware/test_proxy_logging.py b/test/unit/common/middleware/test_proxy_logging.py index 2fe7bfa438..7ab0e88750 100644 --- a/test/unit/common/middleware/test_proxy_logging.py +++ b/test/unit/common/middleware/test_proxy_logging.py @@ -423,9 +423,10 @@ class TestProxyLogging(unittest.TestCase): 'template which can be edited in config: ' '{protocol} {path} {method} ' '{path.anonymized} {container.anonymized} ' - '{request_time} {start_time.datetime} {end_time} {ttfb}')}) + '{request_time} {start_time.datetime} {end_time} {ttfb} ' + '{domain}')}) app.access_logger = debug_logger() - req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'}) + req = Request.blank('/', headers={'Host': 'example.com'}) with mock.patch('time.time', mock.MagicMock( side_effect=[10000000.0, 10000000.5, 10000001.0])): @@ -443,6 +444,7 @@ class TestProxyLogging(unittest.TestCase): self.assertEqual(log_parts[13], '26/Apr/1970/17/46/40') self.assertEqual(log_parts[14], '10000001.000000000') self.assertEqual(log_parts[15], '0.5') + self.assertEqual(log_parts[16], 'example.com') self.assertEqual(resp_body, b'FAKE APP') def test_log_msg_template_s3api(self):