proxy-logging: Allow to add domain in log messages

Change-Id: Id441688aac1088041e243b8ee70710d9c5d7911b
This commit is contained in:
Aymeric Ducroquetz 2022-06-20 18:13:33 +02:00 committed by Tim Burke
parent 26b2425ab8
commit 1831658b33
3 changed files with 13 additions and 2 deletions

View File

@ -59,6 +59,7 @@ remote_addr The IP address of the other end of the TCP connection.
(anonymizable) (anonymizable)
end_time Timestamp of the request. (timestamp) end_time Timestamp of the request. (timestamp)
method The HTTP verb in the request. method The HTTP verb in the request.
domain The domain in the request. (anonymizable)
path The path portion of the request. (anonymizable) path The path portion of the request. (anonymizable)
protocol The transport protocol used (currently one of http or protocol The transport protocol used (currently one of http or
https). https).

View File

@ -151,6 +151,8 @@ class ProxyLoggingMiddleware(object):
self.anonymization_salt), self.anonymization_salt),
'remote_addr': StrAnonymizer('4.3.2.1', self.anonymization_method, 'remote_addr': StrAnonymizer('4.3.2.1', self.anonymization_method,
self.anonymization_salt), self.anonymization_salt),
'domain': StrAnonymizer('', self.anonymization_method,
self.anonymization_salt),
'path': StrAnonymizer('/', self.anonymization_method, 'path': StrAnonymizer('/', self.anonymization_method,
self.anonymization_salt), self.anonymization_salt),
'referer': StrAnonymizer('ref', self.anonymization_method, 'referer': StrAnonymizer('ref', self.anonymization_method,
@ -236,6 +238,10 @@ class ProxyLoggingMiddleware(object):
:param wire_status_int: the on the wire status int :param wire_status_int: the on the wire status int
""" """
self.obscure_req(req) 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 {} resp_headers = resp_headers or {}
logged_headers = None logged_headers = None
if self.log_hdrs: if self.log_hdrs:
@ -267,6 +273,8 @@ class ProxyLoggingMiddleware(object):
'remote_addr': StrAnonymizer(req.remote_addr, 'remote_addr': StrAnonymizer(req.remote_addr,
self.anonymization_method, self.anonymization_method,
self.anonymization_salt), self.anonymization_salt),
'domain': StrAnonymizer(domain, self.anonymization_method,
self.anonymization_salt),
'path': StrAnonymizer(req.path_qs, self.anonymization_method, 'path': StrAnonymizer(req.path_qs, self.anonymization_method,
self.anonymization_salt), self.anonymization_salt),
'referer': StrAnonymizer(req.referer, self.anonymization_method, 'referer': StrAnonymizer(req.referer, self.anonymization_method,

View File

@ -423,9 +423,10 @@ class TestProxyLogging(unittest.TestCase):
'template which can be edited in config: ' 'template which can be edited in config: '
'{protocol} {path} {method} ' '{protocol} {path} {method} '
'{path.anonymized} {container.anonymized} ' '{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() app.access_logger = debug_logger()
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'}) req = Request.blank('/', headers={'Host': 'example.com'})
with mock.patch('time.time', with mock.patch('time.time',
mock.MagicMock( mock.MagicMock(
side_effect=[10000000.0, 10000000.5, 10000001.0])): 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[13], '26/Apr/1970/17/46/40')
self.assertEqual(log_parts[14], '10000001.000000000') self.assertEqual(log_parts[14], '10000001.000000000')
self.assertEqual(log_parts[15], '0.5') self.assertEqual(log_parts[15], '0.5')
self.assertEqual(log_parts[16], 'example.com')
self.assertEqual(resp_body, b'FAKE APP') self.assertEqual(resp_body, b'FAKE APP')
def test_log_msg_template_s3api(self): def test_log_msg_template_s3api(self):