Refactor secret_store for consistency
Mostly re-arranged the code a bit, so that for...else... Statements are used in a more consistent way. On the other, the exception SecretStorePluginsNotConfigured was introduced to more properly reflect the occations where the plugins have not been configured correctly. Change-Id: I84693a65c6e6b44cb81973590ff8c62f28c0b068
This commit is contained in:
@@ -46,7 +46,13 @@ CONF.register_opts(store_opts, group=store_opt_group)
|
||||
|
||||
class SecretStorePluginNotFound(exception.BarbicanException):
|
||||
"""Raised when no plugins are installed."""
|
||||
message = u._("Secret store plugin not found.")
|
||||
def __init__(self, plugin_name=None):
|
||||
if plugin_name:
|
||||
message = u._("Secret store plugin \"{0}\""
|
||||
" not found.").format(plugin_name)
|
||||
else:
|
||||
message = u._("Secret store plugin not found.")
|
||||
super(SecretStorePluginNotFound, self).__init__(message)
|
||||
|
||||
|
||||
class SecretStoreSupportedPluginNotFound(exception.BarbicanException):
|
||||
@@ -137,6 +143,14 @@ class SecretAlgorithmNotSupportedException(exception.BarbicanException):
|
||||
self.algorithm = algorithm
|
||||
|
||||
|
||||
class SecretStorePluginsNotConfigured(exception.BarbicanException):
|
||||
"""Raised when there are no secret store plugins configured."""
|
||||
def __init__(self):
|
||||
super(SecretStorePluginsNotConfigured, self).__init__(
|
||||
u._('No secret store plugins have been configured')
|
||||
)
|
||||
|
||||
|
||||
class SecretType(object):
|
||||
|
||||
"""Constant to define the symmetric key type. Used by getSecret to retrieve
|
||||
@@ -396,6 +410,14 @@ class SecretStoreBase(object):
|
||||
return False
|
||||
|
||||
|
||||
def _enforce_extensions_configured(plugin_related_function):
|
||||
def _check_plugins_configured(self, *args, **kwargs):
|
||||
if len(self.extensions) < 1:
|
||||
raise SecretStorePluginsNotConfigured()
|
||||
return plugin_related_function(self, *args, **kwargs)
|
||||
return _check_plugins_configured
|
||||
|
||||
|
||||
class SecretStorePluginManager(named.NamedExtensionManager):
|
||||
def __init__(self, conf=CONF, invoke_on_load=True,
|
||||
invoke_args=(), invoke_kwargs={}):
|
||||
@@ -407,6 +429,7 @@ class SecretStorePluginManager(named.NamedExtensionManager):
|
||||
invoke_kwds=invoke_kwargs
|
||||
)
|
||||
|
||||
@_enforce_extensions_configured
|
||||
def get_plugin_store(self, key_spec, plugin_name=None,
|
||||
transport_key_needed=False):
|
||||
"""Gets a secret store plugin.
|
||||
@@ -417,14 +440,11 @@ class SecretStorePluginManager(named.NamedExtensionManager):
|
||||
:returns: SecretStoreBase plugin implementation
|
||||
"""
|
||||
|
||||
if len(self.extensions) < 1:
|
||||
raise SecretStorePluginNotFound()
|
||||
|
||||
if plugin_name is not None:
|
||||
for ext in self.extensions:
|
||||
if utils.generate_fullname_for(ext.obj) == plugin_name:
|
||||
return ext.obj
|
||||
raise SecretStoreSupportedPluginNotFound()
|
||||
raise SecretStorePluginNotFound(plugin_name)
|
||||
|
||||
if not transport_key_needed:
|
||||
for ext in self.extensions:
|
||||
@@ -439,24 +459,19 @@ class SecretStorePluginManager(named.NamedExtensionManager):
|
||||
|
||||
raise SecretStoreSupportedPluginNotFound()
|
||||
|
||||
@_enforce_extensions_configured
|
||||
def get_plugin_retrieve_delete(self, plugin_name):
|
||||
"""Gets a secret retrieve/delete plugin.
|
||||
|
||||
:returns: SecretStoreBase plugin implementation
|
||||
"""
|
||||
|
||||
if len(self.extensions) < 1:
|
||||
raise SecretStorePluginNotFound()
|
||||
|
||||
for ext in self.extensions:
|
||||
if utils.generate_fullname_for(ext.obj) == plugin_name:
|
||||
retrieve_delete_plugin = ext.obj
|
||||
break
|
||||
else:
|
||||
raise SecretStoreSupportedPluginNotFound()
|
||||
|
||||
return retrieve_delete_plugin
|
||||
return ext.obj
|
||||
raise SecretStorePluginNotFound(plugin_name)
|
||||
|
||||
@_enforce_extensions_configured
|
||||
def get_plugin_generate(self, key_spec):
|
||||
"""Gets a secret generate plugin.
|
||||
|
||||
@@ -465,14 +480,7 @@ class SecretStorePluginManager(named.NamedExtensionManager):
|
||||
:returns: SecretStoreBase plugin implementation
|
||||
"""
|
||||
|
||||
if len(self.extensions) < 1:
|
||||
raise SecretStorePluginNotFound()
|
||||
|
||||
for ext in self.extensions:
|
||||
if ext.obj.generate_supports(key_spec):
|
||||
generate_plugin = ext.obj
|
||||
break
|
||||
else:
|
||||
raise SecretStoreSupportedPluginNotFound()
|
||||
|
||||
return generate_plugin
|
||||
return ext.obj
|
||||
raise SecretStoreSupportedPluginNotFound()
|
||||
|
||||
@@ -114,7 +114,7 @@ class WhenTestingSecretStorePluginManager(testtools.TestCase):
|
||||
self.manager.extensions = []
|
||||
keySpec = str.KeySpec(str.KeyAlgorithm.AES, 128)
|
||||
self.assertRaises(
|
||||
str.SecretStorePluginNotFound,
|
||||
str.SecretStorePluginsNotConfigured,
|
||||
self.manager.get_plugin_store,
|
||||
keySpec,
|
||||
)
|
||||
@@ -123,7 +123,7 @@ class WhenTestingSecretStorePluginManager(testtools.TestCase):
|
||||
self.manager.extensions = []
|
||||
keySpec = str.KeySpec(str.KeyAlgorithm.AES, 128)
|
||||
self.assertRaises(
|
||||
str.SecretStorePluginNotFound,
|
||||
str.SecretStorePluginsNotConfigured,
|
||||
self.manager.get_plugin_generate,
|
||||
keySpec,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user