From b61b177a3f6e00e30315ab1b2fbdd768427506a1 Mon Sep 17 00:00:00 2001 From: Matthieu Huin Date: Wed, 24 Apr 2013 18:07:01 +0200 Subject: [PATCH] Skip cname_lookup when host is an IP address cname_lookup cannot resolve IP addresses as CNAME records, and therefore should not attempt to resolve the host in that case. The middleware is skipped when the host is an IP address. Change-Id: I6961ec205e771116ace1ebcb8c088f3116eb38f0 Fixes: bug #1172289 --- swift/common/middleware/cname_lookup.py | 12 ++++++++++++ test/unit/common/middleware/test_cname_lookup.py | 11 +++++++++++ tools/pip-requires | 1 + 3 files changed, 24 insertions(+) diff --git a/swift/common/middleware/cname_lookup.py b/swift/common/middleware/cname_lookup.py index e6d8717c88..a29a86e9cf 100644 --- a/swift/common/middleware/cname_lookup.py +++ b/swift/common/middleware/cname_lookup.py @@ -27,6 +27,8 @@ maximum lookup depth. If a match is found, the environment's Host header is rewritten and the request is passed further down the WSGI chain. """ +import socket + try: import dns.resolver from dns.exception import DNSException @@ -58,6 +60,14 @@ def lookup_cname(domain): # pragma: no cover return 0, None +def is_ip(domain): + try: + socket.inet_aton(domain) + return True + except socket.error: + return False + + class CNAMELookupMiddleware(object): """ CNAME Lookup Middleware @@ -93,6 +103,8 @@ class CNAMELookupMiddleware(object): given_domain, port = given_domain.rsplit(':', 1) if given_domain == self.storage_domain[1:]: # strip initial '.' return self.app(env, start_response) + if is_ip(given_domain): + return self.app(env, start_response) a_domain = given_domain if not a_domain.endswith(self.storage_domain): if self.memcache is None: diff --git a/test/unit/common/middleware/test_cname_lookup.py b/test/unit/common/middleware/test_cname_lookup.py index d8df5cf21f..cfe1366a46 100644 --- a/test/unit/common/middleware/test_cname_lookup.py +++ b/test/unit/common/middleware/test_cname_lookup.py @@ -36,6 +36,8 @@ def start_response(*args): pass +original_lookup = cname_lookup.lookup_cname + class TestCNAMELookup(unittest.TestCase): def setUp(self): @@ -44,6 +46,15 @@ class TestCNAMELookup(unittest.TestCase): self.app = cname_lookup.CNAMELookupMiddleware(FakeApp(), {'lookup_depth': 2}) + def test_pass_ip_addresses(self): + + cname_lookup.lookup_cname = original_lookup + + req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'}, + headers={'Host': '10.134.23.198'}) + resp = self.app(req.environ, start_response) + self.assertEquals(resp, 'FAKE APP') + def test_passthrough(self): def my_lookup(d): diff --git a/tools/pip-requires b/tools/pip-requires index ff7f919b60..558b6e815f 100644 --- a/tools/pip-requires +++ b/tools/pip-requires @@ -1,3 +1,4 @@ +dnspython>=1.10.0 eventlet>=0.9.15 greenlet>=0.3.1 netifaces>=0.5