Accept storage_domain as a list in domain_remap

Middleware domain_remap can work with cname_lookup middleware. This last
middleware accept that storage_domain is a list of domains. To be
consistent, domain_remap should have the same behavior.

Closes-Bug: #1664647

Change-Id: Iacc6619968cc7c677bf63e0b8d101a20c86ce599
This commit is contained in:
Romain LE DISEZ 2017-02-18 10:19:20 +01:00
parent 11bd6c82ec
commit 5c93d6f238
4 changed files with 53 additions and 6 deletions

View File

@ -497,7 +497,7 @@ Logging address. The default is /dev/log.
.IP "\fBset log_headers\fR " .IP "\fBset log_headers\fR "
Enables the ability to log request headers. The default is False. Enables the ability to log request headers. The default is False.
.IP \fBstorage_domain\fR .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 .IP \fBpath_root\fR
The path root value for the storage URL. The default is v1. The path root value for the storage URL. The default is v1.
.IP \fBreseller_prefixes\fR .IP \fBreseller_prefixes\fR

View File

@ -503,7 +503,10 @@ use = egg:swift#domain_remap
# set log_headers = false # set log_headers = false
# set log_address = /dev/log # 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 # storage_domain = example.com
# path_root = v1 # path_root = v1
# Browsers can convert a host header to lowercase, so check that reseller # 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): def __init__(self, app, conf):
self.app = app self.app = app
self.storage_domain = conf.get('storage_domain', 'example.com') storage_domain = conf.get('storage_domain', 'example.com')
if self.storage_domain and not self.storage_domain.startswith('.'): self.storage_domain = ['.' + s for s in
self.storage_domain = '.' + self.storage_domain 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('/') self.path_root = conf.get('path_root', 'v1').strip('/')
prefixes = conf.get('reseller_prefixes', 'AUTH') prefixes = conf.get('reseller_prefixes', 'AUTH')
self.reseller_prefixes = list_from_csv(prefixes) self.reseller_prefixes = list_from_csv(prefixes)
@ -87,8 +90,10 @@ class DomainRemapMiddleware(object):
port = '' port = ''
if ':' in given_domain: if ':' in given_domain:
given_domain, port = given_domain.rsplit(':', 1) given_domain, port = given_domain.rsplit(':', 1)
if given_domain.endswith(self.storage_domain): storage_domain = next((domain for domain in self.storage_domain
parts_to_parse = given_domain[:-len(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('.') parts_to_parse = parts_to_parse.strip('.').split('.')
len_parts_to_parse = len(parts_to_parse) len_parts_to_parse = len(parts_to_parse)
if len_parts_to_parse == 2: if len_parts_to_parse == 2:

View File

@ -115,6 +115,27 @@ class TestDomainRemap(unittest.TestCase):
resp = self.app(req.environ, start_response) resp = self.app(req.environ, start_response)
self.assertEqual(resp, '/test') 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): def test_domain_remap_configured_with_prefixes(self):
conf = {'reseller_prefixes': 'PREFIX'} conf = {'reseller_prefixes': 'PREFIX'}
self.app = domain_remap.DomainRemapMiddleware(FakeApp(), conf) self.app = domain_remap.DomainRemapMiddleware(FakeApp(), conf)
@ -155,6 +176,24 @@ class TestDomainRemap(unittest.TestCase):
resp = self.app(req.environ, start_response) resp = self.app(req.environ, start_response)
self.assertEqual(resp, '/v1/AUTH_uuid/test') 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): class TestSwiftInfo(unittest.TestCase):
def setUp(self): def setUp(self):