blob: 641d7272bba3d69f3bfcfec2104c9781388b4ace [file] [log] [blame]
Matthew Treinishc791ac42014-07-16 09:15:23 -04001# 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
15import hashlib
16import os
Matthew Treinish96e9e882014-06-09 18:37:19 -040017
Doug Hellmann583ce2c2015-03-11 14:55:46 +000018from oslo_concurrency import lockutils
19from oslo_log import log as logging
Matthew Treinish1c517a22015-04-23 11:39:44 -040020import six
Matthew Treinishc791ac42014-07-16 09:15:23 -040021import yaml
22
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050023from tempest.lib import auth
Matthew Treinish00ab6be2016-10-07 16:29:18 -040024from tempest.lib.common import cred_provider
Matthew Treinishb19c55d2017-07-17 12:38:35 -040025from tempest.lib.common import fixed_network
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050026from tempest.lib import exceptions as lib_exc
Andrea Frittolidcd91002017-07-18 11:34:13 +010027from tempest.lib.services import clients
Matthew Treinishc791ac42014-07-16 09:15:23 -040028
Matthew Treinishc791ac42014-07-16 09:15:23 -040029LOG = logging.getLogger(__name__)
30
31
32def read_accounts_yaml(path):
Matthew Treinishd89db1b2015-12-16 17:29:14 -050033 try:
34 with open(path, 'r') as yaml_file:
Dao Cong Tien40d02082017-01-16 16:59:18 +070035 accounts = yaml.safe_load(yaml_file)
Matthew Treinishd89db1b2015-12-16 17:29:14 -050036 except IOError:
Matthew Treinish4217a702016-10-07 17:27:11 -040037 raise lib_exc.InvalidConfiguration(
Matthew Treinishd89db1b2015-12-16 17:29:14 -050038 'The path for the test accounts file: %s '
39 'could not be found' % path)
Matthew Treinishc791ac42014-07-16 09:15:23 -040040 return accounts
41
42
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070043class PreProvisionedCredentialProvider(cred_provider.CredentialProvider):
Masayuki Igawaa1c3af32017-09-07 10:22:37 +090044 """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 Treinishc791ac42014-07-16 09:15:23 -040073
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +010074 # 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)848e3482015-10-12 14:17:21 +010079 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 Frittolidcd91002017-07-18 11:34:13 +010082 object_storage_reseller_admin_role=None, identity_uri=None):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070083 super(PreProvisionedCredentialProvider, self).__init__(
Andrea Frittoli (andreaf)1eb04962015-10-09 14:48:06 +010084 identity_version=identity_version, name=name,
Andrea Frittolidcd91002017-07-18 11:34:13 +010085 admin_role=admin_role, credentials_domain=credentials_domain,
86 identity_uri=identity_uri)
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +010087 self.test_accounts_file = test_accounts_file
Matthew Treinishd89db1b2015-12-16 17:29:14 -050088 if test_accounts_file:
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +010089 accounts = read_accounts_yaml(self.test_accounts_file)
Matthew Treinishb19eeb82014-09-04 09:57:46 -040090 else:
Andrea Frittoli (andreaf)ee5d56b2016-06-08 11:19:09 +010091 raise lib_exc.InvalidCredentials("No accounts file specified")
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +010092 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)17209bb2015-05-22 10:16:57 -070096 self._creds = {}
Matthew Treinishc791ac42014-07-16 09:15:23 -040097
98 @classmethod
Matthew Treinish976e8df2014-12-19 14:21:54 -050099 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
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100107 def get_hash_dict(cls, accounts, admin_role,
108 object_storage_operator_role=None,
109 object_storage_reseller_admin_role=None):
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400110 hash_dict = {'roles': {}, 'creds': {}, 'networks': {}}
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100111
Matthew Treinish976e8df2014-12-19 14:21:54 -0500112 # Loop over the accounts read from the yaml file
Matthew Treinishc791ac42014-07-16 09:15:23 -0400113 for account in accounts:
Matthew Treinish976e8df2014-12-19 14:21:54 -0500114 roles = []
115 types = []
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400116 resources = []
Matthew Treinish976e8df2014-12-19 14:21:54 -0500117 if 'roles' in account:
118 roles = account.pop('roles')
119 if 'types' in account:
120 types = account.pop('types')
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400121 if 'resources' in account:
122 resources = account.pop('resources')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400123 temp_hash = hashlib.md5()
guo yunxian7bbbec12016-08-21 20:03:10 +0800124 account_for_hash = dict((k, v) for (k, v) in account.items()
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100125 if k in cls.HASH_CRED_FIELDS)
126 temp_hash.update(six.text_type(account_for_hash).encode('utf-8'))
Matthew Treinish976e8df2014-12-19 14:21:54 -0500127 temp_hash_key = temp_hash.hexdigest()
128 hash_dict['creds'][temp_hash_key] = account
129 for role in roles:
130 hash_dict = cls._append_role(role, temp_hash_key,
131 hash_dict)
132 # If types are set for the account append the matching role
133 # subdict with the hash
134 for type in types:
135 if type == 'admin':
Andrea Frittoli (andreaf)29491a72015-10-13 11:24:17 +0100136 hash_dict = cls._append_role(admin_role, temp_hash_key,
137 hash_dict)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500138 elif type == 'operator':
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100139 if object_storage_operator_role:
140 hash_dict = cls._append_role(
141 object_storage_operator_role, temp_hash_key,
142 hash_dict)
143 else:
144 msg = ("Type 'operator' configured, but no "
145 "object_storage_operator_role specified")
146 raise lib_exc.InvalidCredentials(msg)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500147 elif type == 'reseller_admin':
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100148 if object_storage_reseller_admin_role:
149 hash_dict = cls._append_role(
150 object_storage_reseller_admin_role,
151 temp_hash_key,
152 hash_dict)
153 else:
154 msg = ("Type 'reseller_admin' configured, but no "
155 "object_storage_reseller_admin_role specified")
156 raise lib_exc.InvalidCredentials(msg)
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400157 # Populate the network subdict
158 for resource in resources:
159 if resource == 'network':
160 hash_dict['networks'][temp_hash_key] = resources[resource]
161 else:
Jordan Pittier525ec712016-12-07 17:51:26 +0100162 LOG.warning(
163 'Unknown resource type %s, ignoring this field',
164 resource
165 )
Matthew Treinishc791ac42014-07-16 09:15:23 -0400166 return hash_dict
167
Matthew Treinish09f17832014-08-15 15:22:50 -0400168 def is_multi_user(self):
Andrea Frittoli (andreaf)ee5d56b2016-06-08 11:19:09 +0100169 return len(self.hash_dict['creds']) > 1
Matthew Treinish09f17832014-08-15 15:22:50 -0400170
Yair Fried76488d72014-10-21 10:13:19 +0300171 def is_multi_tenant(self):
172 return self.is_multi_user()
173
Matthew Treinish09f17832014-08-15 15:22:50 -0400174 def _create_hash_file(self, hash_string):
Masayuki Igawa9e492ee2019-09-19 12:15:04 +0900175 path = os.path.join(self.accounts_dir, hash_string)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400176 if not os.path.isfile(path):
Matthew Treinish4041b262015-02-27 11:18:54 -0500177 with open(path, 'w') as fd:
178 fd.write(self.name)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400179 return True
180 return False
181
182 @lockutils.synchronized('test_accounts_io', external=True)
183 def _get_free_hash(self, hashes):
Matthew Treinish976e8df2014-12-19 14:21:54 -0500184 # Cast as a list because in some edge cases a set will be passed in
185 hashes = list(hashes)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400186 if not os.path.isdir(self.accounts_dir):
187 os.mkdir(self.accounts_dir)
188 # Create File from first hash (since none are in use)
189 self._create_hash_file(hashes[0])
190 return hashes[0]
Matthew Treinish4041b262015-02-27 11:18:54 -0500191 names = []
Matthew Treinish09f17832014-08-15 15:22:50 -0400192 for _hash in hashes:
193 res = self._create_hash_file(_hash)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400194 if res:
Matthew Treinish09f17832014-08-15 15:22:50 -0400195 return _hash
Matthew Treinish4041b262015-02-27 11:18:54 -0500196 else:
Masayuki Igawa9e492ee2019-09-19 12:15:04 +0900197 path = os.path.join(self.accounts_dir, _hash)
Matthew Treinish4041b262015-02-27 11:18:54 -0500198 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)848e3482015-10-12 14:17:21 +0100202 raise lib_exc.InvalidCredentials(msg)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400203
Matthew Treinish976e8df2014-12-19 14:21:54 -0500204 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)848e3482015-10-12 14:17:21 +0100212 raise lib_exc.InvalidCredentials(
Matthew Treinish976e8df2014-12-19 14:21:54 -0500213 "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
zhufl0892cb22016-05-06 14:46:00 +0800225 # 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)29491a72015-10-13 11:24:17 +0100228 admin_hashes = self.hash_dict['roles'].get(self.admin_role,
Matthew Treinish976e8df2014-12-19 14:21:54 -0500229 None)
Andrea Frittoli (andreaf)29491a72015-10-13 11:24:17 +0100230 if ((not roles or self.admin_role not in roles) and
Matthew Treinish976e8df2014-12-19 14:21:54 -0500231 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 Treinishfd683e82015-04-13 20:30:06 -0400237 def _sanitize_creds(self, creds):
238 temp_creds = creds.copy()
239 temp_creds.pop('password')
240 return temp_creds
241
Matthew Treinish976e8df2014-12-19 14:21:54 -0500242 def _get_creds(self, roles=None):
Matthew Treinish976e8df2014-12-19 14:21:54 -0500243 useable_hashes = self._get_match_hash_list(roles)
Masayuki Igawa0c0f0142017-04-10 17:22:02 +0900244 if not useable_hashes:
Andrea Frittoli (andreaf)16d4a9a2016-06-02 17:12:44 +0100245 msg = 'No users configured for type/roles %s' % roles
246 raise lib_exc.InvalidCredentials(msg)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500247 free_hash = self._get_free_hash(useable_hashes)
Matthew Treinishfd683e82015-04-13 20:30:06 -0400248 clean_creds = self._sanitize_creds(
249 self.hash_dict['creds'][free_hash])
Jordan Pittier525ec712016-12-07 17:51:26 +0100250 LOG.info('%s allocated creds:\n%s', self.name, clean_creds)
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400251 return self._wrap_creds_with_network(free_hash)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400252
253 @lockutils.synchronized('test_accounts_io', external=True)
Matthew Treinish09f17832014-08-15 15:22:50 -0400254 def remove_hash(self, hash_string):
255 hash_path = os.path.join(self.accounts_dir, hash_string)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400256 if not os.path.isfile(hash_path):
257 LOG.warning('Expected an account lock file %s to remove, but '
Jordan Pittier525ec712016-12-07 17:51:26 +0100258 'one did not exist', hash_path)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400259 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 Treinish976e8df2014-12-19 14:21:54 -0500265 for _hash in self.hash_dict['creds']:
266 # Comparing on the attributes that are expected in the YAML
Andrea Frittoli (andreaf)f39f9f32015-04-13 20:55:31 +0100267 init_attributes = creds.get_init_attributes()
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100268 # 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)f39f9f32015-04-13 20:55:31 +0100271 hash_attributes = self.hash_dict['creds'][_hash].copy()
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100272 # 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
afazekas40fcb9b2019-03-08 11:25:11 +0100275 k in init_attributes]):
Matthew Treinish09f17832014-08-15 15:22:50 -0400276 return _hash
Matthew Treinishc791ac42014-07-16 09:15:23 -0400277 raise AttributeError('Invalid credentials %s' % creds)
278
279 def remove_credentials(self, creds):
Matthew Treinish09f17832014-08-15 15:22:50 -0400280 _hash = self.get_hash(creds)
Matthew Treinishfd683e82015-04-13 20:30:06 -0400281 clean_creds = self._sanitize_creds(self.hash_dict['creds'][_hash])
Matthew Treinish09f17832014-08-15 15:22:50 -0400282 self.remove_hash(_hash)
Jordan Pittier525ec712016-12-07 17:51:26 +0100283 LOG.info("%s returned allocated creds:\n%s", self.name, clean_creds)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400284
285 def get_primary_creds(self):
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700286 if self._creds.get('primary'):
287 return self._creds.get('primary')
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400288 net_creds = self._get_creds()
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700289 self._creds['primary'] = net_creds
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400290 return net_creds
Matthew Treinishc791ac42014-07-16 09:15:23 -0400291
292 def get_alt_creds(self):
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700293 if self._creds.get('alt'):
294 return self._creds.get('alt')
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400295 net_creds = self._get_creds()
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700296 self._creds['alt'] = net_creds
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400297 return net_creds
Matthew Treinishc791ac42014-07-16 09:15:23 -0400298
Matthew Treinish976e8df2014-12-19 14:21:54 -0500299 def get_creds_by_roles(self, roles, force_new=False):
300 roles = list(set(roles))
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700301 exist_creds = self._creds.get(six.text_type(roles).encode(
Matthew Treinish1c517a22015-04-23 11:39:44 -0400302 'utf-8'), None)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500303 # 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 Ohmichiedff8862015-10-13 01:10:53 +0000305 # in the _creds dict will be moved.
Matthew Treinish976e8df2014-12-19 14:21:54 -0500306 if exist_creds and not force_new:
307 return exist_creds
308 elif exist_creds and force_new:
Andrea Frittoli52d3ffa2016-12-13 18:17:45 +0000309 # 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)17209bb2015-05-22 10:16:57 -0700312 six.text_type(len(self._creds)).encode('utf-8')
313 self._creds[new_index] = exist_creds
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400314 net_creds = self._get_creds(roles=roles)
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700315 self._creds[six.text_type(roles).encode('utf-8')] = net_creds
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400316 return net_creds
Matthew Treinish976e8df2014-12-19 14:21:54 -0500317
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700318 def clear_creds(self):
319 for creds in self._creds.values():
Matthew Treinishc791ac42014-07-16 09:15:23 -0400320 self.remove_credentials(creds)
321
322 def get_admin_creds(self):
Andrea Frittoli (andreaf)29491a72015-10-13 11:24:17 +0100323 return self.get_creds_by_roles([self.admin_role])
Matthew Treinish976e8df2014-12-19 14:21:54 -0500324
Matthew Treinish4a596932015-03-06 20:37:01 -0500325 def is_role_available(self, role):
Andrea Frittoli (andreaf)ee5d56b2016-06-08 11:19:09 +0100326 if self.hash_dict['roles'].get(role):
327 return True
328 return False
Matthew Treinish4a596932015-03-06 20:37:01 -0500329
330 def admin_available(self):
Andrea Frittoli (andreaf)29491a72015-10-13 11:24:17 +0100331 return self.is_role_available(self.admin_role)
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100332
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400333 def _wrap_creds_with_network(self, hash):
334 creds_dict = self.hash_dict['creds'][hash]
Andrea Frittoli (andreaf)c625bcf2015-10-09 12:09:05 +0100335 # Make sure a domain scope if defined for users in case of V3
Sean Dagueed6e5862016-04-04 10:49:13 -0400336 # Make sure a tenant is available in case of V2
Andrea Frittoli (andreaf)c625bcf2015-10-09 12:09:05 +0100337 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 Treinishf83f35c2015-04-10 11:59:11 -0400342 identity_version=self.identity_version, **creds_dict)
343 net_creds = cred_provider.TestResources(credential)
Andrea Frittolidcd91002017-07-18 11:34:13 +0100344 net_clients = clients.ServiceClients(credentials=credential,
345 identity_uri=self.identity_uri)
zhufl33289a22018-01-04 15:02:00 +0800346 networks_client = net_clients.network.NetworksClient()
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400347 net_name = self.hash_dict['networks'].get(hash, None)
Matthew Treinishbe855fd2015-04-16 13:10:49 -0400348 try:
349 network = fixed_network.get_network_from_name(
zhufl33289a22018-01-04 15:02:00 +0800350 net_name, networks_client)
Matthew Treinishb19c55d2017-07-17 12:38:35 -0400351 except lib_exc.InvalidTestResource:
Matthew Treinishbe855fd2015-04-16 13:10:49 -0400352 network = {}
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400353 net_creds.set_resources(network=network)
354 return net_creds
355
Andrea Frittoli (andreaf)c625bcf2015-10-09 12:09:05 +0100356 def _extend_credentials(self, creds_dict):
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100357 # Add or remove credential domain fields to fit the identity version
358 domain_fields = set(x for x in auth.KeystoneV3Credentials.ATTRIBUTES
359 if 'domain' in x)
360 msg = 'Assuming they are valid in the default domain.'
Andrea Frittoli (andreaf)c625bcf2015-10-09 12:09:05 +0100361 if self.identity_version == 'v3':
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100362 if not domain_fields.intersection(set(creds_dict.keys())):
363 msg = 'Using credentials %s for v3 API calls. ' + msg
364 LOG.warning(msg, self._sanitize_creds(creds_dict))
365 creds_dict['domain_name'] = self.credentials_domain
Sean Dagueed6e5862016-04-04 10:49:13 -0400366 if self.identity_version == 'v2':
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100367 if domain_fields.intersection(set(creds_dict.keys())):
368 msg = 'Using credentials %s for v2 API calls. ' + msg
369 LOG.warning(msg, self._sanitize_creds(creds_dict))
370 # Remove all valid domain attributes
371 for attr in domain_fields.intersection(set(creds_dict.keys())):
372 creds_dict.pop(attr)
Andrea Frittoli (andreaf)c625bcf2015-10-09 12:09:05 +0100373 return creds_dict