Use Stevedore for better extensions

Since all Oslo library drivers are discoverable via
stevedore, we should use stevedore in Castellan as well.
This will make it easier for folks to write their own
custom drivers. Stevedore uses setuptools entry points
for implementing the common patterns for dynamically
loading extensions.

We add [key_manager]/backend as the new option to set
the custom driver. For a while, we should support the
older values that used to be specified using
[key_manager]/apiclass.

Change-Id: I2610459839806a5591da1efa314dfa52bcfb7cda
This commit is contained in:
Davanum Srinivas 2017-07-14 14:51:20 -04:00
parent 9787639cc6
commit 8980bf7da5
7 changed files with 67 additions and 15 deletions

View File

@ -13,13 +13,21 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import importutils from oslo_utils import importutils
from stevedore import driver
from stevedore import exception
LOG = logging.getLogger(__name__)
key_manager_opts = [ key_manager_opts = [
cfg.StrOpt('api_class', cfg.StrOpt('backend',
default='castellan.key_manager.barbican_key_manager' default='barbican',
'.BarbicanKeyManager', deprecated_name='api_class',
help='The full class name of the key manager API class'), deprecated_group='key_manager',
help='Specify the key manager implementation. Default is '
'"barbican".Will support the values earlier set using '
'[key_manager]/api_class for some time.'),
] ]
@ -27,5 +35,14 @@ def API(configuration=None):
conf = configuration or cfg.CONF conf = configuration or cfg.CONF
conf.register_opts(key_manager_opts, group='key_manager') conf.register_opts(key_manager_opts, group='key_manager')
cls = importutils.import_class(conf.key_manager.api_class) try:
return cls(configuration=conf) mgr = driver.DriverManager("castellan.drivers",
conf.key_manager.backend,
invoke_on_load=True,
invoke_args=[conf])
return mgr.driver
except exception.NoMatches:
LOG.warning("Deprecation Warning : %s is not a stevedore based driver,"
" trying to load it as a class", conf.key_manager.backend)
cls = importutils.import_class(conf.key_manager.backend)
return cls(configuration=conf)

View File

