blob: 9630d1c109ab2a787786634be4e15b7488314c12 [file] [log] [blame]
Andrea Frittoli8283b4e2014-07-17 13:28:58 +01001# Copyright (c) 2014 Deutsche Telekom AG
2# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
Marc Kodererd2690fe2014-07-16 14:17:47 +02003# 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
15import abc
Matthew Treinish96e9e882014-06-09 18:37:19 -040016
Doug Hellmann583ce2c2015-03-11 14:55:46 +000017from oslo_log import log as logging
Marc Kodererd2690fe2014-07-16 14:17:47 +020018import six
19
Andrea Frittoli9efbe952015-01-29 12:43:09 +000020from tempest import auth
Marc Kodererd2690fe2014-07-16 14:17:47 +020021from tempest import config
Andrea Frittoli9efbe952015-01-29 12:43:09 +000022from tempest import exceptions
Marc Kodererd2690fe2014-07-16 14:17:47 +020023
24CONF = config.CONF
25LOG = logging.getLogger(__name__)
26
Andrea Frittoli9efbe952015-01-29 12:43:09 +000027# Type of credentials available from configuration
28CREDENTIAL_TYPES = {
29 'identity_admin': ('identity', 'admin'),
30 'user': ('identity', None),
31 'alt_user': ('identity', 'alt')
32}
33
Andrea Frittoli7c4ed9c2015-03-09 15:27:50 +000034DEFAULT_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 Frittoli9efbe952015-01-29 12:43:09 +000041
42# Read credentials from configuration, builds a Credentials object
43# based on the specified or configured version
44def 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 Frittoli64896292015-03-12 16:29:59 +000056 params = DEFAULT_PARAMS.copy()
Andrea Frittoli9efbe952015-01-29 12:43:09 +000057 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)484ba662015-03-25 16:25:29 -040066 credentials = get_credentials(fill_in=fill_in,
67 identity_version=identity_version, **params)
Andrea Frittoli9efbe952015-01-29 12:43:09 +000068 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 Kodererd2690fe2014-07-16 14:17:47 +020076
Andrea Frittoli878d5ab2015-01-30 13:22:50 +000077# Wrapper around auth.get_credentials to use the configured identity version
78# is none is specified
79def get_credentials(fill_in=True, identity_version=None, **kwargs):
Andrea Frittoli7c4ed9c2015-03-09 15:27:50 +000080 params = dict(DEFAULT_PARAMS, **kwargs)
Andrea Frittoli878d5ab2015-01-30 13:22:50 +000081 identity_version = identity_version or CONF.identity.auth_version
Andrea Frittoli5c05d592015-01-30 13:42:32 +000082 # 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()):
Andrea Frittolic3280152015-02-26 12:42:34 +000087 params['user_domain_name'] = CONF.identity.admin_domain_name
ghanshyam5ff763f2015-02-18 16:15:58 +090088 auth_url = CONF.identity.uri_v3
89 else:
90 auth_url = CONF.identity.uri
91 return auth.get_credentials(auth_url,
92 fill_in=fill_in,
Andrea Frittoli878d5ab2015-01-30 13:22:50 +000093 identity_version=identity_version,
Andrea Frittoli7c4ed9c2015-03-09 15:27:50 +000094 **params)
Andrea Frittoli878d5ab2015-01-30 13:22:50 +000095
96
Marc Kodererd2690fe2014-07-16 14:17:47 +020097@six.add_metaclass(abc.ABCMeta)
98class CredentialProvider(object):
Andrea Frittolic3280152015-02-26 12:42:34 +000099 def __init__(self, identity_version=None, name=None, password='pass',
100 network_resources=None):
101 """A CredentialProvider supplies credentials to test classes.
102 :param identity_version If specified it will return credentials of the
103 corresponding identity version, otherwise it
104 uses auth_version from configuration
105 :param name Name of the calling test. Included in provisioned
106 credentials when credentials are provisioned on the fly
107 :param password Used for provisioned credentials when credentials are
108 provisioned on the fly
109 :param network_resources Network resources required for the credentials
110 """
111 # TODO(andreaf) name and password are tenant isolation specific, and
112 # could be removed from this abstract class
113 self.name = name or "test_creds"
114 self.identity_version = identity_version or CONF.identity.auth_version
115 if not auth.is_identity_version_supported(self.identity_version):
116 raise exceptions.InvalidIdentityVersion(
117 identity_version=self.identity_version)
Marc Kodererd2690fe2014-07-16 14:17:47 +0200118
119 @abc.abstractmethod
120 def get_primary_creds(self):
121 return
122
123 @abc.abstractmethod
124 def get_admin_creds(self):
125 return
126
127 @abc.abstractmethod
128 def get_alt_creds(self):
129 return
130
131 @abc.abstractmethod
132 def clear_isolated_creds(self):
133 return
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100134
135 @abc.abstractmethod
136 def is_multi_user(self):
137 return
Yair Fried76488d72014-10-21 10:13:19 +0300138
139 @abc.abstractmethod
140 def is_multi_tenant(self):
141 return
Matthew Treinish976e8df2014-12-19 14:21:54 -0500142
143 @abc.abstractmethod
144 def get_creds_by_roles(self, roles, force_new=False):
145 return
Matthew Treinish4a596932015-03-06 20:37:01 -0500146
147 @abc.abstractmethod
148 def is_role_available(self, role):
149 return