| 1 |
# Copyright 2016, 2018 Red Hat, Inc. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
from config_tempest.constants import LOG |
| 17 |
from tempest.lib import exceptions |
| 18 |
|
| 19 |
|
| 20 |
class Users(object): |
| 21 |
def __init__(self, projects_client, roles_client, users_client, conf): |
| 22 |
"""Init. |
| 23 |
|
| 24 |
:type projects_client: ProjectsClient object |
| 25 |
:type roles_client: RolesClient object from tempest lib |
| 26 |
:type users_client: UsersClient object from tempest lib |
| 27 |
:type conf: TempestConf object |
| 28 |
""" |
| 29 |
self.projects_client = projects_client |
| 30 |
self.roles_client = roles_client |
| 31 |
self.users_client = users_client |
| 32 |
self._conf = conf |
| 33 |
|
| 34 |
def create_tempest_users(self): |
| 35 |
"""Create users necessary for Tempest if they don't exist already. |
| 36 |
|
| 37 |
""" |
| 38 |
sec = 'identity' |
| 39 |
self.create_user_with_project(self._conf.get(sec, 'username'), |
| 40 |
self._conf.get(sec, 'password'), |
| 41 |
self._conf.get(sec, 'project_name')) |
| 42 |
|
| 43 |
self.create_user_with_project(self._conf.get(sec, 'alt_username'), |
| 44 |
self._conf.get(sec, 'alt_password'), |
| 45 |
self._conf.get(sec, 'alt_project_name')) |
| 46 |
|
| 47 |
username = self._conf.get_defaulted('auth', 'admin_username') |
| 48 |
|
| 49 |
self.give_role_to_user(username, role_name='admin') |
| 50 |
|
| 51 |
def _filter_ids_by_name(self, mode, name, role_name, roles_list): |
| 52 |
"""Filter the id that belongs to the name in the given list. |
| 53 |
|
| 54 |
:param mode: 'roles' or 'users', depending what we're looking for |
| 55 |
:param name: Name we want to filter by |
| 56 |
:param role_name: Name of the role we wanna assign the user |
| 57 |
:param roles_list: List of roles or users to search in. |
| 58 |
:return: List containing an id matching the name in the roles_list. |
| 59 |
:rtype: list |
| 60 |
""" |
| 61 |
match = [n['id'] for n in roles_list[mode] if n['name'] == name] |
| 62 |
if not match and role_name == 'admin': |
| 63 |
# in a few cases there might be the admin role with upper-case |
| 64 |
# first letter, therefore, let's try Admin |
| 65 |
name = 'Admin' |
| 66 |
match = [n['id'] for n in roles_list[mode] if n['name'] == name] |
| 67 |
if match and mode == 'users': |
| 68 |
# if we found a match with Admin and we're in the users mode, |
| 69 |
# change also the admin_username in the tempest.conf |
| 70 |
self._conf.set('auth', 'admin_username', name) |
| 71 |
return match |
| 72 |
|
| 73 |
def give_role_to_user(self, username, role_name, |
| 74 |
role_required=True): |
| 75 |
"""Give the user a role in the project. |
| 76 |
|
| 77 |
If the role is 'admin' and it isn't found, the method will try 'Admin' |
| 78 |
as sometimes the admin role can be named with upper-case 1st letter. |
| 79 |
|
| 80 |
:type username: string |
| 81 |
:type role_name: string |
| 82 |
:type role_required: boolean |
| 83 |
""" |
| 84 |
project_name = self._conf.get('identity', 'project_name') |
| 85 |
proj_id = self.projects_client.get_project_by_name(project_name)['id'] |
| 86 |
users = self.users_client.list_users() |
| 87 |
user_ids = self._filter_ids_by_name('users', username, |
| 88 |
role_name, users) |
| 89 |
if not user_ids: |
| 90 |
raise Exception("user %s not found" % username) |
| 91 |
user_id = user_ids[0] |
| 92 |
|
| 93 |
roles = self.roles_client.list_roles() |
| 94 |
self.check_user_roles(roles) |
| 95 |
role_ids = self._filter_ids_by_name('roles', role_name, |
| 96 |
role_name, roles) |
| 97 |
if not role_ids: |
| 98 |
if role_required: |
| 99 |
raise Exception("required role %s not found" % role_name) |
| 100 |
LOG.debug("%s role not required", role_name) |
| 101 |
return |
| 102 |
role_id = role_ids[0] |
| 103 |
try: |
| 104 |
self.roles_client.create_user_role_on_project(proj_id, user_id, |
| 105 |
role_id) |
| 106 |
LOG.debug("User '%s' was given the '%s' role in project '%s'", |
| 107 |
username, role_name, project_name) |
| 108 |
except exceptions.Conflict: |
| 109 |
LOG.debug("(no change) User '%s' already has the '%s' role in" |
| 110 |
" project '%s'", username, role_name, project_name) |
| 111 |
|
| 112 |
def check_user_roles(self, roles): |
| 113 |
"""Check if roles provided by user (or the default one) exist. |
| 114 |
|
| 115 |
:param roles: value returned by roles_client.list_roles |
| 116 |
:type roles: dict |
| 117 |
:return: List of the existing roles given by user (or by defaults) |
| 118 |
:rtype: list |
| 119 |
""" |
| 120 |
roles_names = [r['name'] for r in roles['roles']] |
| 121 |
user_roles = self._conf.get('auth', 'tempest_roles').split(',') |
| 122 |
available_roles = [] |
| 123 |
for r in user_roles: |
| 124 |
if r in roles_names: |
| 125 |
available_roles.append(r) |
| 126 |
else: |
| 127 |
LOG.debug("Provided %s role is not present in the system.", r) |
| 128 |
|
| 129 |
if len(available_roles) == 0: |
| 130 |
# try 'member' or 'Member', they might present in a system |
| 131 |
if 'member' in roles_names: |
| 132 |
self._conf.set('auth', 'tempest_roles', 'member') |
| 133 |
elif 'Member' in roles_names: |
| 134 |
self._conf.set('auth', 'tempest_roles', 'Member') |
| 135 |
else: |
| 136 |
LOG.debug("Setting auth.tempest_roles to an empty list " |
| 137 |
"because none of the provided roles exists.") |
| 138 |
self._conf.set('auth', 'tempest_roles', "") |
| 139 |
return available_roles |
| 140 |
|
| 141 |
def create_user_with_project(self, username, password, project_name): |
| 142 |
"""Create a user and a project if it doesn't exist. |
| 143 |
|
| 144 |
:type username: string |
| 145 |
:type password: string |
| 146 |
:type project_name: string |
| 147 |
""" |
| 148 |
LOG.info("Creating user '%s' with project '%s' and password '%s'", |
| 149 |
username, project_name, password) |
| 150 |
project_description = "Project for Tempest %s user" % username |
| 151 |
email = "%[email protected]" % username |
| 152 |
# create a project |
| 153 |
try: |
| 154 |
self.projects_client.create_project( |
| 155 |
name=project_name, description=project_description) |
| 156 |
except exceptions.Conflict: |
| 157 |
LOG.info("(no change) Project '%s' already exists", project_name) |
| 158 |
|
| 159 |
proj_id = self.projects_client.get_project_by_name(project_name)['id'] |
| 160 |
|
| 161 |
params = {'name': username, 'password': password, |
| 162 |
'tenantId': proj_id, 'email': email} |
| 163 |
# create a user |
| 164 |
try: |
| 165 |
self.users_client.create_user(**params) |
| 166 |
except exceptions.Conflict: |
| 167 |
LOG.info("User '%s' already exists.", username) |