Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 1 | # Copyright (c) 2014 Deutsche Telekom AG |
| 2 | # Copyright (c) 2014 Hewlett-Packard Development Company, L.P. |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain 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, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import abc |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 16 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 17 | from oslo_log import log as logging |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 18 | import six |
andreaf | b8a5228 | 2015-03-19 22:21:54 +0000 | [diff] [blame] | 19 | from tempest_lib import auth |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 20 | |
| 21 | from tempest import config |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 22 | from tempest import exceptions |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 23 | |
| 24 | CONF = config.CONF |
| 25 | LOG = logging.getLogger(__name__) |
| 26 | |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 27 | # Type of credentials available from configuration |
| 28 | CREDENTIAL_TYPES = { |
Matthew Treinish | 16cf1e5 | 2015-08-11 10:39:23 -0400 | [diff] [blame] | 29 | 'identity_admin': ('auth', 'admin'), |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 30 | 'user': ('identity', None), |
| 31 | 'alt_user': ('identity', 'alt') |
| 32 | } |
| 33 | |
Andrea Frittoli | 7c4ed9c | 2015-03-09 15:27:50 +0000 | [diff] [blame] | 34 | DEFAULT_PARAMS = { |
| 35 | 'disable_ssl_certificate_validation': |
| 36 | CONF.identity.disable_ssl_certificate_validation, |
| 37 | 'ca_certs': CONF.identity.ca_certificates_file, |
| 38 | 'trace_requests': CONF.debug.trace_requests |
| 39 | } |
| 40 | |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 41 | |
| 42 | # Read credentials from configuration, builds a Credentials object |
| 43 | # based on the specified or configured version |
| 44 | def get_configured_credentials(credential_type, fill_in=True, |
| 45 | identity_version=None): |
| 46 | identity_version = identity_version or CONF.identity.auth_version |
| 47 | if identity_version not in ('v2', 'v3'): |
| 48 | raise exceptions.InvalidConfiguration( |
| 49 | 'Unsupported auth version: %s' % identity_version) |
| 50 | if credential_type not in CREDENTIAL_TYPES: |
| 51 | raise exceptions.InvalidCredentials() |
| 52 | conf_attributes = ['username', 'password', 'tenant_name'] |
| 53 | if identity_version == 'v3': |
| 54 | conf_attributes.append('domain_name') |
| 55 | # Read the parts of credentials from config |
Andrea Frittoli | 6489629 | 2015-03-12 16:29:59 +0000 | [diff] [blame] | 56 | params = DEFAULT_PARAMS.copy() |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 57 | section, prefix = CREDENTIAL_TYPES[credential_type] |
| 58 | for attr in conf_attributes: |
| 59 | _section = getattr(CONF, section) |
| 60 | if prefix is None: |
| 61 | params[attr] = getattr(_section, attr) |
| 62 | else: |
| 63 | params[attr] = getattr(_section, prefix + "_" + attr) |
| 64 | # Build and validate credentials. We are reading configured credentials, |
| 65 | # so validate them even if fill_in is False |
Andrea Frittoli (andreaf) | 484ba66 | 2015-03-25 16:25:29 -0400 | [diff] [blame] | 66 | credentials = get_credentials(fill_in=fill_in, |
| 67 | identity_version=identity_version, **params) |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 68 | if not fill_in: |
| 69 | if not credentials.is_valid(): |
| 70 | msg = ("The %s credentials are incorrectly set in the config file." |
| 71 | " Double check that all required values are assigned" % |
| 72 | credential_type) |
| 73 | raise exceptions.InvalidConfiguration(msg) |
| 74 | return credentials |
| 75 | |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 76 | |
Andrea Frittoli | 878d5ab | 2015-01-30 13:22:50 +0000 | [diff] [blame] | 77 | # Wrapper around auth.get_credentials to use the configured identity version |
| 78 | # is none is specified |
| 79 | def get_credentials(fill_in=True, identity_version=None, **kwargs): |
Andrea Frittoli | 7c4ed9c | 2015-03-09 15:27:50 +0000 | [diff] [blame] | 80 | params = dict(DEFAULT_PARAMS, **kwargs) |
Andrea Frittoli | 878d5ab | 2015-01-30 13:22:50 +0000 | [diff] [blame] | 81 | identity_version = identity_version or CONF.identity.auth_version |
Andrea Frittoli | 5c05d59 | 2015-01-30 13:42:32 +0000 | [diff] [blame] | 82 | # In case of "v3" add the domain from config if not specified |
| 83 | if identity_version == 'v3': |
| 84 | domain_fields = set(x for x in auth.KeystoneV3Credentials.ATTRIBUTES |
| 85 | if 'domain' in x) |
| 86 | if not domain_fields.intersection(kwargs.keys()): |
David Kranz | 87fc7e9 | 2015-07-28 14:05:20 -0400 | [diff] [blame] | 87 | domain_name = CONF.auth.default_credentials_domain_name |
| 88 | params['user_domain_name'] = domain_name |
| 89 | |
ghanshyam | 5ff763f | 2015-02-18 16:15:58 +0900 | [diff] [blame] | 90 | auth_url = CONF.identity.uri_v3 |
| 91 | else: |
| 92 | auth_url = CONF.identity.uri |
| 93 | return auth.get_credentials(auth_url, |
| 94 | fill_in=fill_in, |
Andrea Frittoli | 878d5ab | 2015-01-30 13:22:50 +0000 | [diff] [blame] | 95 | identity_version=identity_version, |
Andrea Frittoli | 7c4ed9c | 2015-03-09 15:27:50 +0000 | [diff] [blame] | 96 | **params) |
Andrea Frittoli | 878d5ab | 2015-01-30 13:22:50 +0000 | [diff] [blame] | 97 | |
| 98 | |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 99 | @six.add_metaclass(abc.ABCMeta) |
| 100 | class CredentialProvider(object): |
Andrea Frittoli (andreaf) | 1eb0496 | 2015-10-09 14:48:06 +0100 | [diff] [blame] | 101 | def __init__(self, identity_version, name=None, network_resources=None, |
Andrea Frittoli (andreaf) | 29491a7 | 2015-10-13 11:24:17 +0100 | [diff] [blame] | 102 | credentials_domain=None, admin_role=None): |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 103 | """A CredentialProvider supplies credentials to test classes. |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 104 | :param identity_version: Identity version of the credentials provided |
Ken'ichi Ohmichi | 592eb13 | 2015-07-01 04:08:30 +0000 | [diff] [blame] | 105 | :param name: Name of the calling test. Included in provisioned |
| 106 | credentials when credentials are provisioned on the fly |
| 107 | :param network_resources: Network resources required for the |
| 108 | credentials |
Andrea Frittoli (andreaf) | 1eb0496 | 2015-10-09 14:48:06 +0100 | [diff] [blame] | 109 | :param credentials_domain: Domain credentials belong to |
Andrea Frittoli (andreaf) | 29491a7 | 2015-10-13 11:24:17 +0100 | [diff] [blame] | 110 | :param admin_role: Name of the role of the admin account |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 111 | """ |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 112 | self.identity_version = identity_version |
Andrea Frittoli (andreaf) | 1eb0496 | 2015-10-09 14:48:06 +0100 | [diff] [blame] | 113 | self.name = name or "test_creds" |
| 114 | self.network_resources = network_resources |
| 115 | self.credentials_domain = credentials_domain or 'Default' |
Andrea Frittoli (andreaf) | 29491a7 | 2015-10-13 11:24:17 +0100 | [diff] [blame] | 116 | self.admin_role = admin_role |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 117 | if not auth.is_identity_version_supported(self.identity_version): |
| 118 | raise exceptions.InvalidIdentityVersion( |
| 119 | identity_version=self.identity_version) |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 120 | |
| 121 | @abc.abstractmethod |
| 122 | def get_primary_creds(self): |
| 123 | return |
| 124 | |
| 125 | @abc.abstractmethod |
| 126 | def get_admin_creds(self): |
| 127 | return |
| 128 | |
| 129 | @abc.abstractmethod |
| 130 | def get_alt_creds(self): |
| 131 | return |
| 132 | |
| 133 | @abc.abstractmethod |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 134 | def clear_creds(self): |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 135 | return |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 136 | |
| 137 | @abc.abstractmethod |
| 138 | def is_multi_user(self): |
| 139 | return |
Yair Fried | 76488d7 | 2014-10-21 10:13:19 +0300 | [diff] [blame] | 140 | |
| 141 | @abc.abstractmethod |
| 142 | def is_multi_tenant(self): |
| 143 | return |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 144 | |
| 145 | @abc.abstractmethod |
| 146 | def get_creds_by_roles(self, roles, force_new=False): |
| 147 | return |
Matthew Treinish | 4a59693 | 2015-03-06 20:37:01 -0500 | [diff] [blame] | 148 | |
| 149 | @abc.abstractmethod |
| 150 | def is_role_available(self, role): |
| 151 | return |
Andrea Frittoli (andreaf) | 9540dfd | 2015-03-25 17:06:50 -0400 | [diff] [blame] | 152 | |
| 153 | |
| 154 | class TestResources(object): |
| 155 | """Readonly Credentials, with network resources added.""" |
| 156 | |
| 157 | def __init__(self, credentials): |
| 158 | self._credentials = credentials |
| 159 | self.network = None |
| 160 | self.subnet = None |
| 161 | self.router = None |
| 162 | |
| 163 | def __getattr__(self, item): |
| 164 | return getattr(self._credentials, item) |
| 165 | |
| 166 | def set_resources(self, **kwargs): |
| 167 | for key in kwargs.keys(): |
| 168 | if hasattr(self, key): |
| 169 | setattr(self, key, kwargs[key]) |
| 170 | |
| 171 | @property |
| 172 | def credentials(self): |
| 173 | return self._credentials |