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 | |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 17 | import six |
| 18 | |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 19 | from tempest import auth |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 20 | from tempest import config |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 21 | from tempest import exceptions |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 22 | from tempest.openstack.common import log as logging |
| 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 = { |
| 29 | 'identity_admin': ('identity', 'admin'), |
| 30 | 'user': ('identity', None), |
| 31 | 'alt_user': ('identity', 'alt') |
| 32 | } |
| 33 | |
| 34 | |
| 35 | # Read credentials from configuration, builds a Credentials object |
| 36 | # based on the specified or configured version |
| 37 | def get_configured_credentials(credential_type, fill_in=True, |
| 38 | identity_version=None): |
| 39 | identity_version = identity_version or CONF.identity.auth_version |
| 40 | if identity_version not in ('v2', 'v3'): |
| 41 | raise exceptions.InvalidConfiguration( |
| 42 | 'Unsupported auth version: %s' % identity_version) |
| 43 | if credential_type not in CREDENTIAL_TYPES: |
| 44 | raise exceptions.InvalidCredentials() |
| 45 | conf_attributes = ['username', 'password', 'tenant_name'] |
| 46 | if identity_version == 'v3': |
| 47 | conf_attributes.append('domain_name') |
| 48 | # Read the parts of credentials from config |
| 49 | params = {} |
| 50 | section, prefix = CREDENTIAL_TYPES[credential_type] |
| 51 | for attr in conf_attributes: |
| 52 | _section = getattr(CONF, section) |
| 53 | if prefix is None: |
| 54 | params[attr] = getattr(_section, attr) |
| 55 | else: |
| 56 | params[attr] = getattr(_section, prefix + "_" + attr) |
| 57 | # Build and validate credentials. We are reading configured credentials, |
| 58 | # so validate them even if fill_in is False |
| 59 | credentials = auth.get_credentials(fill_in=fill_in, **params) |
| 60 | if not fill_in: |
| 61 | if not credentials.is_valid(): |
| 62 | msg = ("The %s credentials are incorrectly set in the config file." |
| 63 | " Double check that all required values are assigned" % |
| 64 | credential_type) |
| 65 | raise exceptions.InvalidConfiguration(msg) |
| 66 | return credentials |
| 67 | |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 68 | |
| 69 | @six.add_metaclass(abc.ABCMeta) |
| 70 | class CredentialProvider(object): |
Andrea Frittoli | c097835 | 2015-02-06 15:57:40 +0000 | [diff] [blame^] | 71 | def __init__(self, name, password='pass', network_resources=None): |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 72 | self.name = name |
| 73 | |
| 74 | @abc.abstractmethod |
| 75 | def get_primary_creds(self): |
| 76 | return |
| 77 | |
| 78 | @abc.abstractmethod |
| 79 | def get_admin_creds(self): |
| 80 | return |
| 81 | |
| 82 | @abc.abstractmethod |
| 83 | def get_alt_creds(self): |
| 84 | return |
| 85 | |
| 86 | @abc.abstractmethod |
| 87 | def clear_isolated_creds(self): |
| 88 | return |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 89 | |
| 90 | @abc.abstractmethod |
| 91 | def is_multi_user(self): |
| 92 | return |
Yair Fried | 76488d7 | 2014-10-21 10:13:19 +0300 | [diff] [blame] | 93 | |
| 94 | @abc.abstractmethod |
| 95 | def is_multi_tenant(self): |
| 96 | return |