From 63f83ced06b62e322bccab333802da918a32e8cc Mon Sep 17 00:00:00 2001 From: Jamie Lennox Date: Fri, 10 Jun 2016 11:54:26 +1000 Subject: [PATCH] Make audit middleware use common config object Audit middleware is starting to need the same sort of config handling that auth_token middleware uses. Make it use the common config object. The test that is removed is because the config project returns None not unknown. This is then used correctly, however the test as written is not useful. Change-Id: Ic99f6435e01661c2b13dd53d7f9f29357b4e244e --- keystonemiddleware/audit.py | 57 ++++++++----------- .../tests/unit/test_audit_middleware.py | 12 +--- 2 files changed, 26 insertions(+), 43 deletions(-) diff --git a/keystonemiddleware/audit.py b/keystonemiddleware/audit.py index a7247ee..7f636f7 100644 --- a/keystonemiddleware/audit.py +++ b/keystonemiddleware/audit.py @@ -21,6 +21,7 @@ provides. import ast import collections +import copy import functools import logging import os.path @@ -51,6 +52,7 @@ from six.moves import configparser from six.moves.urllib import parse as urlparse import webob.dec +from keystonemiddleware._common import config from keystonemiddleware.i18n import _LE, _LI, _LW @@ -350,37 +352,6 @@ class AuditMiddleware(object): http://docs.openstack.org/developer/keystonemiddleware/audit.html """ - def _conf_get(self, name, group=AUDIT_MIDDLEWARE_GROUP): - # try config from paste-deploy first - if name in self._conf: - return self._conf[name] - else: - return CONF[group][name] - - def _determine_project(self): - """Determine a project name from all available config sources. - - The sources are checked in the following order: - - 1. The paste-deploy config for audit middleware - 2. The audit_middleware_notifications in the project's config - 3. The oslo.config CONF.project property - - """ - try: - return self._conf_get('project') - except cfg.NoSuchOptError: - try: - # CONF.project will exist only if the service uses - # oslo.config. It will only be set when the project - # calls CONF(...) and when not set oslo.config oddly - # raises a NoSuchOptError exception. - return CONF.project - except cfg.NoSuchOptError: - # Unable to determine the project so set it to something - # that is obvious so operators can mitigate. - return taxonomy.UNKNOWN - @staticmethod def _get_aliases(proj): aliases = {} @@ -398,15 +369,19 @@ class AuditMiddleware(object): def __init__(self, app, **conf): self._application = app + self._conf = config.Config('audit', + AUDIT_MIDDLEWARE_GROUP, + _list_opts(), + conf) global _LOG _LOG = logging.getLogger(conf.get('log_name', __name__)) - self._conf = conf self._service_name = conf.get('service_name') self._ignore_req_list = [x.upper().strip() for x in conf.get('ignore_req_list', '').split(',')] self._cadf_audit = OpenStackAuditApi(conf.get('audit_map_file')) - transport_aliases = self._get_aliases(self._determine_project()) + project = self._conf.project or taxonomy.UNKNOWN + transport_aliases = self._get_aliases(project) if messaging: transport = oslo_messaging.get_transport( cfg.CONF, @@ -516,6 +491,22 @@ class AuditMiddleware(object): return response +def _list_opts(): + """Return a list of oslo_config options available in audit middleware. + + The returned list includes all oslo_config options which may be registered + at runtime by the project. + + Each element of the list is a tuple. The first element is the name of the + group under which the list of elements in the second element will be + registered. A group name of None corresponds to the [DEFAULT] group in + config files. + + :returns: a list of (group_name, opts) tuples + """ + return [(AUDIT_MIDDLEWARE_GROUP, copy.deepcopy(_AUDIT_OPTS))] + + def filter_factory(global_conf, **local_conf): """Return a WSGI filter app for use with paste.deploy.""" conf = global_conf.copy() diff --git a/keystonemiddleware/tests/unit/test_audit_middleware.py b/keystonemiddleware/tests/unit/test_audit_middleware.py index 110fb33..110c79d 100644 --- a/keystonemiddleware/tests/unit/test_audit_middleware.py +++ b/keystonemiddleware/tests/unit/test_audit_middleware.py @@ -262,22 +262,14 @@ class AuditMiddlewareTest(BaseAuditMiddlewareTest): def test_project_name_from_oslo_config(self): self.assertEqual(self.PROJECT_NAME, - self.middleware._determine_project()) + self.middleware._conf.project) def test_project_name_from_local_config(self): project_name = uuid.uuid4().hex self.middleware = audit.AuditMiddleware( FakeApp(), audit_map_file=self.audit_map, service_name='pycadf', project=project_name) - self.assertEqual(project_name, self.middleware._determine_project()) - - def test_project_undetermined(self): - self.middleware = audit.AuditMiddleware( - FakeApp(), audit_map_file=self.audit_map, - service_name='pycadf') - del cfg.CONF.project - self.assertEqual(taxonomy.UNKNOWN, - self.middleware._determine_project()) + self.assertEqual(project_name, self.middleware._conf.project) def _get_transport(conf, aliases=None, url=None):