From 765b6c98e08577038d33c9a5d745bb75c6b9f06d Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Fri, 21 Feb 2014 16:18:16 +0100 Subject: [PATCH] Fix logging context to include user_identity The user_identity generated created from user, tenant, domain user_domain and project_domain. The new domain related values are default to None Closes-Bug: #1283080 Change-Id: I5e43142afba3492ecf05b65ba24ee70f158f88de --- glance/context.py | 17 ++++++++++++++++- glance/tests/unit/test_context.py | 9 +++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/glance/context.py b/glance/context.py index 89ca3c1818..356bad1576 100644 --- a/glance/context.py +++ b/glance/context.py @@ -25,10 +25,13 @@ class RequestContext(object): accesses the system, as well as additional request information. """ + user_idt_format = '{user} {tenant} {domain} {user_domain} {p_domain}' + def __init__(self, auth_tok=None, user=None, tenant=None, roles=None, is_admin=False, read_only=False, show_deleted=False, owner_is_tenant=True, service_catalog=None, - policy_enforcer=None): + policy_enforcer=None, domain=None, user_domain=None, + project_domain=None): self.auth_tok = auth_tok self.user = user self.tenant = tenant @@ -40,6 +43,9 @@ class RequestContext(object): self.service_catalog = service_catalog self.policy_enforcer = policy_enforcer or policy.Enforcer() self.is_admin = is_admin + self.domain = domain + self.user_domain = user_domain + self.project_domain = project_domain if not self.is_admin: self.is_admin = \ self.policy_enforcer.check_is_admin(self) @@ -50,6 +56,14 @@ class RequestContext(object): def to_dict(self): # NOTE(ameade): These keys are named to correspond with the default # format string for logging the context in openstack common + + user_idt = ( + self.user_idt_format.format(user=self.user or '-', + tenant=self.tenant or '-', + domain=self.domain or '-', + user_domain=self.user_domain or '-', + p_domain=self.project_domain or '-')) + return { 'request_id': self.request_id, @@ -67,6 +81,7 @@ class RequestContext(object): 'roles': self.roles, 'auth_token': self.auth_tok, 'service_catalog': self.service_catalog, + 'user_identity': user_idt } @classmethod diff --git a/glance/tests/unit/test_context.py b/glance/tests/unit/test_context.py index 6bc02dd08c..825313a4fb 100644 --- a/glance/tests/unit/test_context.py +++ b/glance/tests/unit/test_context.py @@ -167,3 +167,12 @@ class TestContext(utils.BaseTestCase): ctx = context.RequestContext() self.assertTrue(hasattr(local.store, 'context')) self.assertEqual(ctx, local.store.context) + + def test_user_identity(self): + ctx = context.RequestContext(user="user", + tenant="tenant", + domain="domain", + user_domain="user-domain", + project_domain="project-domain") + self.assertEqual('user tenant domain user-domain project-domain', + ctx.to_dict()["user_identity"])