| 1 |
# Copyright 2016, 2018 Red Hat, Inc. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
import requests |
| 17 |
from tempest.lib import auth |
| 18 |
|
| 19 |
from config_tempest import utils |
| 20 |
|
| 21 |
|
| 22 |
class Credentials(object): |
| 23 |
"""Class contains all needed credentials. |
| 24 |
|
| 25 |
Wrapps credentials obtained from TempestConf object and Tempest |
| 26 |
credentialsfrom auth library. |
| 27 |
""" |
| 28 |
def __init__(self, conf, admin, **kwargs): |
| 29 |
"""Init method of Credentials. |
| 30 |
|
| 31 |
:type conf: TempestConf object |
| 32 |
:param admin: True if the user is admin, False otherwise |
| 33 |
:type admin: Boolean |
| 34 |
""" |
| 35 |
self.admin = admin |
| 36 |
self._conf = conf |
| 37 |
self.verify = kwargs.get('verify', True) |
| 38 |
self.cert = kwargs.get('cert', None) |
| 39 |
self.username = self.get_credential('username') |
| 40 |
self.password = self.get_credential('password') |
| 41 |
self.project_name = self.get_credential('project_name') |
| 42 |
self.domain_name = self.get_credential('domain_name') |
| 43 |
self.user_domain_name = self.get_credential('user_domain_name') |
| 44 |
self.project_domain_name = self.get_credential('project_domain_name') |
| 45 |
self.identity_version = self._get_identity_version() |
| 46 |
self.api_version = 3 if self.identity_version == "v3" else 2 |
| 47 |
self.identity_region = self._conf.get_defaulted('identity', 'region') |
| 48 |
self.set_ssl_certificate_validation() |
| 49 |
self.set_credentials() |
| 50 |
|
| 51 |
def set_ssl_certificate_validation(self): |
| 52 |
# is there a specific CA bundle to use? |
| 53 |
# self.verify is either a boolean, in which case it controls whether |
| 54 |
# server's TLS certificates are verified, or a string, in which case |
| 55 |
# it is a path to a CA bundle to use, default in requests package |
| 56 |
# is True. |
| 57 |
if isinstance(self.verify, str): |
| 58 |
self.disable_ssl_certificate_validation = False |
| 59 |
self.ca_certs = self.verify |
| 60 |
self._conf.set('identity', 'ca_certificates_file', self.ca_certs) |
| 61 |
else: |
| 62 |
self.disable_ssl_certificate_validation = self._conf.get_defaulted( |
| 63 |
'identity', 'disable_ssl_certificate_validation' |
| 64 |
) |
| 65 |
self.ca_certs = self._conf.get_defaulted('identity', |
| 66 |
'ca_certificates_file') |
| 67 |
self._conf.set('identity', 'disable_ssl_certificate_validation', |
| 68 |
str(self.disable_ssl_certificate_validation)) |
| 69 |
|
| 70 |
def get_ssl_certificate_validation(self): |
| 71 |
return { |
| 72 |
'disable_ssl_certificate_validation': |
| 73 |
self.disable_ssl_certificate_validation, |
| 74 |
'ca_certs': self.ca_certs, |
| 75 |
} |
| 76 |
|
| 77 |
def get_credential(self, key): |
| 78 |
"""Helper for getting credential by its name. |
| 79 |
|
| 80 |
:param key: credential name |
| 81 |
:type key: string |
| 82 |
:returns: credential |
| 83 |
:rtype: string |
| 84 |
""" |
| 85 |
if self.admin: |
| 86 |
# admin credentials are stored in auth section |
| 87 |
# and are prefixed by 'admin_' |
| 88 |
return self._conf.get_defaulted('auth', 'admin_' + key) |
| 89 |
else: |
| 90 |
# Tempest doesn't have non admin credentials, but the |
| 91 |
# tool keeps them in identity section for further usage |
| 92 |
return self._conf.get_defaulted('identity', key) |
| 93 |
|
| 94 |
def _list_versions(self, base_url, **kwargs): |
| 95 |
resp = requests.get(base_url, **kwargs) |
| 96 |
data = resp.json() |
| 97 |
return data["versions"]["values"] |
| 98 |
|
| 99 |
def _get_identity_version(self): |
| 100 |
"""Looks for identity version in TempestConf object. |
| 101 |
|
| 102 |
:returns: identity version |
| 103 |
:rtype: string |
| 104 |
""" |
| 105 |
base_url = utils.get_base_url(self._conf.get("identity", "uri")) |
| 106 |
kwargs = { |
| 107 |
'verify': self.verify, |
| 108 |
'cert': self.cert, |
| 109 |
} |
| 110 |
versions = self._list_versions(base_url, **kwargs) |
| 111 |
for version in versions: |
| 112 |
if version["status"] == "stable" and "v3" in version["id"]: |
| 113 |
return "v3" |
| 114 |
return "v2" |
| 115 |
|
| 116 |
def _get_creds_kwargs(self): |
| 117 |
"""Creates kwargs. |
| 118 |
|
| 119 |
Kwargs based on the identity version, for obtaining |
| 120 |
Tempest credentials. |
| 121 |
|
| 122 |
:returns: kwargs |
| 123 |
:rtype: dict |
| 124 |
""" |
| 125 |
creds_kwargs = {'username': self.username, |
| 126 |
'password': self.password} |
| 127 |
if self.identity_version == 'v3': |
| 128 |
creds_kwargs.update( |
| 129 |
{ |
| 130 |
'project_name': self.project_name, |
| 131 |
'domain_name': self.domain_name, |
| 132 |
'project_domain_name': self.project_domain_name, |
| 133 |
'user_domain_name': self.user_domain_name |
| 134 |
} |
| 135 |
) |
| 136 |
else: |
| 137 |
creds_kwargs.update({'project_name': self.project_name}) |
| 138 |
return creds_kwargs |
| 139 |
|
| 140 |
def set_credentials(self): |
| 141 |
"""Gets and saves Tempest credentials from auth library.""" |
| 142 |
creds_kwargs = self._get_creds_kwargs() |
| 143 |
disable_ssl = self.disable_ssl_certificate_validation |
| 144 |
self.tempest_creds = auth.get_credentials( |
| 145 |
auth_url=None, |
| 146 |
fill_in=False, |
| 147 |
identity_version=self.identity_version, |
| 148 |
disable_ssl_certificate_validation=disable_ssl, |
| 149 |
ca_certs=self.ca_certs, |
| 150 |
**creds_kwargs) |
| 151 |
|
| 152 |
def get_auth_provider(self): |
| 153 |
"""Gets auth provider based on the type of Tempest credentials. |
| 154 |
|
| 155 |
:returns: auth provider |
| 156 |
:rtype: auth.KeystoneV2AuthProvider/auth.KeystoneV3AuthProvider |
| 157 |
""" |
| 158 |
if isinstance(self.tempest_creds, auth.KeystoneV3Credentials): |
| 159 |
# We set uri and uri_v3 to /v3 here because if the endpoint on the |
| 160 |
# rc file don't set the /v3 it will fail with a error 404 |
| 161 |
uri = self._conf.get_defaulted('identity', 'uri_v3') |
| 162 |
uri = utils.get_base_url(uri) + 'v3' |
| 163 |
self._conf.set('identity', 'uri_v3', uri) |
| 164 |
return auth.KeystoneV3AuthProvider( |
| 165 |
self.tempest_creds, |
| 166 |
self._conf.get_defaulted('identity', 'uri_v3'), |
| 167 |
self.disable_ssl_certificate_validation, |
| 168 |
self.ca_certs) |
| 169 |
else: |
| 170 |
return auth.KeystoneV2AuthProvider( |
| 171 |
self.tempest_creds, |
| 172 |
self._conf.get_defaulted('identity', 'uri'), |
| 173 |
self.disable_ssl_certificate_validation, |
| 174 |
self.ca_certs) |