Fix LIBCLOUD-965 Signed-off-by: Quentin Pradet <[email protected]>
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/7ef969c0 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/7ef969c0 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/7ef969c0 Branch: refs/heads/trunk Commit: 7ef969c03841dc2afd624ace7f607e50d6f400ab Parents: 22545c7 Author: micafer <[email protected]> Authored: Thu Nov 30 09:56:02 2017 +0100 Committer: Quentin Pradet <[email protected]> Committed: Tue Jan 30 21:37:27 2018 +0400 ---------------------------------------------------------------------- libcloud/common/openstack_identity.py | 13 +++++- libcloud/test/common/test_openstack_identity.py | 47 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/7ef969c0/libcloud/common/openstack_identity.py ---------------------------------------------------------------------- diff --git a/libcloud/common/openstack_identity.py b/libcloud/common/openstack_identity.py index 966c12b..4285933 100644 --- a/libcloud/common/openstack_identity.py +++ b/libcloud/common/openstack_identity.py @@ -1525,12 +1525,23 @@ class OpenStackIdentity_3_0_Connection_OIDC_access_token( """ Get the first project ID accessible with the specified access token """ - path = '/v3/OS-FEDERATION/projects' + # Try new path first (from ver 1.1) + path = '/v3/auth/projects' response = self.request(path, headers={'Content-Type': 'application/json', 'X-Auth-Token': token}, method='GET') + if response.status not in [httplib.UNAUTHORIZED, httplib.OK, + httplib.CREATED]: + # In case of error try old one + path = '/v3/OS-FEDERATION/projects' + response = self.request(path, + headers={'Content-Type': + 'application/json', + 'X-Auth-Token': token}, + method='GET') + if response.status == httplib.UNAUTHORIZED: # Invalid credentials raise InvalidCredsError() http://git-wip-us.apache.org/repos/asf/libcloud/blob/7ef969c0/libcloud/test/common/test_openstack_identity.py ---------------------------------------------------------------------- diff --git a/libcloud/test/common/test_openstack_identity.py b/libcloud/test/common/test_openstack_identity.py index 3812f12..79a129e 100644 --- a/libcloud/test/common/test_openstack_identity.py +++ b/libcloud/test/common/test_openstack_identity.py @@ -424,6 +424,30 @@ class OpenStackIdentity_3_0_ConnectionTests(unittest.TestCase): self.assertTrue(result) +class OpenStackIdentity_3_0_Connection_OIDC_access_token_federation_projectsTests( + unittest.TestCase): + def setUp(self): + mock_cls = OpenStackIdentity_3_0_federation_projects_MockHttp + mock_cls.type = None + OpenStackIdentity_3_0_Connection_OIDC_access_token.conn_class = mock_cls + + self.auth_instance = OpenStackIdentity_3_0_Connection_OIDC_access_token(auth_url='http://none', + user_id='idp', + key='token', + tenant_name='oidc', + domain_name='test_domain') + self.auth_instance.auth_token = 'mock' + + def test_authenticate(self): + auth = OpenStackIdentity_3_0_Connection_OIDC_access_token(auth_url='http://none', + user_id='idp', + key='token', + token_scope='project', + tenant_name="oidc", + domain_name='test_domain') + auth.authenticate() + + class OpenStackIdentity_3_0_Connection_OIDC_access_tokenTests( unittest.TestCase): def setUp(self): @@ -729,6 +753,29 @@ class OpenStackIdentity_3_0_MockHttp(MockHttp): return (httplib.OK, body, self.json_content_headers, httplib.responses[httplib.OK]) raise NotImplementedError() + def _v3_auth_projects(self, method, url, body, headers): + if method == 'GET': + # get user projects + body = json.dumps({"projects": [{"id": "project_id"}]}) + return (httplib.OK, body, self.json_content_headers, httplib.responses[httplib.OK]) + raise NotImplementedError() + + +class OpenStackIdentity_3_0_federation_projects_MockHttp(OpenStackIdentity_3_0_MockHttp): + fixtures = ComputeFileFixtures('openstack_identity/v3') + json_content_headers = {'content-type': 'application/json; charset=UTF-8'} + + def _v3_OS_FEDERATION_projects(self, method, url, body, headers): + if method == 'GET': + # get user projects + body = json.dumps({"projects": [{"id": "project_id"}]}) + return (httplib.OK, body, self.json_content_headers, httplib.responses[httplib.OK]) + raise NotImplementedError() + + def _v3_auth_projects(self, method, url, body, headers): + return (httplib.INTERNAL_SERVER_ERROR, body, self.json_content_headers, + httplib.responses[httplib.INTERNAL_SERVER_ERROR]) + class OpenStackIdentity_2_0_Connection_VOMSMockHttp(MockHttp): fixtures = ComputeFileFixtures('openstack_identity/v2')
