Fix circular dependency of certificate_manager module

The certificate_manager module had a circular dependency that made it
problematic to use for certain versions of the python interpreter.
This was because we were initializing a singleton EVENT_PLUGIN_MANAGER
that in turn loaded the plugins; The problem was that the plugins
imported certificate_manger themselves and this caused the circular
dependency. So instead of creating that class in the module, I
created a function that should be called instead.

Closes-Bug: #1570356
Change-Id: I9cdb7c3ecc9c275ee561363f6ce13248259f88d0
This commit is contained in:
Juan Antonio Osorio Robles 2016-04-14 06:31:26 +00:00
parent 2b724d65bb
commit 4c6704eda7
4 changed files with 17 additions and 8 deletions

View File

@ -105,6 +105,10 @@ INFO_INTERMEDIATES = "intermediates"
INFO_EXPIRATION = "expiration"
# Singleton to avoid loading the CertificateEventManager plugins more than once
_EVENT_PLUGIN_MANAGER = None
class CertificateRequestType(object):
"""Constants to define the certificate request type."""
CUSTOM_REQUEST = "custom"
@ -702,8 +706,8 @@ class _CertificateEventPluginManager(named.NamedExtensionManager,
Each time this class is initialized it will load a new instance
of each enabled plugin. This is undesirable, so rather than initializing a
new instance of this class use the EVENT_PLUGIN_MANAGER at the module
level.
new instance of this class use the get_event_plugin_manager function
at the module level.
"""
def __init__(self, conf=CONF, invoke_args=(), invoke_kwargs={}):
super(_CertificateEventPluginManager, self).__init__(
@ -750,4 +754,9 @@ class _CertificateEventPluginManager(named.NamedExtensionManager,
getattr(plugin, method)(*args, **kwargs)
EVENT_PLUGIN_MANAGER = _CertificateEventPluginManager()
def get_event_plugin_manager():
global _EVENT_PLUGIN_MANAGER
if _EVENT_PLUGIN_MANAGER:
return _EVENT_PLUGIN_MANAGER
_EVENT_PLUGIN_MANAGER = _CertificateEventPluginManager()
return _EVENT_PLUGIN_MANAGER

View File

@ -516,7 +516,7 @@ def _get_container_from_order_meta(order_model, project_model):
def _notify_ca_unavailable(order_model, result):
"""Notify observer(s) that the CA was unavailable at this time."""
cert.EVENT_PLUGIN_MANAGER.notify_ca_is_unavailable(
cert.get_event_plugin_manager().notify_ca_is_unavailable(
order_model.project_id,
hrefs.convert_order_to_href(order_model.id),
result.status_message,

View File

@ -38,7 +38,7 @@ class WhenTestingCertificateEventPluginManager(testtools.TestCase):
self.plugin_name = common_utils.generate_fullname_for(
self.plugin_returned)
self.plugin_loaded = mock.MagicMock(obj=self.plugin_returned)
self.manager = cm.EVENT_PLUGIN_MANAGER
self.manager = cm.get_event_plugin_manager()
self.manager.extensions = [self.plugin_loaded]
def test_get_plugin_by_name(self):

View File

@ -395,7 +395,7 @@ class BaseCertificateRequestsTestCase(database_utils.RepositoryTestCase):
"""Mock the certificate event plugin manager."""
self.cert_event_plugin_patcher = mock.patch(
'barbican.plugin.interface.certificate_manager'
'.EVENT_PLUGIN_MANAGER'
'._EVENT_PLUGIN_MANAGER'
)
self.cert_event_plugin_patcher.start()
@ -690,7 +690,7 @@ class WhenIssuingCertificateRequests(BaseCertificateRequestsTestCase):
self._verify_issue_certificate_plugins_called()
epm = self.cert_event_plugin_patcher.target.EVENT_PLUGIN_MANAGER
epm = self.cert_event_plugin_patcher.target._EVENT_PLUGIN_MANAGER
epm.notify_ca_is_unavailable.assert_called_once_with(
self.project.id,
order_ref,
@ -775,7 +775,7 @@ class WhenCheckingCertificateRequests(BaseCertificateRequestsTestCase):
self._verify_check_certificate_plugins_called()
epm = self.cert_event_plugin_patcher.target.EVENT_PLUGIN_MANAGER
epm = self.cert_event_plugin_patcher.target._EVENT_PLUGIN_MANAGER
epm.notify_ca_is_unavailable.assert_called_once_with(
self.project.id,
order_ref,