From db3fb0be9b8df62716cecd7d7328c1f96d581cd6 Mon Sep 17 00:00:00 2001 From: Chris Gilmer Date: Thu, 5 Apr 2012 13:48:12 -0700 Subject: [PATCH] Added the expire time for auth tokens --- libcloud/common/openstack.py | 7 +++++-- test/compute/test_openstack.py | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/libcloud/common/openstack.py b/libcloud/common/openstack.py index 29c93eb..08daff3 100644 --- a/libcloud/common/openstack.py +++ b/libcloud/common/openstack.py @@ -168,6 +168,7 @@ class OpenStackAuthConnection(ConnectionUserAndKey): raise MalformedResponseError('Failed to parse JSON', e) try: self.auth_token = body['auth']['token']['id'] + self.auth_token_expires = body['auth']['token']['expires'] self.urls = body['auth']['serviceCatalog'] except KeyError: e = sys.exc_info()[1] @@ -218,8 +219,8 @@ class OpenStackAuthConnection(ConnectionUserAndKey): try: access = body['access'] - token = access['token'] - self.auth_token = token['id'] + self.auth_token = access['token']['id'] + self.auth_token_expires = access['token']['expires'] self.urls = access['serviceCatalog'] except KeyError: e = sys.exc_info()[1] @@ -356,6 +357,7 @@ class OpenStackBaseConnection(ConnectionUserAndKey): auth_url = None auth_token = None + auth_token_expires = None service_catalog = None service_type = None service_name = None @@ -454,6 +456,7 @@ class OpenStackBaseConnection(ConnectionUserAndKey): osa.authenticate() self.auth_token = osa.auth_token + self.auth_token_expires = osa.auth_token_expires # pull out and parse the service catalog self.service_catalog = OpenStackServiceCatalog(osa.urls, ex_force_auth_version=self._auth_version) diff --git a/test/compute/test_openstack.py b/test/compute/test_openstack.py index 44f731b..0599356 100644 --- a/test/compute/test_openstack.py +++ b/test/compute/test_openstack.py @@ -97,6 +97,14 @@ class OpenStack_1_0_Tests(unittest.TestCase, TestCaseMixin): self.driver.connection._populate_hosts_and_request_paths() clear_pricing_data() + def test_auth_token_is_set(self): + self.driver.connection._populate_hosts_and_request_paths() + self.assertEquals(self.driver.connection.auth_token, "603d2bd9-f45c-4583-b91c-2c8eac0b5654") + + def test_auth_token_expires_is_set(self): + self.driver.connection._populate_hosts_and_request_paths() + self.assertEquals(self.driver.connection.auth_token_expires, "2011-09-18T02:44:17.000-05:00") + def test_auth(self): OpenStackMockHttp.type = 'UNAUTHORIZED' try: @@ -541,6 +549,24 @@ class OpenStack_1_1_Tests(unittest.TestCase, TestCaseMixin): clear_pricing_data() self.node = self.driver.list_nodes()[1] + def test_auth_token_is_set(self): + # change base url and trash the current auth token so we can re-authenticate + self.driver.connection._ex_force_base_url = 'http://ex_force_base_url.com:666/forced_url' + self.driver.connection.auth_token = None + self.driver.connection.auth_token_expires = None + self.driver.connection._populate_hosts_and_request_paths() + + self.assertEquals(self.driver.connection.auth_token, "aaaaaaaaaaaa-bbb-cccccccccccccc") + + def test_auth_token_expires_is_set(self): + # change base url and trash the current auth token so we can re-authenticate + self.driver.connection._ex_force_base_url = 'http://ex_force_base_url.com:666/forced_url' + self.driver.connection.auth_token = None + self.driver.connection.auth_token_expires = None + self.driver.connection._populate_hosts_and_request_paths() + + self.assertEquals(self.driver.connection.auth_token_expires, "2011-11-23T21:00:14.000-06:00") + def test_ex_force_base_url(self): # change base url and trash the current auth token so we can re-authenticate self.driver.connection._ex_force_base_url = 'http://ex_force_base_url.com:666/forced_url' -- 1.7.6.1