From cd8af3f8f1a3762eb1591cd4b312b15d01675b74 Mon Sep 17 00:00:00 2001 From: gholt Date: Sat, 20 Apr 2013 04:16:57 +0000 Subject: [PATCH] Made colons quote-safe in logs; mainly for ipv6 Previous logging made a mess of ipv6 addresses. This just makes the colon quote-safe so it passes through unscathed. The logs will still be backward compatible because unquote don't care: $ python >>> from urllib import quote, unquote >>> quote('2001:db8:85a3:8d3:1319:8a2e:370:7348') '2001%3Adb8%3A85a3%3A8d3%3A1319%3A8a2e%3A370%3A7348' >>> unquote(quote('2001:db8:85a3:8d3:1319:8a2e:370:7348')) '2001:db8:85a3:8d3:1319:8a2e:370:7348' >>> quote('2001:db8:85a3:8d3:1319:8a2e:370:7348', '/:') '2001:db8:85a3:8d3:1319:8a2e:370:7348' >>> unquote(quote('2001:db8:85a3:8d3:1319:8a2e:370:7348', '/:')) '2001:db8:85a3:8d3:1319:8a2e:370:7348' Change-Id: Ia13a9bc8a320cde5c56034a7f0b645913428bf21 --- swift/common/middleware/proxy_logging.py | 6 ++++-- .../common/middleware/test_proxy_logging.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/swift/common/middleware/proxy_logging.py b/swift/common/middleware/proxy_logging.py index 97950a7c94..273c31064d 100644 --- a/swift/common/middleware/proxy_logging.py +++ b/swift/common/middleware/proxy_logging.py @@ -66,6 +66,8 @@ from swift.common.utils import (get_logger, get_remote_client, get_valid_utf8_str, config_true_value, InputProxy) +QUOTE_SAFE = '/:' + class ProxyLoggingMiddleware(object): """ @@ -122,7 +124,7 @@ class ProxyLoggingMiddleware(object): if self.req_already_logged(req): return req_path = get_valid_utf8_str(req.path) - the_request = quote(unquote(req_path)) + the_request = quote(unquote(req_path), QUOTE_SAFE) if req.query_string: the_request = the_request + '?' + req.query_string logged_headers = None @@ -131,7 +133,7 @@ class ProxyLoggingMiddleware(object): for k, v in req.headers.items()) method = self.method_from_req(req) self.access_logger.info(' '.join( - quote(str(x) if x else '-') + quote(str(x) if x else '-', QUOTE_SAFE) for x in ( get_remote_client(req), req.remote_addr, diff --git a/test/unit/common/middleware/test_proxy_logging.py b/test/unit/common/middleware/test_proxy_logging.py index 7c66ea2669..04b574e43b 100644 --- a/test/unit/common/middleware/test_proxy_logging.py +++ b/test/unit/common/middleware/test_proxy_logging.py @@ -519,6 +519,24 @@ class TestProxyLogging(unittest.TestCase): self.assertEquals(resp_body, 'FAKE APP') self.assertEquals(log_parts[11], str(len(resp_body))) + def test_ipv6(self): + ipv6addr = '2001:db8:85a3:8d3:1319:8a2e:370:7348' + app = proxy_logging.ProxyLoggingMiddleware(FakeApp(), {}) + app.access_logger = FakeLogger() + req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'}) + req.remote_addr = ipv6addr + resp = app(req.environ, start_response) + resp_body = ''.join(resp) + log_parts = self._log_parts(app) + self.assertEquals(log_parts[0], ipv6addr) + self.assertEquals(log_parts[1], ipv6addr) + self.assertEquals(log_parts[3], 'GET') + self.assertEquals(log_parts[4], '/') + self.assertEquals(log_parts[5], 'HTTP/1.0') + self.assertEquals(log_parts[6], '200') + self.assertEquals(resp_body, 'FAKE APP') + self.assertEquals(log_parts[11], str(len(resp_body))) + if __name__ == '__main__': unittest.main()