Allow host URL for versions to be configurable

The versions resource constructs the links by using application_url,
but it's possible that the API endpoint is behind a load balancer
or SSL terminator. This means that the application_url might be
incorrect. This fix provides a config option (similar to other
services) which lets us  override the host URL when constructing
links for the versions API.

Co-Authored-By: Nikhil Manchanda <SlickNik@gmail.com>
Change-Id: I23f06c6c2d52ba46c74e0d097c4963d2de731d30
Closes-bug: 1384379
This commit is contained in:
Zhao Chao 2018-02-07 11:07:02 +08:00
parent e8df4126f5
commit 1667ad5e80
3 changed files with 52 additions and 2 deletions

View File

@ -44,6 +44,15 @@ path_opts = [
help='Directory where the Trove python module is installed.'),
]
versions_opts = [
cfg.StrOpt('public_endpoint', default=None,
help='Public URL to use for versions endpoint. The default '
'is None, which will use the request\'s host_url '
'attribute to populate the URL base. If Trove is '
'operating behind a proxy, you will want to change '
'this to represent the proxy\'s URL.')
]
common_opts = [
cfg.IPOpt('bind_host', default='0.0.0.0',
help='IP address the API server will listen on.'),
@ -1526,6 +1535,7 @@ rpcapi_cap_opts = [
CONF = cfg.CONF
CONF.register_opts(path_opts)
CONF.register_opts(versions_opts)
CONF.register_opts(common_opts)
CONF.register_opts(database_opts, 'database')

View File

@ -14,7 +14,9 @@
# under the License.
from mock import Mock
import webob
from trove.common import cfg
from trove.tests.unittests import trove_testtools
from trove.versions import BaseVersion
from trove.versions import Version
@ -41,6 +43,10 @@ class VersionsControllerTest(trove_testtools.TestCase):
self.assertIsNotNone(self.controller,
"VersionsController instance was None")
def tearDown(self):
super(VersionsControllerTest, self).tearDown()
cfg.CONF.clear_override('public_endpoint')
def test_index_json(self):
request = Mock()
result = self.controller.index(request)
@ -66,6 +72,23 @@ class VersionsControllerTest(trove_testtools.TestCase):
self.assertEqual('2012-08-01T00:00:00Z', json_data['updated'],
'Version updated value is incorrect')
def test_index_json_with_public_endpoint(self):
cfg.CONF.set_override('public_endpoint', "https://example.com:8779")
req = webob.Request.blank('/')
resp = self.controller.index(req)
result = resp.data('application/json')['versions']
expected = [
{
'status': 'CURRENT',
'updated': '2012-08-01T00:00:00Z',
'id': 'v1.0',
'links': [{
'href': 'https://example.com:8779/v1.0/',
'rel': 'self'}]
}
]
self.assertEqual(expected, result)
def test_show_json(self):
request = Mock()
request.url_version = '1.0'
@ -84,6 +107,22 @@ class VersionsControllerTest(trove_testtools.TestCase):
"Version updated was not '2012-08-01T00:00:00Z'")
self.assertEqual('v1.0', version['id'], "Version id was not 'v1.0'")
def test_show_json_with_public_endpoint(self):
cfg.CONF.set_override('public_endpoint', "https://example.com:8779")
req = webob.Request.blank('/')
req.url_version = '1.0'
resp = self.controller.show(req)
result = resp.data('application/json')['version']
expected = {
'status': 'CURRENT',
'updated': '2012-08-01T00:00:00Z',
'id': 'v1.0',
'links': [{
'href': 'https://example.com:8779/',
'rel': 'self'}]
}
self.assertEqual(expected, result)
class BaseVersionTestCase(trove_testtools.TestCase):

View File

@ -16,9 +16,10 @@
import os
import routes
from trove.common import cfg
from trove.common import wsgi
CONF = cfg.CONF
VERSIONS = {
"1.0": {
"id": "v1.0",
@ -56,7 +57,7 @@ class BaseVersion(object):
def __init__(self, id, status, base_url, updated):
self.id = id
self.status = status
self.base_url = base_url
self.base_url = CONF.public_endpoint or base_url
self.updated = updated
def data(self):