diff --git a/doc/manpages/proxy-server.conf.5 b/doc/manpages/proxy-server.conf.5 index c860743b96..26d639c12c 100644 --- a/doc/manpages/proxy-server.conf.5 +++ b/doc/manpages/proxy-server.conf.5 @@ -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 diff --git a/etc/proxy-server.conf-sample b/etc/proxy-server.conf-sample index 1b6b133003..a937275c9b 100644 --- a/etc/proxy-server.conf-sample +++ b/etc/proxy-server.conf-sample @@ -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 diff --git a/swift/common/middleware/domain_remap.py b/swift/common/middleware/domain_remap.py index 07376b7357..60f3d475a2 100644 --- a/swift/common/middleware/domain_remap.py +++ b/swift/common/middleware/domain_remap.py @@ -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: diff --git a/test/unit/common/middleware/test_domain_remap.py b/test/unit/common/middleware/test_domain_remap.py index d02327ddfd..48b9cec08b 100644 --- a/test/unit/common/middleware/test_domain_remap.py +++ b/test/unit/common/middleware/test_domain_remap.py @@ -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):