From 617b772e556a9cf874031a9068a9e31156242aa0 Mon Sep 17 00:00:00 2001 From: Dane LeBlanc Date: Thu, 11 Jun 2015 16:01:22 -0400 Subject: [PATCH] Modify magnum api context to use user_name and project_name Problem description: If DevStack is used to instantiate the magnum plugin, and the devstack localrc/local.conf has the default values for: LOG_COLOR (default value = True) SYSLOG (default value = False) then upon startup (i.e. running DevStack's stack.sh), the magnum devstack lib calls the DevStack common setup_colorized_logging function, but without passing the optional 'project_var' and 'user_var' arguments to this function. As a result, the setup_colorized_logging function uses its default values of "user_name" and "project_name" when it defines the logging_context_format_string (which in turn gets configured in /etc/magnum/magnum.conf). The problem is that "user_name" and "project_name" are not defined in the API context used by Magnum, so that whenever the magnum plugin does a logging call, a KeyError exception for the non-existant key "user_name" is generated. Fix description: The fix is to modify the Magnum context to use "user_name" and "project_name" attributes to be consistent with the default context format string set up by DevStack. Change-Id: Ia0c34899609735ff9d8b4597101e004e2684657e Closes-Bug: #1464376 --- devstack/lib/magnum | 2 +- magnum/api/hooks.py | 4 ++-- magnum/common/context.py | 19 +++++++++++-------- magnum/tests/unit/common/test_context.py | 12 ++++++------ 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/devstack/lib/magnum b/devstack/lib/magnum index 0674f31a27..a1a701daea 100644 --- a/devstack/lib/magnum +++ b/devstack/lib/magnum @@ -151,7 +151,7 @@ function create_magnum_conf { setup_colorized_logging $MAGNUM_CONF DEFAULT else # Show user_name and project_name instead of user_id and project_id - iniset $MAGNUM_CONF DEFAULT logging_context_format_string "%(asctime)s.%(msecs)03d %(levelname)s %(name)s [%(request_id)s %(user)s %(project)s] %(instance)s%(message)s" + iniset $MAGNUM_CONF DEFAULT logging_context_format_string "%(asctime)s.%(msecs)03d %(levelname)s %(name)s [%(request_id)s %(user_name)s %(project_name)s] %(instance)s%(message)s" fi # Register SSL certificates if provided diff --git a/magnum/api/hooks.py b/magnum/api/hooks.py index 65081f1f4b..ba9632d8c8 100644 --- a/magnum/api/hooks.py +++ b/magnum/api/hooks.py @@ -66,9 +66,9 @@ class ContextHook(hooks.PecanHook): auth_token=auth_token, auth_url=auth_url, auth_token_info=auth_token_info, - user=user, + user_name=user, user_id=user_id, - project=project, + project_name=project, project_id=project_id, domain_id=domain_id, domain_name=domain_name) diff --git a/magnum/common/context.py b/magnum/common/context.py index be0e55ed9e..9d534baeae 100644 --- a/magnum/common/context.py +++ b/magnum/common/context.py @@ -17,10 +17,11 @@ class RequestContext(context.RequestContext): """Extends security contexts from the OpenStack common library.""" def __init__(self, auth_token=None, auth_url=None, domain_id=None, - domain_name=None, user=None, user_id=None, project=None, - project_id=None, is_admin=False, is_public_api=False, - read_only=False, show_deleted=False, request_id=None, - trust_id=None, auth_token_info=None, **kwargs): + domain_name=None, user_name=None, user_id=None, + project_name=None, project_id=None, is_admin=False, + is_public_api=False, read_only=False, show_deleted=False, + request_id=None, trust_id=None, auth_token_info=None, + **kwargs): """Stores several additional request parameters: :param domain_id: The ID of the domain. @@ -30,8 +31,9 @@ class RequestContext(context.RequestContext): """ self.is_public_api = is_public_api + self.user_name = user_name self.user_id = user_id - self.project = project + self.project_name = project_name self.project_id = project_id self.domain_id = domain_id self.domain_name = domain_name @@ -40,7 +42,8 @@ class RequestContext(context.RequestContext): self.trust_id = trust_id super(RequestContext, self).__init__(auth_token=auth_token, - user=user, tenant=project, + user=user_name, + tenant=project_name, is_admin=is_admin, read_only=read_only, show_deleted=show_deleted, @@ -52,9 +55,9 @@ class RequestContext(context.RequestContext): 'auth_url': self.auth_url, 'domain_id': self.domain_id, 'domain_name': self.domain_name, - 'user': self.user, + 'user_name': self.user_name, 'user_id': self.user_id, - 'project': self.project, + 'project_name': self.project_name, 'project_id': self.project_id, 'is_admin': self.is_admin, 'is_public_api': self.is_public_api, diff --git a/magnum/tests/unit/common/test_context.py b/magnum/tests/unit/common/test_context.py index 3e9028123d..52813c6e12 100644 --- a/magnum/tests/unit/common/test_context.py +++ b/magnum/tests/unit/common/test_context.py @@ -23,9 +23,9 @@ class ContextTestCase(base.TestCase): auth_url='auth_url1', domain_id='domain_id1', domain_name='domain_name1', - user='user1', + user_name='user1', user_id='user-id1', - project='tenant1', + project_name='tenant1', project_id='tenant-id1', is_admin=True, is_public_api=True, @@ -42,9 +42,9 @@ class ContextTestCase(base.TestCase): self.assertEqual("auth_url1", ctx.auth_url) self.assertEqual("domain_id1", ctx.domain_id) self.assertEqual("domain_name1", ctx.domain_name) - self.assertEqual("user1", ctx.user) + self.assertEqual("user1", ctx.user_name) self.assertEqual("user-id1", ctx.user_id) - self.assertEqual("tenant1", ctx.project) + self.assertEqual("tenant1", ctx.project_name) self.assertEqual("tenant-id1", ctx.project_id) self.assertTrue(ctx.is_admin) self.assertTrue(ctx.is_public_api) @@ -62,10 +62,10 @@ class ContextTestCase(base.TestCase): self.assertEqual(ctx.auth_url, ctx2.auth_url) self.assertEqual(ctx.domain_id, ctx2.domain_id) self.assertEqual(ctx.domain_name, ctx2.domain_name) - self.assertEqual(ctx.user, ctx2.user) + self.assertEqual(ctx.user_name, ctx2.user_name) self.assertEqual(ctx.user_id, ctx2.user_id) self.assertEqual(ctx.tenant, ctx2.tenant) - self.assertEqual(ctx.project, ctx2.project) + self.assertEqual(ctx.project_name, ctx2.project_name) self.assertEqual(ctx.project_id, ctx2.project_id) self.assertEqual(ctx.is_admin, ctx2.is_admin) self.assertEqual(ctx.is_public_api, ctx2.is_public_api)