Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
| 13 | import abc |
| 14 | |
| 15 | from oslo_log import log as logging |
| 16 | import six |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 17 | |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 18 | from tempest.lib import auth |
| 19 | from tempest.lib import exceptions as lib_exc |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 20 | from tempest.services.identity.v2.json import identity_client as v2_identity |
| 21 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 22 | LOG = logging.getLogger(__name__) |
| 23 | |
| 24 | |
| 25 | @six.add_metaclass(abc.ABCMeta) |
| 26 | class CredsClient(object): |
Ken'ichi Ohmichi | cb67d2d | 2015-11-19 08:23:22 +0000 | [diff] [blame] | 27 | """This class is a wrapper around the identity clients |
| 28 | |
| 29 | to provide a single interface for managing credentials in both v2 and v3 |
| 30 | cases. It's not bound to created credentials, only to a specific set of |
| 31 | admin credentials used for generating credentials. |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 32 | """ |
| 33 | |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 34 | def __init__(self, identity_client, projects_client, users_client, |
| 35 | roles_client=None): |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 36 | # The client implies version and credentials |
| 37 | self.identity_client = identity_client |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 38 | self.users_client = users_client |
Yaroslav Lobankov | 47a93ab | 2016-02-07 16:32:49 -0600 | [diff] [blame] | 39 | self.projects_client = projects_client |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 40 | # this is temporary until the v3 clients are |
| 41 | # separated, then using *only* each client will become mandatory |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 42 | self.roles_client = roles_client or identity_client |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 43 | |
| 44 | def create_user(self, username, password, project, email): |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 45 | user = self.users_client.create_user( |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 46 | username, password, project['id'], email) |
| 47 | if 'user' in user: |
| 48 | user = user['user'] |
| 49 | return user |
| 50 | |
Yaroslav Lobankov | 47a93ab | 2016-02-07 16:32:49 -0600 | [diff] [blame] | 51 | def delete_user(self, user_id): |
| 52 | self.users_client.delete_user(user_id) |
| 53 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 54 | @abc.abstractmethod |
| 55 | def create_project(self, name, description): |
| 56 | pass |
| 57 | |
| 58 | def _check_role_exists(self, role_name): |
| 59 | try: |
| 60 | roles = self._list_roles() |
| 61 | role = next(r for r in roles if r['name'] == role_name) |
| 62 | except StopIteration: |
| 63 | return None |
| 64 | return role |
| 65 | |
| 66 | def create_user_role(self, role_name): |
| 67 | if not self._check_role_exists(role_name): |
piyush110786 | afaaf26 | 2015-12-11 18:54:05 +0530 | [diff] [blame] | 68 | self.roles_client.create_role(name=role_name) |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 69 | |
| 70 | def assign_user_role(self, user, project, role_name): |
| 71 | role = self._check_role_exists(role_name) |
| 72 | if not role: |
| 73 | msg = 'No "%s" role found' % role_name |
| 74 | raise lib_exc.NotFound(msg) |
| 75 | try: |
Ghanshyam | c044867 | 2016-02-17 12:53:25 +0900 | [diff] [blame] | 76 | self._assign_user_role(project, user, role) |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 77 | except lib_exc.Conflict: |
| 78 | LOG.debug("Role %s already assigned on project %s for user %s" % ( |
| 79 | role['id'], project['id'], user['id'])) |
| 80 | |
| 81 | @abc.abstractmethod |
| 82 | def get_credentials(self, user, project, password): |
Andrea Frittoli (andreaf) | 278463c | 2015-10-08 15:04:09 +0100 | [diff] [blame] | 83 | """Produces a Credentials object from the details provided |
| 84 | |
| 85 | :param user: a user dict |
| 86 | :param project: a project dict |
| 87 | :param password: the password as a string |
| 88 | :return: a Credentials object with all the available credential details |
| 89 | """ |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 90 | pass |
| 91 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 92 | def _list_roles(self): |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 93 | roles = self.roles_client.list_roles()['roles'] |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 94 | return roles |
| 95 | |
| 96 | |
| 97 | class V2CredsClient(CredsClient): |
| 98 | |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 99 | def __init__(self, identity_client, projects_client, users_client, |
| 100 | roles_client): |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 101 | super(V2CredsClient, self).__init__(identity_client, |
| 102 | projects_client, |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 103 | users_client, |
| 104 | roles_client) |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 105 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 106 | def create_project(self, name, description): |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 107 | tenant = self.projects_client.create_tenant( |
Anusha Ramineni | 0cfb461 | 2015-08-24 08:49:10 +0530 | [diff] [blame] | 108 | name=name, description=description)['tenant'] |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 109 | return tenant |
| 110 | |
Yaroslav Lobankov | 47a93ab | 2016-02-07 16:32:49 -0600 | [diff] [blame] | 111 | def delete_project(self, project_id): |
| 112 | self.projects_client.delete_tenant(project_id) |
| 113 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 114 | def get_credentials(self, user, project, password): |
Andrea Frittoli (andreaf) | 278463c | 2015-10-08 15:04:09 +0100 | [diff] [blame] | 115 | # User and project already include both ID and name here, |
| 116 | # so there's no need to use the fill_in mode |
| 117 | return auth.get_credentials( |
| 118 | auth_url=None, |
| 119 | fill_in=False, |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 120 | identity_version='v2', |
| 121 | username=user['name'], user_id=user['id'], |
| 122 | tenant_name=project['name'], tenant_id=project['id'], |
| 123 | password=password) |
| 124 | |
Ghanshyam | c044867 | 2016-02-17 12:53:25 +0900 | [diff] [blame] | 125 | def _assign_user_role(self, project, user, role): |
| 126 | self.roles_client.assign_user_role(project['id'], user['id'], |
| 127 | role['id']) |
| 128 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 129 | |
| 130 | class V3CredsClient(CredsClient): |
| 131 | |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 132 | def __init__(self, identity_client, projects_client, users_client, |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 133 | domains_client, domain_name): |
| 134 | super(V3CredsClient, self).__init__(identity_client, projects_client, |
| 135 | users_client) |
| 136 | self.domains_client = domains_client |
| 137 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 138 | try: |
| 139 | # Domain names must be unique, in any case a list is returned, |
| 140 | # selecting the first (and only) element |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 141 | self.creds_domain = self.domains_client.list_domains( |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 142 | params={'name': domain_name})['domains'][0] |
| 143 | except lib_exc.NotFound: |
| 144 | # TODO(andrea) we could probably create the domain on the fly |
Andrea Frittoli (andreaf) | 278463c | 2015-10-08 15:04:09 +0100 | [diff] [blame] | 145 | msg = "Requested domain %s could not be found" % domain_name |
| 146 | raise lib_exc.InvalidCredentials(msg) |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 147 | |
| 148 | def create_project(self, name, description): |
Yaroslav Lobankov | 47a93ab | 2016-02-07 16:32:49 -0600 | [diff] [blame] | 149 | project = self.projects_client.create_project( |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 150 | name=name, description=description, |
| 151 | domain_id=self.creds_domain['id'])['project'] |
| 152 | return project |
| 153 | |
Yaroslav Lobankov | 47a93ab | 2016-02-07 16:32:49 -0600 | [diff] [blame] | 154 | def delete_project(self, project_id): |
| 155 | self.projects_client.delete_project(project_id) |
| 156 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 157 | def get_credentials(self, user, project, password): |
Andrea Frittoli (andreaf) | 278463c | 2015-10-08 15:04:09 +0100 | [diff] [blame] | 158 | # User, project and domain already include both ID and name here, |
| 159 | # so there's no need to use the fill_in mode. |
| 160 | return auth.get_credentials( |
| 161 | auth_url=None, |
| 162 | fill_in=False, |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 163 | identity_version='v3', |
| 164 | username=user['name'], user_id=user['id'], |
| 165 | project_name=project['name'], project_id=project['id'], |
| 166 | password=password, |
Andrea Frittoli (andreaf) | 278463c | 2015-10-08 15:04:09 +0100 | [diff] [blame] | 167 | project_domain_id=self.creds_domain['id'], |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 168 | project_domain_name=self.creds_domain['name']) |
| 169 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 170 | def _list_roles(self): |
| 171 | roles = self.identity_client.list_roles()['roles'] |
| 172 | return roles |
| 173 | |
Ghanshyam | c044867 | 2016-02-17 12:53:25 +0900 | [diff] [blame] | 174 | def _assign_user_role(self, project, user, role): |
| 175 | self.roles_client.assign_user_role_on_project(project['id'], |
| 176 | user['id'], |
| 177 | role['id']) |
| 178 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 179 | |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 180 | def get_creds_client(identity_client, |
Yaroslav Lobankov | 47a93ab | 2016-02-07 16:32:49 -0600 | [diff] [blame] | 181 | projects_client, |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 182 | users_client, |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 183 | roles_client=None, |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 184 | domains_client=None, |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 185 | project_domain_name=None): |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 186 | if isinstance(identity_client, v2_identity.IdentityClient): |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 187 | return V2CredsClient(identity_client, projects_client, users_client, |
| 188 | roles_client) |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 189 | else: |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 190 | return V3CredsClient(identity_client, projects_client, users_client, |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 191 | domains_client, project_domain_name) |