@ -30,9 +30,10 @@ _DEFAULT_LOGGING_CONTEXT_FORMAT = ('%(asctime)s.%(msecs)03d %(process)d '
'%(message)s') '%(message)s')
def set_defaults(conf, api_class=None, barbican_endpoint=None, def set_defaults(conf, backend=None, barbican_endpoint=None,
barbican_api_version=None, auth_endpoint=None, barbican_api_version=None, auth_endpoint=None,
retry_delay=None, number_of_retries=None, verify_ssl=None): retry_delay=None, number_of_retries=None, verify_ssl=None,
api_class=None):
"""Set defaults for configuration values. """Set defaults for configuration values.
Overrides the default options values. Overrides the default options values.
@ -49,8 +50,10 @@ def set_defaults(conf, api_class=None, barbican_endpoint=None,
if bkm: if bkm:
conf.register_opts(bkm.barbican_opts, group=bkm.BARBICAN_OPT_GROUP) conf.register_opts(bkm.barbican_opts, group=bkm.BARBICAN_OPT_GROUP)
if api_class is not None: # Use the new backend option if set or fall back to the older api_class
conf.set_default('api_class', api_class, group='key_manager') default_backend = backend or api_class
if default_backend is not None:
conf.set_default('backend', default_backend, group='key_manager')
if bkm is not None: if bkm is not None:
if barbican_endpoint is not None: if barbican_endpoint is not None:

View File

@ -20,7 +20,6 @@ import calendar
from barbicanclient import exceptions as barbican_exceptions from barbicanclient import exceptions as barbican_exceptions
import mock import mock
from oslo_config import cfg
from oslo_utils import timeutils from oslo_utils import timeutils
from castellan.common import exception from castellan.common import exception
@ -32,7 +31,7 @@ from castellan.tests.unit.key_manager import test_key_manager
class BarbicanKeyManagerTestCase(test_key_manager.KeyManagerTestCase): class BarbicanKeyManagerTestCase(test_key_manager.KeyManagerTestCase):
def _create_key_manager(self): def _create_key_manager(self):
return barbican_key_manager.BarbicanKeyManager(cfg.CONF) return barbican_key_manager.BarbicanKeyManager(self.conf)
def setUp(self): def setUp(self):
super(BarbicanKeyManagerTestCase, self).setUp() super(BarbicanKeyManagerTestCase, self).setUp()

View File

@ -17,8 +17,15 @@
Test cases for the key manager. Test cases for the key manager.
""" """
from oslo_config import cfg
from oslo_config import fixture
from castellan import key_manager
from castellan.key_manager import barbican_key_manager
from castellan.tests import base from castellan.tests import base
CONF = cfg.CONF
class KeyManagerTestCase(base.TestCase): class KeyManagerTestCase(base.TestCase):
@ -28,4 +35,18 @@ class KeyManagerTestCase(base.TestCase):
def setUp(self): def setUp(self):
super(KeyManagerTestCase, self).setUp() super(KeyManagerTestCase, self).setUp()
self.conf = self.useFixture(fixture.Config()).conf
self.key_mgr = self._create_key_manager() self.key_mgr = self._create_key_manager()
class DefaultKeyManagerImplTestCase(KeyManagerTestCase):
def _create_key_manager(self):
return key_manager.API(self.conf)
def test_default_key_manager(self):
self.assertEqual("barbican", self.conf.key_manager.backend)
self.assertIsNotNone(self.key_mgr)
self.assertIsInstance(self.key_mgr,
barbican_key_manager.BarbicanKeyManager)

View File

@ -15,9 +15,11 @@
from oslo_config import cfg from oslo_config import cfg
from castellan import key_manager
from castellan.key_manager import barbican_key_manager as bkm from castellan.key_manager import barbican_key_manager as bkm
from castellan import options from castellan import options
from castellan.tests import base from castellan.tests import base
from castellan.tests.unit.key_manager import mock_key_manager
class TestOptions(base.TestCase): class TestOptions(base.TestCase):
@ -25,9 +27,15 @@ class TestOptions(base.TestCase):
def test_set_defaults(self): def test_set_defaults(self):
conf = cfg.ConfigOpts() conf = cfg.ConfigOpts()
api_class = 'test.api.class' self.assertTrue(isinstance(key_manager.API(conf),
options.set_defaults(conf, api_class=api_class) bkm.BarbicanKeyManager))
self.assertEqual(api_class, conf.key_manager.api_class)
cls = mock_key_manager.MockKeyManager
backend = '%s.%s' % (cls.__module__, cls.__name__)
options.set_defaults(conf, backend=backend)
self.assertEqual(backend, conf.key_manager.backend)
self.assertIsInstance(key_manager.API(conf),
mock_key_manager.MockKeyManager)
barbican_endpoint = 'http://test-server.org:9311/' barbican_endpoint = 'http://test-server.org:9311/'
options.set_defaults(conf, barbican_endpoint=barbican_endpoint) options.set_defaults(conf, barbican_endpoint=barbican_endpoint)

View File

@ -11,4 +11,5 @@ oslo.context>=2.14.0 # Apache-2.0
oslo.i18n!=3.15.2,>=2.1.0 # Apache-2.0 oslo.i18n!=3.15.2,>=2.1.0 # Apache-2.0
oslo.log>=3.22.0 # Apache-2.0 oslo.log>=3.22.0 # Apache-2.0
oslo.utils>=3.20.0 # Apache-2.0 oslo.utils>=3.20.0 # Apache-2.0
stevedore>=1.20.0 # Apache-2.0
keystoneauth1>=3.1.0 # Apache-2.0 keystoneauth1>=3.1.0 # Apache-2.0

View File

@ -27,6 +27,9 @@ oslo.config.opts =
castellan.tests.functional.config = castellan.tests.functional.config:list_opts castellan.tests.functional.config = castellan.tests.functional.config:list_opts
castellan.config = castellan.options:list_opts castellan.config = castellan.options:list_opts
castellan.drivers =
barbican = castellan.key_manager.barbican_key_manager:BarbicanKeyManager
[build_sphinx] [build_sphinx]
source-dir = doc/source source-dir = doc/source
build-dir = doc/build build-dir = doc/build