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
This commit is contained in:
Tim Burke 2017-05-12 10:43:30 -04:00
parent a6fb2076bb
commit 849d204c59
2 changed files with 11 additions and 6 deletions

View File

@ -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

View File

@ -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'})