| 1 |
# Copyright 2013 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 json |
| 17 |
import requests |
| 18 |
|
| 19 |
from pathlib import PurePosixPath |
| 20 |
from six.moves import urllib |
| 21 |
|
| 22 |
from config_tempest.constants import LOG |
| 23 |
from config_tempest.services.base import VersionedService |
| 24 |
|
| 25 |
|
| 26 |
class IdentityService(VersionedService): |
| 27 |
def __init__(self, name, s_type, service_url, token, |
| 28 |
disable_ssl_validation, client=None, **kwargs): |
| 29 |
super(IdentityService, self).__init__( |
| 30 |
name, s_type, service_url, token, disable_ssl_validation, |
| 31 |
client, **kwargs) |
| 32 |
self.extensions_v3 = [] |
| 33 |
version = '' |
| 34 |
url_parse = urllib.parse.urlparse(self.service_url) |
| 35 |
if PurePosixPath(url_parse.path).name.startswith('v2'): |
| 36 |
version = '/v2.0' |
| 37 |
self.service_url = '{}://{}{}'.format(url_parse.scheme, |
| 38 |
url_parse.netloc, version) |
| 39 |
|
| 40 |
def set_extensions(self): |
| 41 |
url_parse = urllib.parse.urlparse(self.service_url) |
| 42 |
if PurePosixPath(url_parse.path).name.startswith('v2'): |
| 43 |
body = self.do_get(self.service_url + '/extensions') |
| 44 |
body = json.loads(body) |
| 45 |
values = body['extensions']['values'] |
| 46 |
self.extensions = list(map(lambda x: x['alias'], values)) |
| 47 |
return |
| 48 |
# Keystone api changed in v3, the concept of extensions changed. Right |
| 49 |
# now, all the existing extensions are part of keystone core api, so, |
| 50 |
# there's no longer the /extensions endpoint. The extensions that are |
| 51 |
# stable, are enabled by default, the ones marked as experimental are |
| 52 |
# disabled by default. Checking the tempest source, there's no test |
| 53 |
# pointing to extensions endpoint, so I am very confident that this |
| 54 |
# will not be an issue. If so, we need to list all the /OS-XYZ |
| 55 |
# extensions to identify what is enabled or not. This would be a manual |
| 56 |
# check every time keystone change, add or delete an extension, so I |
| 57 |
# rather prefer to set empty list here for now. |
| 58 |
self.extensions = [] |
| 59 |
|
| 60 |
def get_service_extension_key(self): |
| 61 |
return 'api_extensions' |
| 62 |
|
| 63 |
def get_supported_versions(self): |
| 64 |
return ['v2', 'v3'] |
| 65 |
|
| 66 |
@staticmethod |
| 67 |
def get_service_type(): |
| 68 |
return ['identity'] |
| 69 |
|
| 70 |
def set_identity_v3_extensions(self): |
| 71 |
"""Returns discovered identity v3 extensions |
| 72 |
|
| 73 |
As keystone V3 uses a JSON Home to store the extensions. |
| 74 |
This method implements a different discovery method. |
| 75 |
|
| 76 |
:return: A list with the discovered extensions |
| 77 |
""" |
| 78 |
try: |
| 79 |
r = requests.get(self.service_url, |
| 80 |
verify=False, |
| 81 |
headers={'Accept': 'application/json-home'}) |
| 82 |
# check for http status |
| 83 |
r.raise_for_status() |
| 84 |
except requests.exceptions.HTTPError: |
| 85 |
LOG.warning("Request on service '%s' with url '%s' failed, " |
| 86 |
"checking for v3", 'identity', self.service_url) |
| 87 |
if 'v3' not in self.service_url: |
| 88 |
self.service_url = self.service_url + '/v3' |
| 89 |
r = requests.get(self.service_url, |
| 90 |
verify=False, |
| 91 |
headers={'Accept': 'application/json-home'}) |
| 92 |
|
| 93 |
ext_h = 'https://docs.openstack.org/api/openstack-identity/3/ext/' |
| 94 |
content = r.content.decode('utf-8') |
| 95 |
res = [x for x in json.loads(content)['resources'].keys()] |
| 96 |
ext = [ex for ex in res if 'ext' in ex] |
| 97 |
ext = [str(e).replace(ext_h, '').split('/')[0] for e in ext] |
| 98 |
self.extensions_v3 = list(set(ext)) |
| 99 |
|
| 100 |
def set_versions(self): |
| 101 |
super(IdentityService, self).set_versions(top_level=False) |
| 102 |
|
| 103 |
def get_extensions(self): |
| 104 |
all_ext_lst = self.extensions + self.extensions_v3 |
| 105 |
return list(set(all_ext_lst)) |
| 106 |
|
| 107 |
def deserialize_versions(self, body): |
| 108 |
try: |
| 109 |
versions = [] |
| 110 |
for v in body['versions']['values']: |
| 111 |
# TripleO is in transition to v3 only, so the environment |
| 112 |
# still returns v2 versions even though they're deprecated. |
| 113 |
# Therefor pick only versions with stable status. |
| 114 |
if v['status'] == 'stable': |
| 115 |
versions.append(v['id']) |
| 116 |
return versions |
| 117 |
except KeyError: |
| 118 |
return [body['version']['id']] |
| 119 |
|
| 120 |
def set_default_tempest_options(self, conf): |
| 121 |
"""Set keystone feature flags based upon version ID.""" |
| 122 |
supported_versions = self.get_versions() |
| 123 |
|
| 124 |
if supported_versions: |
| 125 |
major, minor = supported_versions[0].split('.') |
| 126 |
conf.set('identity', 'auth_version', major) |
| 127 |
|
| 128 |
if len(supported_versions) <= 1: |
| 129 |
return |
| 130 |
for version in supported_versions: |
| 131 |
major, minor = version.split('.')[:2] |
| 132 |
# Enable the domain specific roles feature flag. |
| 133 |
# For more information see: |
| 134 |
# https://docs.openstack.org/api-ref/identity/v3 |
| 135 |
if major == 'v3' and int(minor) >= 6: |
| 136 |
conf.set('identity-feature-enabled', |
| 137 |
'forbid_global_implied_dsr', |
| 138 |
'True') |