def get_plugin_store(self, key_spec, plugin_name=None, transport_key_needed=False, project_id=None): """Gets a secret store plugin. :param: plugin_name: set to plugin_name to get specific plugin :param: key_spec: KeySpec of key that will be stored :param: transport_key_needed: set to True if a transport key is required. :returns: SecretStoreBase plugin implementation """ active_plugins = multiple_backends.get_applicable_store_plugins( self, project_id=project_id, existing_plugin_name=plugin_name) if plugin_name is not None: for plugin in active_plugins: if utils.generate_fullname_for(plugin) == plugin_name: return plugin raise SecretStorePluginNotFound(plugin_name) if not transport_key_needed: for plugin in active_plugins: if plugin.store_secret_supports(key_spec): return plugin else: for plugin in active_plugins: if (plugin.get_transport_key() is not None and plugin.store_secret_supports(key_spec)): return plugin raise SecretStoreSupportedPluginNotFound()
def get_plugin_generate(self, key_spec, project_id=None): """Gets a secret generate plugin. :param key_spec: KeySpec that contains details on the type of key to generate :returns: SecretStoreBase plugin implementation """ active_plugins = multiple_backends.get_applicable_store_plugins( self, project_id=project_id, existing_plugin_name=None) for plugin in active_plugins: if plugin.generate_supports(key_spec): return plugin raise SecretStoreSupportedPluginNotFound()
def test_get_applicable_store_plugins_when_multiple_backend_not_enabled( self): ss_config = config.get_module_config('secretstore') ss_plugins = ['ss_p11', 'ss_p22', 'ss_p33', 'ss_p44'] ss_conf_plugins = ['ss_p1', 'ss_p2', 'ss_p3'] cr_conf_plugins = ['cr_p1', 'cr_p2', 'cr_p3'] self.init_via_conf_file(ss_conf_plugins, cr_conf_plugins, enabled=False) ss_manager = MockedManager(ss_plugins) ss_config.set_override("enabled_secretstore_plugins", ss_plugins, group='secretstore') objs = multiple_backends.get_applicable_store_plugins(ss_manager, None, None) self.assertEqual(4, len(objs))
def test_get_applicable_store_plugins_when_multiple_backend_not_enabled( self): ss_config = config.get_module_config('secretstore') ss_plugins = ['ss_p11', 'ss_p22', 'ss_p33', 'ss_p44'] ss_conf_plugins = ['ss_p1', 'ss_p2', 'ss_p3'] cr_conf_plugins = ['cr_p1', 'cr_p2', 'cr_p3'] self.init_via_conf_file(ss_conf_plugins, cr_conf_plugins, enabled=False) ss_manager = MockedManager(ss_plugins) ss_config.set_override("enabled_secretstore_plugins", ss_plugins, group='secretstore') objs = multiple_backends.get_applicable_store_plugins(ss_manager, None, None) self.assertEqual(4, len(objs))
def test_get_when_project_preferred_plugin_is_set(self): ss_plugins = ['ss_p1', 'ss_p2', 'ss_p3'] cr_plugins = ['cr_p1', 'cr_p2', 'cr_p3'] self.init_via_conf_file(ss_plugins, cr_plugins, enabled=True) ss_manager = MockedManager(ss_plugins) project_id = uuid.uuid4().hex with mock.patch('barbican.model.repositories.ProjectSecretStoreRepo.' 'get_secret_store_for_project') as pref_func: # set preferred secret store to one of value in config m_dict = {'store_plugin': 'ss_p3'} m_rec = mock.MagicMock() m_rec.secret_store.to_dict_fields.return_value = m_dict pref_func.return_value = m_rec objs = multiple_backends.get_applicable_store_plugins( ss_manager, project_id, None) self.assertIn(project_id, pref_func.call_args_list[0][0]) self.assertIsInstance(objs, list) self.assertEqual(1, len(objs)) self.assertIn('ss_p3', objs[0].get_plugin_name())
def test_get_when_project_preferred_plugin_is_set(self): ss_plugins = ['ss_p1', 'ss_p2', 'ss_p3'] cr_plugins = ['cr_p1', 'cr_p2', 'cr_p3'] self.init_via_conf_file(ss_plugins, cr_plugins, enabled=True) ss_manager = MockedManager(ss_plugins) project_id = uuidutils.generate_uuid(dashed=False) with mock.patch('barbican.model.repositories.ProjectSecretStoreRepo.' 'get_secret_store_for_project') as pref_func: # set preferred secret store to one of value in config m_dict = {'store_plugin': 'ss_p3'} m_rec = mock.MagicMock() m_rec.secret_store.to_dict_fields.return_value = m_dict pref_func.return_value = m_rec objs = multiple_backends.get_applicable_store_plugins( ss_manager, project_id, None) self.assertIn(project_id, pref_func.call_args_list[0][0]) self.assertIsInstance(objs, list) self.assertEqual(1, len(objs)) self.assertIn('ss_p3', objs[0].get_plugin_name())