Merge "Accept storage_domain as a list in domain_remap"

This commit is contained in:
Jenkins 2017-03-16 12:35:48 +00:00 committed by Gerrit Code Review
commit b43414c905
4 changed files with 53 additions and 6 deletions

View File

@ -499,7 +499,7 @@ Logging address. The default is /dev/log.
.IP "\fBset log_headers\fR "
Enables the ability to log request headers. The default is False.
.IP \fBstorage_domain\fR
The domain to be used by the middleware.
The domain to be used by the middleware. Multiple domains can be specified separated by a comma.
.IP \fBpath_root\fR
The path root value for the storage URL. The default is v1.
.IP \fBreseller_prefixes\fR

View File

@ -506,7 +506,10 @@ use = egg:swift#domain_remap
# set log_headers = false
# set log_address = /dev/log
#
# Specify the storage_domain that match your cloud, multiple domains
# can be specified separated by a comma
# storage_domain = example.com
# path_root = v1
# Browsers can convert a host header to lowercase, so check that reseller

View File

@ -67,9 +67,12 @@ class DomainRemapMiddleware(object):
def __init__(self, app, conf):
self.app = app
self.storage_domain = conf.get('storage_domain', 'example.com')
if self.storage_domain and not self.storage_domain.startswith('.'):
self.storage_domain = '.' + self.storage_domain
storage_domain = conf.get('storage_domain', 'example.com')
self.storage_domain = ['.' + s for s in
list_from_csv(storage_domain)
if not s.startswith('.')]
self.storage_domain += [s for s in list_from_csv(storage_domain)
if s.startswith('.')]
self.path_root = conf.get('path_root', 'v1').strip('/')
prefixes = conf.get('reseller_prefixes', 'AUTH')
self.reseller_prefixes = list_from_csv(prefixes)
@ -87,8 +90,10 @@ class DomainRemapMiddleware(object):
port = ''
if ':' in given_domain:
given_domain, port = given_domain.rsplit(':', 1)
if given_domain.endswith(self.storage_domain):
parts_to_parse = given_domain[:-len(self.storage_domain)]
storage_domain = next((domain for domain in self.storage_domain
if given_domain.endswith(domain)), None)
if storage_domain:
parts_to_parse = given_domain[:-len(storage_domain)]
parts_to_parse = parts_to_parse.strip('.').split('.')
len_parts_to_parse = len(parts_to_parse)
if len_parts_to_parse == 2:

View File

@ -115,6 +115,27 @@ class TestDomainRemap(unittest.TestCase):
resp = self.app(req.environ, start_response)
self.assertEqual(resp, '/test')
def test_storage_domains_conf_format(self):
conf = {'storage_domain': 'foo.com'}
app = domain_remap.filter_factory(conf)(FakeApp())
self.assertEqual(app.storage_domain, ['.foo.com'])
conf = {'storage_domain': 'foo.com, '}
app = domain_remap.filter_factory(conf)(FakeApp())
self.assertEqual(app.storage_domain, ['.foo.com'])
conf = {'storage_domain': 'foo.com, bar.com'}
app = domain_remap.filter_factory(conf)(FakeApp())
self.assertEqual(app.storage_domain, ['.foo.com', '.bar.com'])
conf = {'storage_domain': 'foo.com, .bar.com'}
app = domain_remap.filter_factory(conf)(FakeApp())
self.assertEqual(app.storage_domain, ['.foo.com', '.bar.com'])
conf = {'storage_domain': '.foo.com, .bar.com'}
app = domain_remap.filter_factory(conf)(FakeApp())
self.assertEqual(app.storage_domain, ['.foo.com', '.bar.com'])
def test_domain_remap_configured_with_prefixes(self):
conf = {'reseller_prefixes': 'PREFIX'}
self.app = domain_remap.DomainRemapMiddleware(FakeApp(), conf)
@ -155,6 +176,24 @@ class TestDomainRemap(unittest.TestCase):
resp = self.app(req.environ, start_response)
self.assertEqual(resp, '/v1/AUTH_uuid/test')
def test_multiple_storage_domains(self):
conf = {'storage_domain': 'storage1.com, storage2.com'}
self.app = domain_remap.DomainRemapMiddleware(FakeApp(), conf)
def do_test(host):
req = Request.blank('/test', environ={'REQUEST_METHOD': 'GET'},
headers={'Host': host})
return self.app(req.environ, start_response)
resp = do_test('auth-uuid.storage1.com')
self.assertEqual(resp, '/v1/AUTH_uuid/test')
resp = do_test('auth-uuid.storage2.com')
self.assertEqual(resp, '/v1/AUTH_uuid/test')
resp = do_test('auth-uuid.storage3.com')
self.assertEqual(resp, '/test')
class TestSwiftInfo(unittest.TestCase):
def setUp(self):