New exception when auth_url is not specified

Certain scenarios into the neutron client will not specify the
auth_url.  This is typically when a token is specified.  However, when
the token is expired the neutron client will attempt to refresh the
token.  Users of this may not have passed in all of the required
information for this reauthentication to properly occur.

This code fixes an error that occurs in this flow where the auth_url
(which is None) is appended to another string.  This results in a core
Python error.

The update will provide a more targetted error message specifying to
the user that the auth_url needs to be specified.  An associated unit
test is also included to validate this behavior.

Change-Id: I577ce0c009a9a281acdc238d290a22c5e561ff82
Closes-Bug: #1241275
This commit is contained in:
Drew Thorstensen 2013-10-23 16:41:45 -05:00
parent 43f7fa4ab2
commit e49819caf9
3 changed files with 37 additions and 0 deletions

View File

@ -228,6 +228,9 @@ class HTTPClient(httplib2.Http):
'password': self.password, },
'tenantName': self.tenant_name, }, }
if self.auth_url is None:
raise exceptions.NoAuthURLProvided()
token_url = self.auth_url + "/tokens"
# Make sure we follow redirects when trying to reach Keystone
@ -252,6 +255,9 @@ class HTTPClient(httplib2.Http):
self._extract_service_catalog(resp_body)
def _get_endpoint_url(self):
if self.auth_url is None:
raise exceptions.NoAuthURLProvided()
url = self.auth_url + '/tokens/%s/endpoints' % self.auth_token
try:
resp, body = self._cs_request(url, "GET")

View File

@ -151,6 +151,10 @@ class BadInputError(Exception):
pass
class NoAuthURLProvided(BadInputError):
message = _("auth_url was not provided to the Neutron client")
class Error(Exception):
def __init__(self, message=None):
super(Error, self).__init__(message)

View File

@ -148,6 +148,33 @@ class CLITestAuthKeystone(testtools.TestCase):
self.mox.ReplayAll()
self.client.do_request('/resource', 'GET')
def test_refresh_token_no_auth_url(self):
self.mox.StubOutWithMock(self.client, "request")
self.client.auth_url = None
self.client.auth_token = TOKEN
self.client.endpoint_url = ENDPOINT_URL
res401 = self.mox.CreateMock(httplib2.Response)
res401.status = 401
# If a token is expired, neutron server returns 401
self.client.request(
mox.StrContains(ENDPOINT_URL + '/resource'), 'GET',
headers=mox.ContainsKeyValue('X-Auth-Token', TOKEN)
).AndReturn((res401, ''))
self.mox.ReplayAll()
self.assertRaises(exceptions.NoAuthURLProvided,
self.client.do_request,
'/resource',
'GET')
def test_get_endpoint_url_with_invalid_auth_url(self):
# Handle the case when auth_url is not provided
self.client.auth_url = None
self.assertRaises(exceptions.NoAuthURLProvided,
self.client._get_endpoint_url)
def test_get_endpoint_url(self):
self.mox.StubOutWithMock(self.client, "request")