From 849d204c596c9089dab606ece72c84092ad156ca Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Fri, 12 May 2017 10:43:30 -0400 Subject: [PATCH] domain_remap: be more careful about client-path mangling The root_path option for domain_remap seems to serve two purposes: - provide the first component (version) for the backend request - be an optional leading component for the client request, which should be stripped off As a result, we have mappings like: c.a.example.com/ -> /v1/AUTH_a/c/ c.a.example.com/o -> /v1/AUTH_a/c/o c.a.example.com/v1/o -> /v1/AUTH_a/c/o Currently, we don't really care about whether there was a full- or partial-match in that first component, leading to mappings like c.a.example.com/v1o -> /v1/AUTH_a/c/o If we're going to continue supporting that second function, we should only consider full-matches, so we'll have c.a.example.com/v1o -> /v1/AUTH_a/c/v1o Change-Id: Ibdc97bb8daf117ad46177617f170d03e481b0007 --- swift/common/middleware/domain_remap.py | 11 +++++------ test/unit/common/middleware/test_domain_remap.py | 6 ++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/swift/common/middleware/domain_remap.py b/swift/common/middleware/domain_remap.py index 13cfee95cf..c0e6d91bc8 100644 --- a/swift/common/middleware/domain_remap.py +++ b/swift/common/middleware/domain_remap.py @@ -78,7 +78,7 @@ class DomainRemapMiddleware(object): 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') self.reseller_prefixes = list_from_csv(prefixes) self.reseller_prefixes_lower = [x.lower() @@ -129,14 +129,13 @@ class DomainRemapMiddleware(object): # account prefix is not in config list. bail. return self.app(env, start_response) - requested_path = path = env['PATH_INFO'] - new_path_parts = [self.path_root, account] + requested_path = env['PATH_INFO'] + path = requested_path[1:] + new_path_parts = ['', self.path_root[:-1], account] if container: new_path_parts.append(container) - if path.startswith(self.path_root): + if (path + '/').startswith(self.path_root): path = path[len(self.path_root):] - if path.startswith('/'): - path = path[1:] new_path_parts.append(path) new_path = '/'.join(new_path_parts) env['PATH_INFO'] = new_path diff --git a/test/unit/common/middleware/test_domain_remap.py b/test/unit/common/middleware/test_domain_remap.py index 0b5e2fab74..d304711a13 100644 --- a/test/unit/common/middleware/test_domain_remap.py +++ b/test/unit/common/middleware/test_domain_remap.py @@ -131,6 +131,12 @@ class TestDomainRemap(unittest.TestCase): resp = self.app(req.environ, start_response) self.assertEqual(resp, ['/v1/AUTH_a/c/obj']) + def test_domain_remap_with_path_root_and_path_no_slash(self): + req = Request.blank('/v1obj', environ={'REQUEST_METHOD': 'GET'}, + headers={'Host': 'c.AUTH_a.example.com'}) + resp = self.app(req.environ, start_response) + self.assertEqual(resp, ['/v1/AUTH_a/c/v1obj']) + def test_domain_remap_account_matching_ending_not_domain(self): req = Request.blank('/dontchange', environ={'REQUEST_METHOD': 'GET'}, headers={'Host': 'c.aexample.com'})