9a17e61e7f
Commit bfbf0d1e78
removed a check that was
meant to avoid to resolve a storage domain. It breaks the behavior of the
middleware as the resolution of a storage domain will return nothing, so
the global resolution will fail.
Example:
Host header: storage.example.com
storage_domain: [.storage.example.com]
Host does not end with one of the storage_domains (because of the dot),
so the middleware will loop to resolve the CNAME of storage.example.com,
but it won't succeed because it's a storage_domain.
Closes-Bug: #1311435
Change-Id: If594b816ff2f7025521de716b32c42bf3137f5dd
288 lines
11 KiB
Python
288 lines
11 KiB
Python
# Copyright (c) 2010-2012 OpenStack Foundation
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
# implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import unittest
|
|
import mock
|
|
from nose import SkipTest
|
|
|
|
try:
|
|
# this test requires the dnspython package to be installed
|
|
import dns.resolver # noqa
|
|
except ImportError:
|
|
skip = True
|
|
else: # executed if the try has no errors
|
|
skip = False
|
|
from swift.common import utils
|
|
from swift.common.middleware import cname_lookup
|
|
from swift.common.swob import Request
|
|
|
|
|
|
class FakeApp(object):
|
|
|
|
def __call__(self, env, start_response):
|
|
return "FAKE APP"
|
|
|
|
|
|
def start_response(*args):
|
|
pass
|
|
|
|
|
|
original_lookup = cname_lookup.lookup_cname
|
|
|
|
|
|
class TestCNAMELookup(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
if skip:
|
|
raise SkipTest
|
|
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.assertEqual(resp, 'FAKE APP')
|
|
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': 'fc00:7ea1:f155::6321:8841'})
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
|
|
def test_passthrough(self):
|
|
|
|
def my_lookup(d):
|
|
return 0, d
|
|
cname_lookup.lookup_cname = my_lookup
|
|
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': 'foo.example.com'})
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': 'foo.example.com:8080'})
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET',
|
|
'SERVER_NAME': 'foo.example.com'},
|
|
headers={'Host': None})
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
|
|
def test_good_lookup(self):
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': 'mysite.com'})
|
|
|
|
def my_lookup(d):
|
|
return 0, '%s.example.com' % d
|
|
cname_lookup.lookup_cname = my_lookup
|
|
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': 'mysite.com:8080'})
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET',
|
|
'SERVER_NAME': 'mysite.com'},
|
|
headers={'Host': None})
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
|
|
def test_lookup_chain_too_long(self):
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': 'mysite.com'})
|
|
|
|
def my_lookup(d):
|
|
if d == 'mysite.com':
|
|
site = 'level1.foo.com'
|
|
elif d == 'level1.foo.com':
|
|
site = 'level2.foo.com'
|
|
elif d == 'level2.foo.com':
|
|
site = 'bar.example.com'
|
|
return 0, site
|
|
cname_lookup.lookup_cname = my_lookup
|
|
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp, ['CNAME lookup failed after 2 tries'])
|
|
|
|
def test_lookup_chain_bad_target(self):
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': 'mysite.com'})
|
|
|
|
def my_lookup(d):
|
|
return 0, 'some.invalid.site.com'
|
|
cname_lookup.lookup_cname = my_lookup
|
|
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp,
|
|
['CNAME lookup failed to resolve to a valid domain'])
|
|
|
|
def test_something_weird(self):
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': 'mysite.com'})
|
|
|
|
def my_lookup(d):
|
|
return 0, None
|
|
cname_lookup.lookup_cname = my_lookup
|
|
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp,
|
|
['CNAME lookup failed to resolve to a valid domain'])
|
|
|
|
def test_with_memcache(self):
|
|
def my_lookup(d):
|
|
return 0, '%s.example.com' % d
|
|
cname_lookup.lookup_cname = my_lookup
|
|
|
|
class memcache_stub(object):
|
|
def __init__(self):
|
|
self.cache = {}
|
|
|
|
def get(self, key):
|
|
return self.cache.get(key, None)
|
|
|
|
def set(self, key, value, *a, **kw):
|
|
self.cache[key] = value
|
|
memcache = memcache_stub()
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET',
|
|
'swift.cache': memcache},
|
|
headers={'Host': 'mysite.com'})
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET',
|
|
'swift.cache': memcache},
|
|
headers={'Host': 'mysite.com'})
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
|
|
def test_cname_matching_ending_not_domain(self):
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': 'foo.com'})
|
|
|
|
def my_lookup(d):
|
|
return 0, 'c.aexample.com'
|
|
cname_lookup.lookup_cname = my_lookup
|
|
|
|
resp = self.app(req.environ, start_response)
|
|
self.assertEqual(resp,
|
|
['CNAME lookup failed to resolve to a valid domain'])
|
|
|
|
def test_cname_configured_with_empty_storage_domain(self):
|
|
app = cname_lookup.CNAMELookupMiddleware(FakeApp(),
|
|
{'storage_domain': '',
|
|
'lookup_depth': 2})
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': 'c.a.example.com'})
|
|
|
|
def my_lookup(d):
|
|
return 0, None
|
|
cname_lookup.lookup_cname = my_lookup
|
|
|
|
resp = app(req.environ, start_response)
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
|
|
def test_storage_domains_conf_format(self):
|
|
conf = {'storage_domain': 'foo.com'}
|
|
app = cname_lookup.filter_factory(conf)(FakeApp())
|
|
self.assertEqual(app.storage_domain, ['.foo.com'])
|
|
|
|
conf = {'storage_domain': 'foo.com, '}
|
|
app = cname_lookup.filter_factory(conf)(FakeApp())
|
|
self.assertEqual(app.storage_domain, ['.foo.com'])
|
|
|
|
conf = {'storage_domain': 'foo.com, bar.com'}
|
|
app = cname_lookup.filter_factory(conf)(FakeApp())
|
|
self.assertEqual(app.storage_domain, ['.foo.com', '.bar.com'])
|
|
|
|
conf = {'storage_domain': 'foo.com, .bar.com'}
|
|
app = cname_lookup.filter_factory(conf)(FakeApp())
|
|
self.assertEqual(app.storage_domain, ['.foo.com', '.bar.com'])
|
|
|
|
conf = {'storage_domain': '.foo.com, .bar.com'}
|
|
app = cname_lookup.filter_factory(conf)(FakeApp())
|
|
self.assertEqual(app.storage_domain, ['.foo.com', '.bar.com'])
|
|
|
|
def test_multiple_storage_domains(self):
|
|
conf = {'storage_domain': 'storage1.com, storage2.com',
|
|
'lookup_depth': 2}
|
|
app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)
|
|
|
|
def do_test(lookup_back):
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': 'c.a.example.com'})
|
|
module = 'swift.common.middleware.cname_lookup.lookup_cname'
|
|
with mock.patch(module, lambda x: (0, lookup_back)):
|
|
return app(req.environ, start_response)
|
|
|
|
resp = do_test('c.storage1.com')
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
|
|
resp = do_test('c.storage2.com')
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
|
|
bad_domain = ['CNAME lookup failed to resolve to a valid domain']
|
|
resp = do_test('c.badtest.com')
|
|
self.assertEqual(resp, bad_domain)
|
|
|
|
def test_host_is_storage_domain(self):
|
|
conf = {'storage_domain': 'storage.example.com',
|
|
'lookup_depth': 2}
|
|
app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)
|
|
|
|
def do_test(host):
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': host})
|
|
return app(req.environ, start_response)
|
|
|
|
bad_domain = ['CNAME lookup failed after 2 tries']
|
|
resp = do_test('c.badtest.com')
|
|
self.assertEqual(resp, bad_domain)
|
|
|
|
resp = do_test('storage.example.com')
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
|
|
def test_resolution_to_storage_domain_exactly(self):
|
|
conf = {'storage_domain': 'example.com',
|
|
'lookup_depth': 1}
|
|
app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)
|
|
|
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
|
headers={'Host': 'mysite.com'})
|
|
module = 'swift.common.middleware.cname_lookup.lookup_cname'
|
|
with mock.patch(module, lambda x: (0, 'example.com')):
|
|
resp = app(req.environ, start_response)
|
|
self.assertEqual(resp, 'FAKE APP')
|
|
|
|
|
|
class TestSwiftInfo(unittest.TestCase):
|
|
def setUp(self):
|
|
utils._swift_info = {}
|
|
utils._swift_admin_info = {}
|
|
|
|
def test_registered_defaults(self):
|
|
cname_lookup.filter_factory({})
|
|
swift_info = utils.get_swift_info()
|
|
self.assertIn('cname_lookup', swift_info)
|
|
self.assertEqual(swift_info['cname_lookup'].get('lookup_depth'), 1)
|
|
|
|
def test_registered_nondefaults(self):
|
|
cname_lookup.filter_factory({'lookup_depth': '2'})
|
|
swift_info = utils.get_swift_info()
|
|
self.assertIn('cname_lookup', swift_info)
|
|
self.assertEqual(swift_info['cname_lookup'].get('lookup_depth'), 2)
|