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
# under the License.
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import importutils
from stevedore import driver
from stevedore import exception
LOG = logging.getLogger(__name__)
key_manager_opts = [
cfg.StrOpt('api_class',
default='castellan.key_manager.barbican_key_manager'
'.BarbicanKeyManager',
help='The full class name of the key manager API class'),
cfg.StrOpt('backend',
default='barbican',
deprecated_name='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.register_opts(key_manager_opts, group='key_manager')
cls = importutils.import_class(conf.key_manager.api_class)
return cls(configuration=conf)
try:
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')
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,
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.
Overrides the default options values.
@ -49,8 +50,10 @@ def set_defaults(conf, api_class=None, barbican_endpoint=None,
if bkm:
conf.register_opts(bkm.barbican_opts, group=bkm.BARBICAN_OPT_GROUP)
if api_class is not None:
conf.set_default('api_class', api_class, group='key_manager')
# Use the new backend option if set or fall back to the older api_class
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 barbican_endpoint is not None:

View File

@ -20,7 +20,6 @@ import calendar
from barbicanclient import exceptions as barbican_exceptions
import mock
from oslo_config import cfg
from oslo_utils import timeutils
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):
def _create_key_manager(self):
return barbican_key_manager.BarbicanKeyManager(cfg.CONF)
return barbican_key_manager.BarbicanKeyManager(self.conf)
def setUp(self):
super(BarbicanKeyManagerTestCase, self).setUp()

View File

@ -17,8 +17,15 @@
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
CONF = cfg.CONF
class KeyManagerTestCase(base.TestCase):
@ -28,4 +35,18 @@ class KeyManagerTestCase(base.TestCase):
def setUp(self):
super(KeyManagerTestCase, self).setUp()
self.conf = self.useFixture(fixture.Config()).conf
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 castellan import key_manager
from castellan.key_manager import barbican_key_manager as bkm
from castellan import options
from castellan.tests import base
from castellan.tests.unit.key_manager import mock_key_manager
class TestOptions(base.TestCase):
@ -25,9 +27,15 @@ class TestOptions(base.TestCase):
def test_set_defaults(self):
conf = cfg.ConfigOpts()
api_class = 'test.api.class'
options.set_defaults(conf, api_class=api_class)
self.assertEqual(api_class, conf.key_manager.api_class)
self.assertTrue(isinstance(key_manager.API(conf),
bkm.BarbicanKeyManager))
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/'
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.log>=3.22.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

View File

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