Merge "Refactor auth plugin loading"
This commit is contained in:
@@ -61,8 +61,7 @@ try to find it and if that fails, you would create it::
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from stevedore import driver
|
||||
|
||||
from openstack import module_loader
|
||||
from openstack import session
|
||||
from openstack import transport as xport
|
||||
|
||||
@@ -74,9 +73,6 @@ _logger = logging.getLogger(__name__)
|
||||
|
||||
class Connection(object):
|
||||
|
||||
AUTH_PLUGIN_NAMESPACE = "openstack.auth.plugin"
|
||||
"""Namespace for the authorization plugin entry point"""
|
||||
|
||||
def __init__(self, transport=None, authenticator=None, preference=None,
|
||||
verify=True, user_agent=USER_AGENT,
|
||||
auth_plugin=None, **auth_args):
|
||||
@@ -140,15 +136,7 @@ class Connection(object):
|
||||
def _create_authenticator(self, authenticator, auth_plugin, **auth_args):
|
||||
if authenticator:
|
||||
return authenticator
|
||||
if auth_plugin is None:
|
||||
auth_plugin = 'identity'
|
||||
|
||||
mgr = driver.DriverManager(
|
||||
namespace=self.AUTH_PLUGIN_NAMESPACE,
|
||||
name=auth_plugin,
|
||||
invoke_on_load=False,
|
||||
)
|
||||
plugin = mgr.driver
|
||||
plugin = module_loader.ModuleLoader().get_auth_plugin(auth_plugin)
|
||||
valid_list = plugin.valid_options
|
||||
args = dict((n, auth_args[n]) for n in valid_list if n in auth_args)
|
||||
return plugin(**args)
|
||||
|
39
openstack/module_loader.py
Normal file
39
openstack/module_loader.py
Normal file
@@ -0,0 +1,39 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""
|
||||
Load various modules for authorization and services.
|
||||
"""
|
||||
from stevedore import extension
|
||||
|
||||
from openstack import exceptions
|
||||
|
||||
|
||||
class ModuleLoader(object):
|
||||
|
||||
def __init__(self):
|
||||
self.auth_mgr = extension.ExtensionManager(
|
||||
namespace="openstack.auth.plugin",
|
||||
invoke_on_load=False,
|
||||
)
|
||||
|
||||
def get_auth_plugin(self, plugin_name):
|
||||
if not plugin_name:
|
||||
plugin_name = 'identity'
|
||||
try:
|
||||
return self.auth_mgr[plugin_name].plugin
|
||||
except KeyError:
|
||||
msg = ('Could not find authorization plugin <%s>' % plugin_name)
|
||||
raise exceptions.NoMatchingPlugin(msg)
|
||||
|
||||
def list_auth_plugins(self):
|
||||
return self.auth_mgr.names()
|
48
openstack/tests/test_module_loader.py
Normal file
48
openstack/tests/test_module_loader.py
Normal file
@@ -0,0 +1,48 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from openstack import exceptions
|
||||
from openstack import module_loader
|
||||
from openstack.tests import base
|
||||
|
||||
|
||||
class TestModuleLoader(base.TestCase):
|
||||
def test_load_identity_v2(self):
|
||||
plugin = module_loader.ModuleLoader().get_auth_plugin('identity_v2')
|
||||
self.assertEqual('openstack.auth.identity.v2', str(plugin.__module__))
|
||||
|
||||
def test_load_identity_v3(self):
|
||||
plugin = module_loader.ModuleLoader().get_auth_plugin('identity_v3')
|
||||
self.assertEqual('openstack.auth.identity.v3', str(plugin.__module__))
|
||||
|
||||
def test_load_identity_discoverable(self):
|
||||
plugin = module_loader.ModuleLoader().get_auth_plugin('identity')
|
||||
self.assertEqual('openstack.auth.identity.discoverable',
|
||||
str(plugin.__module__))
|
||||
|
||||
def test_load_none(self):
|
||||
plugin = module_loader.ModuleLoader().get_auth_plugin(None)
|
||||
self.assertEqual('openstack.auth.identity.discoverable',
|
||||
str(plugin.__module__))
|
||||
|
||||
def test_load_empty(self):
|
||||
plugin = module_loader.ModuleLoader().get_auth_plugin('')
|
||||
self.assertEqual('openstack.auth.identity.discoverable',
|
||||
str(plugin.__module__))
|
||||
|
||||
def test_load_bogus(self):
|
||||
self.assertRaises(exceptions.NoMatchingPlugin,
|
||||
module_loader.ModuleLoader().get_auth_plugin, 'wot')
|
||||
|
||||
def test_list_auth_plugins(self):
|
||||
plugins = sorted(module_loader.ModuleLoader().list_auth_plugins())
|
||||
self.assertEqual(['identity', 'identity_v2', 'identity_v3'], plugins)
|
Reference in New Issue
Block a user