diff --git a/barbican/api/controllers/versions.py b/barbican/api/controllers/versions.py index 62610865d..e73259fe8 100644 --- a/barbican/api/controllers/versions.py +++ b/barbican/api/controllers/versions.py @@ -43,6 +43,8 @@ def _get_versioned_url(version): version += '/' # If host_href is not set in barbican conf, then derive it from request url host_part = utils.get_base_url_from_request() + if host_part[-1] != '/': + host_part += '/' return parse.urljoin(host_part, version) diff --git a/barbican/common/utils.py b/barbican/common/utils.py index c23dc767e..030d113e1 100644 --- a/barbican/common/utils.py +++ b/barbican/common/utils.py @@ -78,7 +78,10 @@ def get_base_url_from_request(): """ if not CONF.host_href and hasattr(pecan.request, 'url'): p_url = parse.urlsplit(pecan.request.url) - base_url = '%s://%s' % (p_url.scheme, p_url.netloc) + if p_url.path: + base_url = '%s://%s%s' % (p_url.scheme, p_url.netloc, p_url.path) + else: + base_url = '%s://%s' % (p_url.scheme, p_url.netloc) return base_url else: # when host_href is set or flow is not within wsgi request context return CONF.host_href diff --git a/barbican/tests/api/controllers/test_versions.py b/barbican/tests/api/controllers/test_versions.py index 77aad2ee9..3e4955f3a 100644 --- a/barbican/tests/api/controllers/test_versions.py +++ b/barbican/tests/api/controllers/test_versions.py @@ -79,6 +79,34 @@ class WhenTestingVersionsResource(utils.BarbicanAPIBaseTestCase): self.assertNotIn(dummy_root, v_info['links'][0]['href']) self.assertNotIn(host_hdr, v_info['links'][0]['href']) + def test_when_host_href_is_general(self): + host_href = 'http://myapp.server.com/key-manager' + cmn_utils.CONF.set_override('host_href', host_href) + host_hdr = 'http://myproxy.server.com:9311' + utils.mock_pecan_request(self, host=host_hdr) + dummy_root = 'http://mylocalhost:9999' + resp = self.app.get(dummy_root) + + versions_response = resp.json['versions']['values'] + + for v_info in versions_response: + self.assertIn(host_href, v_info['links'][0]['href']) + self.assertNotIn(dummy_root, v_info['links'][0]['href']) + self.assertNotIn(host_hdr, v_info['links'][0]['href']) + + def test_when_host_href_is_not_set_with_general_request_url(self): + cmn_utils.CONF.set_override('host_href', '') + host_hdr = 'http://myproxy.server.com/key-manager' + utils.mock_pecan_request(self, host=host_hdr) + dummy_root = 'http://mylocalhost:9999' + resp = self.app.get(dummy_root) + + versions_response = resp.json['versions']['values'] + + for v_info in versions_response: + self.assertIn(host_hdr, v_info['links'][0]['href']) + self.assertNotIn(dummy_root, v_info['links'][0]['href']) + class WhenTestingV1Resource(utils.BarbicanAPIBaseTestCase):