Package list python-castellan / 8980bf7
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 Davanum Srinivas 4 years ago
7 changed file(s) with 67 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
1212 # License for the specific language governing permissions and limitations
1313 # under the License.
1414 from oslo_config import cfg
15 from oslo_log import log as logging
1516 from oslo_utils import importutils
17 from stevedore import driver
18 from stevedore import exception
19
20 LOG = logging.getLogger(__name__)
1621
1722 key_manager_opts = [
18 cfg.StrOpt('api_class',
19 default='castellan.key_manager.barbican_key_manager'
20 '.BarbicanKeyManager',
21 help='The full class name of the key manager API class'),
23 cfg.StrOpt('backend',
24 default='barbican',
25 deprecated_name='api_class',
26 deprecated_group='key_manager',
27 help='Specify the key manager implementation. Default is '
28 '"barbican".Will support the values earlier set using '
29 '[key_manager]/api_class for some time.'),
2230 ]
2331
2432
2634 conf = configuration or cfg.CONF
2735 conf.register_opts(key_manager_opts, group='key_manager')
2836
29 cls = importutils.import_class(conf.key_manager.api_class)
30 return cls(configuration=conf)
37 try:
38 mgr = driver.DriverManager("castellan.drivers",
39 conf.key_manager.backend,
40 invoke_on_load=True,
41 invoke_args=[conf])
42 return mgr.driver
43 except exception.NoMatches:
44 LOG.warning("Deprecation Warning : %s is not a stevedore based driver,"
45 " trying to load it as a class", conf.key_manager.backend)
46 cls = importutils.import_class(conf.key_manager.backend)
47 return cls(configuration=conf)
2929 '%(message)s')
3030
3131
32 def set_defaults(conf, api_class=None, barbican_endpoint=None,
32 def set_defaults(conf, backend=None, barbican_endpoint=None,
3333 barbican_api_version=None, auth_endpoint=None,
34 retry_delay=None, number_of_retries=None, verify_ssl=None):
34 retry_delay=None, number_of_retries=None, verify_ssl=None,
35 api_class=None):
3536 """Set defaults for configuration values.
3637
3738 Overrides the default options values.
4849 if bkm:
4950 conf.register_opts(bkm.barbican_opts, group=bkm.BARBICAN_OPT_GROUP)
5051
51 if api_class is not None:
52 conf.set_default('api_class', api_class, group='key_manager')
52 # Use the new backend option if set or fall back to the older api_class
53 default_backend = backend or api_class
54 if default_backend is not None:
55 conf.set_default('backend', default_backend, group='key_manager')
5356
5457 if bkm is not None:
5558 if barbican_endpoint is not None:
1919
2020 from barbicanclient import exceptions as barbican_exceptions
2121 import mock
22 from oslo_config import cfg
2322 from oslo_utils import timeutils
2423
2524 from castellan.common import exception
3130 class BarbicanKeyManagerTestCase(test_key_manager.KeyManagerTestCase):
3231
3332 def _create_key_manager(self):
34 return barbican_key_manager.BarbicanKeyManager(cfg.CONF)
33 return barbican_key_manager.BarbicanKeyManager(self.conf)
3534
3635 def setUp(self):
3736 super(BarbicanKeyManagerTestCase, self).setUp()
1616 Test cases for the key manager.
1717 """
1818
19 from oslo_config import cfg
20 from oslo_config import fixture
21
22 from castellan import key_manager
23 from castellan.key_manager import barbican_key_manager
1924 from castellan.tests import base
25
26 CONF = cfg.CONF
2027
2128
2229 class KeyManagerTestCase(base.TestCase):
2734 def setUp(self):
2835 super(KeyManagerTestCase, self).setUp()
2936
37 self.conf = self.useFixture(fixture.Config()).conf
38
3039 self.key_mgr = self._create_key_manager()
40
41
42 class DefaultKeyManagerImplTestCase(KeyManagerTestCase):
43
44 def _create_key_manager(self):
45 return key_manager.API(self.conf)
46
47 def test_default_key_manager(self):
48 self.assertEqual("barbican", self.conf.key_manager.backend)
49 self.assertIsNotNone(self.key_mgr)
50 self.assertIsInstance(self.key_mgr,
51 barbican_key_manager.BarbicanKeyManager)
1414
1515 from oslo_config import cfg
1616
17 from castellan import key_manager
1718 from castellan.key_manager import barbican_key_manager as bkm
1819 from castellan import options
1920 from castellan.tests import base
21 from castellan.tests.unit.key_manager import mock_key_manager
2022
2123
2224 class TestOptions(base.TestCase):
2426 def test_set_defaults(self):
2527 conf = cfg.ConfigOpts()
2628
27 api_class = 'test.api.class'
28 options.set_defaults(conf, api_class=api_class)
29 self.assertEqual(api_class, conf.key_manager.api_class)
29 self.assertTrue(isinstance(key_manager.API(conf),
30 bkm.BarbicanKeyManager))
31
32 cls = mock_key_manager.MockKeyManager
33 backend = '%s.%s' % (cls.__module__, cls.__name__)
34 options.set_defaults(conf, backend=backend)
35 self.assertEqual(backend, conf.key_manager.backend)
36 self.assertIsInstance(key_manager.API(conf),
37 mock_key_manager.MockKeyManager)
3038
3139 barbican_endpoint = 'http://test-server.org:9311/'
3240 options.set_defaults(conf, barbican_endpoint=barbican_endpoint)
1010 oslo.i18n!=3.15.2,>=2.1.0 # Apache-2.0
1111 oslo.log>=3.22.0 # Apache-2.0
1212 oslo.utils>=3.20.0 # Apache-2.0
13 stevedore>=1.20.0 # Apache-2.0
1314 keystoneauth1>=3.1.0 # Apache-2.0
2626 castellan.tests.functional.config = castellan.tests.functional.config:list_opts
2727 castellan.config = castellan.options:list_opts
2828
29 castellan.drivers =
30 barbican = castellan.key_manager.barbican_key_manager:BarbicanKeyManager
31
2932 [build_sphinx]
3033 source-dir = doc/source
3134 build-dir = doc/build