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