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 | |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 18 | import yaml |
| 19 | |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 20 | from tempest.common import cred_provider |
| 21 | from tempest import config |
| 22 | from tempest import exceptions |
| 23 | from tempest.openstack.common import lockutils |
| 24 | from tempest.openstack.common import log as logging |
| 25 | |
| 26 | CONF = config.CONF |
| 27 | LOG = logging.getLogger(__name__) |
| 28 | |
| 29 | |
| 30 | def read_accounts_yaml(path): |
| 31 | yaml_file = open(path, 'r') |
| 32 | accounts = yaml.load(yaml_file) |
| 33 | return accounts |
| 34 | |
| 35 | |
| 36 | class Accounts(cred_provider.CredentialProvider): |
| 37 | |
| 38 | def __init__(self, name): |
| 39 | super(Accounts, self).__init__(name) |
Matthew Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 40 | if os.path.isfile(CONF.auth.test_accounts_file): |
| 41 | accounts = read_accounts_yaml(CONF.auth.test_accounts_file) |
| 42 | self.use_default_creds = False |
| 43 | else: |
| 44 | accounts = {} |
| 45 | self.use_default_creds = True |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 46 | self.hash_dict = self.get_hash_dict(accounts) |
| 47 | self.accounts_dir = os.path.join(CONF.lock_path, 'test_accounts') |
| 48 | self.isolated_creds = {} |
| 49 | |
| 50 | @classmethod |
| 51 | def get_hash_dict(cls, accounts): |
| 52 | hash_dict = {} |
| 53 | for account in accounts: |
| 54 | temp_hash = hashlib.md5() |
| 55 | temp_hash.update(str(account)) |
| 56 | hash_dict[temp_hash.hexdigest()] = account |
| 57 | return hash_dict |
| 58 | |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 59 | def is_multi_user(self): |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 60 | # Default credentials is not a valid option with locking Account |
| 61 | if self.use_default_creds: |
| 62 | raise exceptions.InvalidConfiguration( |
| 63 | "Account file %s doesn't exist" % CONF.auth.test_accounts_file) |
| 64 | else: |
| 65 | return len(self.hash_dict) > 1 |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 66 | |
Yair Fried | 76488d7 | 2014-10-21 10:13:19 +0300 | [diff] [blame] | 67 | def is_multi_tenant(self): |
| 68 | return self.is_multi_user() |
| 69 | |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 70 | def _create_hash_file(self, hash_string): |
| 71 | path = os.path.join(os.path.join(self.accounts_dir, hash_string)) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 72 | if not os.path.isfile(path): |
| 73 | open(path, 'w').close() |
| 74 | return True |
| 75 | return False |
| 76 | |
| 77 | @lockutils.synchronized('test_accounts_io', external=True) |
| 78 | def _get_free_hash(self, hashes): |
| 79 | if not os.path.isdir(self.accounts_dir): |
| 80 | os.mkdir(self.accounts_dir) |
| 81 | # Create File from first hash (since none are in use) |
| 82 | self._create_hash_file(hashes[0]) |
| 83 | return hashes[0] |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 84 | for _hash in hashes: |
| 85 | res = self._create_hash_file(_hash) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 86 | if res: |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 87 | return _hash |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 88 | msg = 'Insufficient number of users provided' |
| 89 | raise exceptions.InvalidConfiguration(msg) |
| 90 | |
| 91 | def _get_creds(self): |
Matthew Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 92 | if self.use_default_creds: |
| 93 | raise exceptions.InvalidConfiguration( |
| 94 | "Account file %s doesn't exist" % CONF.auth.test_accounts_file) |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 95 | free_hash = self._get_free_hash(self.hash_dict.keys()) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 96 | return self.hash_dict[free_hash] |
| 97 | |
| 98 | @lockutils.synchronized('test_accounts_io', external=True) |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 99 | def remove_hash(self, hash_string): |
| 100 | hash_path = os.path.join(self.accounts_dir, hash_string) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 101 | if not os.path.isfile(hash_path): |
| 102 | LOG.warning('Expected an account lock file %s to remove, but ' |
Matthew Treinish | 53a2b4b | 2015-02-24 23:32:07 -0500 | [diff] [blame] | 103 | 'one did not exist' % hash_path) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 104 | else: |
| 105 | os.remove(hash_path) |
| 106 | if not os.listdir(self.accounts_dir): |
| 107 | os.rmdir(self.accounts_dir) |
| 108 | |
| 109 | def get_hash(self, creds): |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 110 | for _hash in self.hash_dict: |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 111 | # Comparing on the attributes that were read from the YAML |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 112 | if all([getattr(creds, k) == self.hash_dict[_hash][k] for k in |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 113 | creds.get_init_attributes()]): |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 114 | return _hash |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 115 | raise AttributeError('Invalid credentials %s' % creds) |
| 116 | |
| 117 | def remove_credentials(self, creds): |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 118 | _hash = self.get_hash(creds) |
| 119 | self.remove_hash(_hash) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 120 | |
| 121 | def get_primary_creds(self): |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 122 | if self.isolated_creds.get('primary'): |
| 123 | return self.isolated_creds.get('primary') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 124 | creds = self._get_creds() |
Andrea Frittoli | 878d5ab | 2015-01-30 13:22:50 +0000 | [diff] [blame] | 125 | primary_credential = cred_provider.get_credentials(**creds) |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 126 | self.isolated_creds['primary'] = primary_credential |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 127 | return primary_credential |
| 128 | |
| 129 | def get_alt_creds(self): |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 130 | if self.isolated_creds.get('alt'): |
| 131 | return self.isolated_creds.get('alt') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 132 | creds = self._get_creds() |
Andrea Frittoli | 878d5ab | 2015-01-30 13:22:50 +0000 | [diff] [blame] | 133 | alt_credential = cred_provider.get_credentials(**creds) |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 134 | self.isolated_creds['alt'] = alt_credential |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 135 | return alt_credential |
| 136 | |
| 137 | def clear_isolated_creds(self): |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 138 | for creds in self.isolated_creds.values(): |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 139 | self.remove_credentials(creds) |
| 140 | |
| 141 | def get_admin_creds(self): |
| 142 | msg = ('If admin credentials are available tenant_isolation should be' |
| 143 | ' used instead') |
| 144 | raise NotImplementedError(msg) |
Andrea Frittoli | b1c23fc | 2014-09-03 13:40:08 +0100 | [diff] [blame] | 145 | |
| 146 | |
| 147 | class NotLockingAccounts(Accounts): |
| 148 | """Credentials provider which always returns the first and second |
| 149 | configured accounts as primary and alt users. |
| 150 | This credential provider can be used in case of serial test execution |
| 151 | to preserve the current behaviour of the serial tempest run. |
| 152 | """ |
| 153 | |
Yair Fried | 76488d7 | 2014-10-21 10:13:19 +0300 | [diff] [blame] | 154 | def _unique_creds(self, cred_arg=None): |
| 155 | """Verify that the configured credentials are valid and distinct """ |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 156 | if self.use_default_creds: |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 157 | try: |
| 158 | user = self.get_primary_creds() |
| 159 | alt_user = self.get_alt_creds() |
Yair Fried | 76488d7 | 2014-10-21 10:13:19 +0300 | [diff] [blame] | 160 | return getattr(user, cred_arg) != getattr(alt_user, cred_arg) |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 161 | except exceptions.InvalidCredentials as ic: |
| 162 | msg = "At least one of the configured credentials is " \ |
| 163 | "not valid: %s" % ic.message |
| 164 | raise exceptions.InvalidConfiguration(msg) |
| 165 | else: |
| 166 | # TODO(andreaf) Add a uniqueness check here |
| 167 | return len(self.hash_dict) > 1 |
| 168 | |
Yair Fried | 76488d7 | 2014-10-21 10:13:19 +0300 | [diff] [blame] | 169 | def is_multi_user(self): |
| 170 | return self._unique_creds('username') |
| 171 | |
| 172 | def is_multi_tenant(self): |
| 173 | return self._unique_creds('tenant_id') |
| 174 | |
Andrea Frittoli | b1c23fc | 2014-09-03 13:40:08 +0100 | [diff] [blame] | 175 | def get_creds(self, id): |
| 176 | try: |
| 177 | # No need to sort the dict as within the same python process |
| 178 | # the HASH seed won't change, so subsequent calls to keys() |
| 179 | # will return the same result |
| 180 | _hash = self.hash_dict.keys()[id] |
| 181 | except IndexError: |
| 182 | msg = 'Insufficient number of users provided' |
| 183 | raise exceptions.InvalidConfiguration(msg) |
| 184 | return self.hash_dict[_hash] |
| 185 | |
| 186 | def get_primary_creds(self): |
| 187 | if self.isolated_creds.get('primary'): |
| 188 | return self.isolated_creds.get('primary') |
Matthew Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 189 | if not self.use_default_creds: |
| 190 | creds = self.get_creds(0) |
Andrea Frittoli | 878d5ab | 2015-01-30 13:22:50 +0000 | [diff] [blame] | 191 | primary_credential = cred_provider.get_credentials(**creds) |
Matthew Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 192 | else: |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 193 | primary_credential = cred_provider.get_configured_credentials( |
| 194 | 'user') |
Andrea Frittoli | b1c23fc | 2014-09-03 13:40:08 +0100 | [diff] [blame] | 195 | self.isolated_creds['primary'] = primary_credential |
| 196 | return primary_credential |
| 197 | |
| 198 | def get_alt_creds(self): |
| 199 | if self.isolated_creds.get('alt'): |
| 200 | return self.isolated_creds.get('alt') |
Matthew Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 201 | if not self.use_default_creds: |
| 202 | creds = self.get_creds(1) |
Andrea Frittoli | 878d5ab | 2015-01-30 13:22:50 +0000 | [diff] [blame] | 203 | alt_credential = cred_provider.get_credentials(**creds) |
Matthew Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 204 | else: |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 205 | alt_credential = cred_provider.get_configured_credentials( |
| 206 | 'alt_user') |
Andrea Frittoli | b1c23fc | 2014-09-03 13:40:08 +0100 | [diff] [blame] | 207 | self.isolated_creds['alt'] = alt_credential |
| 208 | return alt_credential |
| 209 | |
| 210 | def clear_isolated_creds(self): |
| 211 | self.isolated_creds = {} |
| 212 | |
| 213 | def get_admin_creds(self): |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 214 | return cred_provider.get_configured_credentials( |
| 215 | "identity_admin", fill_in=False) |