Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 1 | # Copyright (c) 2014 Hewlett-Packard Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import hashlib |
| 16 | import os |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 17 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 18 | from oslo_concurrency import lockutils |
| 19 | from oslo_log import log as logging |
Matthew Treinish | 1c517a2 | 2015-04-23 11:39:44 -0400 | [diff] [blame] | 20 | import six |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 21 | import yaml |
| 22 | |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 23 | from tempest import clients |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 24 | from tempest.common import cred_provider |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 25 | from tempest.common import fixed_network |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 26 | from tempest import exceptions |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 27 | from tempest.lib import auth |
| 28 | from tempest.lib import exceptions as lib_exc |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 29 | |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 30 | LOG = logging.getLogger(__name__) |
| 31 | |
| 32 | |
| 33 | def read_accounts_yaml(path): |
Matthew Treinish | d89db1b | 2015-12-16 17:29:14 -0500 | [diff] [blame] | 34 | try: |
| 35 | with open(path, 'r') as yaml_file: |
| 36 | accounts = yaml.load(yaml_file) |
| 37 | except IOError: |
| 38 | raise exceptions.InvalidConfiguration( |
| 39 | 'The path for the test accounts file: %s ' |
| 40 | 'could not be found' % path) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 41 | return accounts |
| 42 | |
| 43 | |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 44 | class PreProvisionedCredentialProvider(cred_provider.CredentialProvider): |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 45 | |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 46 | def __init__(self, identity_version, test_accounts_file, |
| 47 | accounts_lock_dir, name=None, credentials_domain=None, |
| 48 | admin_role=None, object_storage_operator_role=None, |
| 49 | object_storage_reseller_admin_role=None): |
| 50 | """Credentials provider using pre-provisioned accounts |
| 51 | |
| 52 | This credentials provider loads the details of pre-provisioned |
| 53 | accounts from a YAML file, in the format specified by |
| 54 | `etc/accounts.yaml.sample`. It locks accounts while in use, using the |
| 55 | external locking mechanism, allowing for multiple python processes |
| 56 | to share a single account file, and thus running tests in parallel. |
| 57 | |
| 58 | The accounts_lock_dir must be generated using `lockutils.get_lock_path` |
| 59 | from the oslo.concurrency library. For instance: |
| 60 | |
| 61 | accounts_lock_dir = os.path.join(lockutils.get_lock_path(CONF), |
| 62 | 'test_accounts') |
| 63 | |
| 64 | Role names for object storage are optional as long as the |
| 65 | `operator` and `reseller_admin` credential types are not used in the |
| 66 | accounts file. |
| 67 | |
| 68 | :param identity_version: identity version of the credentials |
| 69 | :param admin_role: name of the admin role |
| 70 | :param test_accounts_file: path to the accounts YAML file |
| 71 | :param accounts_lock_dir: the directory for external locking |
| 72 | :param name: name of the hash file (optional) |
| 73 | :param credentials_domain: name of the domain credentials belong to |
| 74 | (if no domain is configured) |
| 75 | :param object_storage_operator_role: name of the role |
| 76 | :param object_storage_reseller_admin_role: name of the role |
| 77 | """ |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 78 | super(PreProvisionedCredentialProvider, self).__init__( |
Andrea Frittoli (andreaf) | 1eb0496 | 2015-10-09 14:48:06 +0100 | [diff] [blame] | 79 | identity_version=identity_version, name=name, |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 80 | admin_role=admin_role, credentials_domain=credentials_domain) |
| 81 | self.test_accounts_file = test_accounts_file |
Matthew Treinish | d89db1b | 2015-12-16 17:29:14 -0500 | [diff] [blame] | 82 | if test_accounts_file: |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 83 | accounts = read_accounts_yaml(self.test_accounts_file) |
Matthew Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 84 | self.use_default_creds = False |
| 85 | else: |
| 86 | accounts = {} |
| 87 | self.use_default_creds = True |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 88 | self.hash_dict = self.get_hash_dict( |
| 89 | accounts, admin_role, object_storage_operator_role, |
| 90 | object_storage_reseller_admin_role) |
| 91 | self.accounts_dir = accounts_lock_dir |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 92 | self._creds = {} |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 93 | |
| 94 | @classmethod |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 95 | def _append_role(cls, role, account_hash, hash_dict): |
| 96 | if role in hash_dict['roles']: |
| 97 | hash_dict['roles'][role].append(account_hash) |
| 98 | else: |
| 99 | hash_dict['roles'][role] = [account_hash] |
| 100 | return hash_dict |
| 101 | |
| 102 | @classmethod |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 103 | def get_hash_dict(cls, accounts, admin_role, |
| 104 | object_storage_operator_role=None, |
| 105 | object_storage_reseller_admin_role=None): |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 106 | hash_dict = {'roles': {}, 'creds': {}, 'networks': {}} |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 107 | # Loop over the accounts read from the yaml file |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 108 | for account in accounts: |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 109 | roles = [] |
| 110 | types = [] |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 111 | resources = [] |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 112 | if 'roles' in account: |
| 113 | roles = account.pop('roles') |
| 114 | if 'types' in account: |
| 115 | types = account.pop('types') |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 116 | if 'resources' in account: |
| 117 | resources = account.pop('resources') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 118 | temp_hash = hashlib.md5() |
Matthew Treinish | 1c517a2 | 2015-04-23 11:39:44 -0400 | [diff] [blame] | 119 | temp_hash.update(six.text_type(account).encode('utf-8')) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 120 | temp_hash_key = temp_hash.hexdigest() |
| 121 | hash_dict['creds'][temp_hash_key] = account |
| 122 | for role in roles: |
| 123 | hash_dict = cls._append_role(role, temp_hash_key, |
| 124 | hash_dict) |
| 125 | # If types are set for the account append the matching role |
| 126 | # subdict with the hash |
| 127 | for type in types: |
| 128 | if type == 'admin': |
Andrea Frittoli (andreaf) | 29491a7 | 2015-10-13 11:24:17 +0100 | [diff] [blame] | 129 | hash_dict = cls._append_role(admin_role, temp_hash_key, |
| 130 | hash_dict) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 131 | elif type == 'operator': |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 132 | if object_storage_operator_role: |
| 133 | hash_dict = cls._append_role( |
| 134 | object_storage_operator_role, temp_hash_key, |
| 135 | hash_dict) |
| 136 | else: |
| 137 | msg = ("Type 'operator' configured, but no " |
| 138 | "object_storage_operator_role specified") |
| 139 | raise lib_exc.InvalidCredentials(msg) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 140 | elif type == 'reseller_admin': |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 141 | if object_storage_reseller_admin_role: |
| 142 | hash_dict = cls._append_role( |
| 143 | object_storage_reseller_admin_role, |
| 144 | temp_hash_key, |
| 145 | hash_dict) |
| 146 | else: |
| 147 | msg = ("Type 'reseller_admin' configured, but no " |
| 148 | "object_storage_reseller_admin_role specified") |
| 149 | raise lib_exc.InvalidCredentials(msg) |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 150 | # Populate the network subdict |
| 151 | for resource in resources: |
| 152 | if resource == 'network': |
| 153 | hash_dict['networks'][temp_hash_key] = resources[resource] |
| 154 | else: |
Zhao Lei | 647cc18 | 2015-09-24 17:47:02 +0800 | [diff] [blame] | 155 | LOG.warning('Unknown resource type %s, ignoring this field' |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 156 | % resource) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 157 | return hash_dict |
| 158 | |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 159 | def is_multi_user(self): |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 160 | # Default credentials is not a valid option with locking Account |
| 161 | if self.use_default_creds: |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 162 | raise lib_exc.InvalidCredentials( |
| 163 | "Account file %s doesn't exist" % self.test_accounts_file) |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 164 | else: |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 165 | return len(self.hash_dict['creds']) > 1 |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 166 | |
Yair Fried | 76488d7 | 2014-10-21 10:13:19 +0300 | [diff] [blame] | 167 | def is_multi_tenant(self): |
| 168 | return self.is_multi_user() |
| 169 | |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 170 | def _create_hash_file(self, hash_string): |
| 171 | path = os.path.join(os.path.join(self.accounts_dir, hash_string)) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 172 | if not os.path.isfile(path): |
Matthew Treinish | 4041b26 | 2015-02-27 11:18:54 -0500 | [diff] [blame] | 173 | with open(path, 'w') as fd: |
| 174 | fd.write(self.name) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 175 | return True |
| 176 | return False |
| 177 | |
| 178 | @lockutils.synchronized('test_accounts_io', external=True) |
| 179 | def _get_free_hash(self, hashes): |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 180 | # Cast as a list because in some edge cases a set will be passed in |
| 181 | hashes = list(hashes) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 182 | if not os.path.isdir(self.accounts_dir): |
| 183 | os.mkdir(self.accounts_dir) |
| 184 | # Create File from first hash (since none are in use) |
| 185 | self._create_hash_file(hashes[0]) |
| 186 | return hashes[0] |
Matthew Treinish | 4041b26 | 2015-02-27 11:18:54 -0500 | [diff] [blame] | 187 | names = [] |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 188 | for _hash in hashes: |
| 189 | res = self._create_hash_file(_hash) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 190 | if res: |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 191 | return _hash |
Matthew Treinish | 4041b26 | 2015-02-27 11:18:54 -0500 | [diff] [blame] | 192 | else: |
| 193 | path = os.path.join(os.path.join(self.accounts_dir, |
| 194 | _hash)) |
| 195 | with open(path, 'r') as fd: |
| 196 | names.append(fd.read()) |
| 197 | msg = ('Insufficient number of users provided. %s have allocated all ' |
| 198 | 'the credentials for this allocation request' % ','.join(names)) |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 199 | raise lib_exc.InvalidCredentials(msg) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 200 | |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 201 | def _get_match_hash_list(self, roles=None): |
| 202 | hashes = [] |
| 203 | if roles: |
| 204 | # Loop over all the creds for each role in the subdict and generate |
| 205 | # a list of cred lists for each role |
| 206 | for role in roles: |
| 207 | temp_hashes = self.hash_dict['roles'].get(role, None) |
| 208 | if not temp_hashes: |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 209 | raise lib_exc.InvalidCredentials( |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 210 | "No credentials with role: %s specified in the " |
| 211 | "accounts ""file" % role) |
| 212 | hashes.append(temp_hashes) |
| 213 | # Take the list of lists and do a boolean and between each list to |
| 214 | # find the creds which fall under all the specified roles |
| 215 | temp_list = set(hashes[0]) |
| 216 | for hash_list in hashes[1:]: |
| 217 | temp_list = temp_list & set(hash_list) |
| 218 | hashes = temp_list |
| 219 | else: |
| 220 | hashes = self.hash_dict['creds'].keys() |
| 221 | # NOTE(mtreinish): admin is a special case because of the increased |
zhufl | 0892cb2 | 2016-05-06 14:46:00 +0800 | [diff] [blame] | 222 | # privilege set which could potentially cause issues on tests where |
| 223 | # that is not expected. So unless the admin role isn't specified do |
| 224 | # not allocate admin. |
Andrea Frittoli (andreaf) | 29491a7 | 2015-10-13 11:24:17 +0100 | [diff] [blame] | 225 | admin_hashes = self.hash_dict['roles'].get(self.admin_role, |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 226 | None) |
Andrea Frittoli (andreaf) | 29491a7 | 2015-10-13 11:24:17 +0100 | [diff] [blame] | 227 | if ((not roles or self.admin_role not in roles) and |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 228 | admin_hashes): |
| 229 | useable_hashes = [x for x in hashes if x not in admin_hashes] |
| 230 | else: |
| 231 | useable_hashes = hashes |
| 232 | return useable_hashes |
| 233 | |
Matthew Treinish | fd683e8 | 2015-04-13 20:30:06 -0400 | [diff] [blame] | 234 | def _sanitize_creds(self, creds): |
| 235 | temp_creds = creds.copy() |
| 236 | temp_creds.pop('password') |
| 237 | return temp_creds |
| 238 | |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 239 | def _get_creds(self, roles=None): |
Matthew Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 240 | if self.use_default_creds: |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 241 | raise lib_exc.InvalidCredentials( |
| 242 | "Account file %s doesn't exist" % self.test_accounts_file) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 243 | useable_hashes = self._get_match_hash_list(roles) |
| 244 | free_hash = self._get_free_hash(useable_hashes) |
Matthew Treinish | fd683e8 | 2015-04-13 20:30:06 -0400 | [diff] [blame] | 245 | clean_creds = self._sanitize_creds( |
| 246 | self.hash_dict['creds'][free_hash]) |
| 247 | LOG.info('%s allocated creds:\n%s' % (self.name, clean_creds)) |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 248 | return self._wrap_creds_with_network(free_hash) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 249 | |
| 250 | @lockutils.synchronized('test_accounts_io', external=True) |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 251 | def remove_hash(self, hash_string): |
| 252 | hash_path = os.path.join(self.accounts_dir, hash_string) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 253 | if not os.path.isfile(hash_path): |
| 254 | LOG.warning('Expected an account lock file %s to remove, but ' |
Matthew Treinish | 53a2b4b | 2015-02-24 23:32:07 -0500 | [diff] [blame] | 255 | 'one did not exist' % hash_path) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 256 | else: |
| 257 | os.remove(hash_path) |
| 258 | if not os.listdir(self.accounts_dir): |
| 259 | os.rmdir(self.accounts_dir) |
| 260 | |
| 261 | def get_hash(self, creds): |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 262 | for _hash in self.hash_dict['creds']: |
| 263 | # Comparing on the attributes that are expected in the YAML |
Andrea Frittoli (andreaf) | f39f9f3 | 2015-04-13 20:55:31 +0100 | [diff] [blame] | 264 | init_attributes = creds.get_init_attributes() |
| 265 | hash_attributes = self.hash_dict['creds'][_hash].copy() |
| 266 | if ('user_domain_name' in init_attributes and 'user_domain_name' |
| 267 | not in hash_attributes): |
| 268 | # Allow for the case of domain_name populated from config |
Andrea Frittoli (andreaf) | 1eb0496 | 2015-10-09 14:48:06 +0100 | [diff] [blame] | 269 | domain_name = self.credentials_domain |
Andrea Frittoli (andreaf) | f39f9f3 | 2015-04-13 20:55:31 +0100 | [diff] [blame] | 270 | hash_attributes['user_domain_name'] = domain_name |
| 271 | if all([getattr(creds, k) == hash_attributes[k] for |
| 272 | k in init_attributes]): |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 273 | return _hash |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 274 | raise AttributeError('Invalid credentials %s' % creds) |
| 275 | |
| 276 | def remove_credentials(self, creds): |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 277 | _hash = self.get_hash(creds) |
Matthew Treinish | fd683e8 | 2015-04-13 20:30:06 -0400 | [diff] [blame] | 278 | clean_creds = self._sanitize_creds(self.hash_dict['creds'][_hash]) |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 279 | self.remove_hash(_hash) |
Matthew Treinish | fd683e8 | 2015-04-13 20:30:06 -0400 | [diff] [blame] | 280 | LOG.info("%s returned allocated creds:\n%s" % (self.name, clean_creds)) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 281 | |
| 282 | def get_primary_creds(self): |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 283 | if self._creds.get('primary'): |
| 284 | return self._creds.get('primary') |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 285 | net_creds = self._get_creds() |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 286 | self._creds['primary'] = net_creds |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 287 | return net_creds |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 288 | |
| 289 | def get_alt_creds(self): |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 290 | if self._creds.get('alt'): |
| 291 | return self._creds.get('alt') |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 292 | net_creds = self._get_creds() |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 293 | self._creds['alt'] = net_creds |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 294 | return net_creds |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 295 | |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 296 | def get_creds_by_roles(self, roles, force_new=False): |
| 297 | roles = list(set(roles)) |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 298 | exist_creds = self._creds.get(six.text_type(roles).encode( |
Matthew Treinish | 1c517a2 | 2015-04-23 11:39:44 -0400 | [diff] [blame] | 299 | 'utf-8'), None) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 300 | # The force kwarg is used to allocate an additional set of creds with |
| 301 | # the same role list. The index used for the previously allocation |
Ken'ichi Ohmichi | edff886 | 2015-10-13 01:10:53 +0000 | [diff] [blame] | 302 | # in the _creds dict will be moved. |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 303 | if exist_creds and not force_new: |
| 304 | return exist_creds |
| 305 | elif exist_creds and force_new: |
Matthew Treinish | 1c517a2 | 2015-04-23 11:39:44 -0400 | [diff] [blame] | 306 | new_index = six.text_type(roles).encode('utf-8') + '-' + \ |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 307 | six.text_type(len(self._creds)).encode('utf-8') |
| 308 | self._creds[new_index] = exist_creds |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 309 | net_creds = self._get_creds(roles=roles) |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 310 | self._creds[six.text_type(roles).encode('utf-8')] = net_creds |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 311 | return net_creds |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 312 | |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 313 | def clear_creds(self): |
| 314 | for creds in self._creds.values(): |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 315 | self.remove_credentials(creds) |
| 316 | |
| 317 | def get_admin_creds(self): |
Andrea Frittoli (andreaf) | 29491a7 | 2015-10-13 11:24:17 +0100 | [diff] [blame] | 318 | return self.get_creds_by_roles([self.admin_role]) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 319 | |
Matthew Treinish | 4a59693 | 2015-03-06 20:37:01 -0500 | [diff] [blame] | 320 | def is_role_available(self, role): |
| 321 | if self.use_default_creds: |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 322 | return False |
Matthew Treinish | 4a59693 | 2015-03-06 20:37:01 -0500 | [diff] [blame] | 323 | else: |
| 324 | if self.hash_dict['roles'].get(role): |
| 325 | return True |
| 326 | return False |
| 327 | |
| 328 | def admin_available(self): |
Andrea Frittoli (andreaf) | 29491a7 | 2015-10-13 11:24:17 +0100 | [diff] [blame] | 329 | return self.is_role_available(self.admin_role) |
Andrea Frittoli | b1c23fc | 2014-09-03 13:40:08 +0100 | [diff] [blame] | 330 | |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 331 | def _wrap_creds_with_network(self, hash): |
| 332 | creds_dict = self.hash_dict['creds'][hash] |
Andrea Frittoli (andreaf) | c625bcf | 2015-10-09 12:09:05 +0100 | [diff] [blame] | 333 | # Make sure a domain scope if defined for users in case of V3 |
Sean Dague | ed6e586 | 2016-04-04 10:49:13 -0400 | [diff] [blame] | 334 | # Make sure a tenant is available in case of V2 |
Andrea Frittoli (andreaf) | c625bcf | 2015-10-09 12:09:05 +0100 | [diff] [blame] | 335 | creds_dict = self._extend_credentials(creds_dict) |
| 336 | # This just builds a Credentials object, it does not validate |
| 337 | # nor fill with missing fields. |
| 338 | credential = auth.get_credentials( |
| 339 | auth_url=None, fill_in=False, |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 340 | identity_version=self.identity_version, **creds_dict) |
| 341 | net_creds = cred_provider.TestResources(credential) |
| 342 | net_clients = clients.Manager(credentials=credential) |
John Warren | 9487a18 | 2015-09-14 18:12:56 -0400 | [diff] [blame] | 343 | compute_network_client = net_clients.compute_networks_client |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 344 | net_name = self.hash_dict['networks'].get(hash, None) |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 345 | try: |
| 346 | network = fixed_network.get_network_from_name( |
| 347 | net_name, compute_network_client) |
Andrea Frittoli (andreaf) | 940f8c6 | 2015-10-30 16:39:24 +0900 | [diff] [blame] | 348 | except exceptions.InvalidTestResource: |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 349 | network = {} |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 350 | net_creds.set_resources(network=network) |
| 351 | return net_creds |
| 352 | |
Andrea Frittoli (andreaf) | c625bcf | 2015-10-09 12:09:05 +0100 | [diff] [blame] | 353 | def _extend_credentials(self, creds_dict): |
| 354 | # In case of v3, adds a user_domain_name field to the creds |
| 355 | # dict if not defined |
| 356 | if self.identity_version == 'v3': |
| 357 | user_domain_fields = set(['user_domain_name', 'user_domain_id']) |
| 358 | if not user_domain_fields.intersection(set(creds_dict.keys())): |
Andrea Frittoli (andreaf) | 1eb0496 | 2015-10-09 14:48:06 +0100 | [diff] [blame] | 359 | creds_dict['user_domain_name'] = self.credentials_domain |
Sean Dague | ed6e586 | 2016-04-04 10:49:13 -0400 | [diff] [blame] | 360 | # NOTE(andreaf) In case of v2, replace project with tenant if project |
| 361 | # is provided and tenant is not |
| 362 | if self.identity_version == 'v2': |
| 363 | if ('project_name' in creds_dict and |
| 364 | 'tenant_name' in creds_dict and |
| 365 | creds_dict['project_name'] != creds_dict['tenant_name']): |
| 366 | clean_creds = self._sanitize_creds(creds_dict) |
| 367 | msg = 'Cannot specify project and tenant at the same time %s' |
| 368 | raise exceptions.InvalidCredentials(msg % clean_creds) |
| 369 | if ('project_name' in creds_dict and |
| 370 | 'tenant_name' not in creds_dict): |
| 371 | creds_dict['tenant_name'] = creds_dict['project_name'] |
| 372 | creds_dict.pop('project_name') |
Andrea Frittoli (andreaf) | c625bcf | 2015-10-09 12:09:05 +0100 | [diff] [blame] | 373 | return creds_dict |