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