Coverage for manila/common/client_auth.py: 100%
47 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright 2016 SAP SE
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.
16import copy
18from keystoneauth1 import loading as ks_loading
19from oslo_config import cfg
21from manila import exception
22from manila.i18n import _
24CONF = cfg.CONF
26"""Helper class to support keystone v2 and v3 for clients
28Builds auth and session context before instantiation of the actual
29client. In order to build this context a dedicated config group is
30needed to load all needed parameters dynamically.
33"""
36class AuthClientLoader(object):
37 def __init__(self, client_class, cfg_group):
38 self.client_class = client_class
39 self.group = cfg_group
40 self.admin_auth = None
41 self.conf = CONF
42 self.session = None
43 self.auth_plugin = None
45 @staticmethod
46 def list_opts(group):
47 """Generates a list of config option for a given group
49 :param group: group name
50 :return: list of auth default configuration
51 """
52 opts = copy.deepcopy(ks_loading.get_session_conf_options())
53 opts.insert(0, ks_loading.get_auth_common_conf_options()[0])
55 for plugin_option in ks_loading.get_auth_plugin_conf_options(
56 'password'):
57 found = False
58 for option in opts:
59 if option.name == plugin_option.name:
60 found = True
61 break
62 if not found:
63 opts.append(plugin_option)
64 opts.sort(key=lambda x: x.name)
65 return [(group, opts)]
67 def _load_auth_plugin(self):
68 if self.admin_auth:
69 return self.admin_auth
70 self.auth_plugin = ks_loading.load_auth_from_conf_options(
71 CONF, self.group)
73 if self.auth_plugin:
74 return self.auth_plugin
76 msg = _('Cannot load auth plugin for %s') % self.group
77 raise exception.BadConfigurationException(reason=msg)
79 def get_client(self, context, admin=False, **kwargs):
80 """Get's the client with the correct auth/session context
82 """
83 auth_plugin = None
85 if not self.session:
86 self.session = ks_loading.load_session_from_conf_options(
87 self.conf, self.group)
89 if admin or (context.is_admin and not context.auth_token):
90 if not self.admin_auth:
91 self.admin_auth = self._load_auth_plugin()
92 auth_plugin = self.admin_auth
93 else:
94 # NOTE(mkoderer): Manila basically needs admin clients for
95 # it's actions. If needed this must be enhanced later
96 raise exception.ManilaException(
97 _("Client (%s) is not flagged as admin") % self.group)
99 return self.client_class(session=self.session, auth=auth_plugin,
100 **kwargs)