Use from_dict to load context params

The context objects accepts and ignores unknown keyword arguments. This
was to allow it to handle the deserialization of parameters it didn't
understand from the base class' to_dict method. This will make it
difficult to pass unknown attributes to the base class so fix the
from_dict method to only accept params it knows about.

Related-Bug: #1602081
Change-Id: Ic58a2025680e8e1ba4f8a177d898be457e2c3160
This commit is contained in:
Jamie Lennox 2016-07-11 12:06:41 +10:00
parent 95472a8969
commit 8ad224d4e6
4 changed files with 50 additions and 44 deletions

View File

@ -229,13 +229,7 @@ class RequestContextSerializer(om_serializer.Serializer):
trace_info = rpc_ctxt_dict.pop("trace_info", None)
if trace_info:
profiler.init(**trace_info)
user_id = rpc_ctxt_dict.pop('user_id', None)
if not user_id:
user_id = rpc_ctxt_dict.pop('user', None)
tenant_id = rpc_ctxt_dict.pop('tenant_id', None)
if not tenant_id:
tenant_id = rpc_ctxt_dict.pop('project_id', None)
return context.Context(user_id, tenant_id, **rpc_ctxt_dict)
return context.Context.from_dict(rpc_ctxt_dict)
@profiler.trace_cls("rpc")

View File

@ -35,14 +35,11 @@ class ContextBase(oslo_context.RequestContext):
def __init__(self, user_id, tenant_id, is_admin=None, roles=None,
timestamp=None, request_id=None, tenant_name=None,
user_name=None, overwrite=True, auth_token=None,
is_advsvc=None, **kwargs):
is_advsvc=None):
"""Object initialization.
:param overwrite: Set to False to ensure that the greenthread local
copy of the index is not overwritten.
:param kwargs: Extra arguments that might be present, but we ignore
because they possibly came in from older rpc messages.
"""
super(ContextBase, self).__init__(auth_token=auth_token,
user=user_id, tenant=tenant_id,
@ -97,7 +94,15 @@ class ContextBase(oslo_context.RequestContext):
@classmethod
def from_dict(cls, values):
return cls(**values)
return cls(user_id=values.get('user_id', values.get('user')),
tenant_id=values.get('tenant_id', values.get('project_id')),
is_admin=values.get('is_admin'),
roles=values.get('roles'),
timestamp=values.get('timestamp'),
request_id=values.get('request_id'),
tenant_name=values.get('tenant_name'),
user_name=values.get('user_name'),
auth_token=values.get('auth_token'))
def elevated(self):
"""Return a version of this context with admin flag set."""

View File

@ -93,7 +93,7 @@ class RequestTestCase(base.BaseTestCase):
def test_request_context_elevated(self):
user_context = context.Context(
'fake_user', 'fake_project', admin=False)
'fake_user', 'fake_project', is_admin=False)
self.assertFalse(user_context.is_admin)
admin_context = user_context.elevated()
self.assertFalse(user_context.is_admin)

View File

@ -23,7 +23,6 @@ from oslo_messaging import conffixture as messaging_conffixture
import testtools
from neutron.common import rpc
from neutron import context
from neutron.tests import base
@ -234,43 +233,51 @@ class TestRequestContextSerializer(base.DietTestCase):
context.to_dict.assert_called_once_with()
@mock.patch.object(context, 'Context')
def test_deserialize_context(self, mock_con):
context = mock.Mock()
context.copy.return_value = {'foo': 'bar',
'user_id': 1,
'tenant_id': 1}
@mock.patch('neutron.policy.check_is_advsvc', return_val=False)
@mock.patch('neutron.policy.check_is_admin', return_val=False)
def test_deserialize_context(self, m, n):
context_dict = {'foo': 'bar',
'user_id': 1,
'tenant_id': 1}
self.ser.deserialize_context(context)
mock_con.assert_called_once_with(1, 1, foo='bar')
c = self.ser.deserialize_context(context_dict)
@mock.patch.object(context, 'Context')
def test_deserialize_context_no_user_id(self, mock_con):
context = mock.Mock()
context.copy.return_value = {'foo': 'bar',
'user': 1,
'tenant_id': 1}
self.assertEqual(1, c.user_id)
self.assertEqual(1, c.project_id)
self.ser.deserialize_context(context)
mock_con.assert_called_once_with(1, 1, foo='bar')
@mock.patch('neutron.policy.check_is_advsvc', return_val=False)
@mock.patch('neutron.policy.check_is_admin', return_val=False)
def test_deserialize_context_no_user_id(self, m, n):
context_dict = {'foo': 'bar',
'user': 1,
'tenant_id': 1}
@mock.patch.object(context, 'Context')
def test_deserialize_context_no_tenant_id(self, mock_con):
context = mock.Mock()
context.copy.return_value = {'foo': 'bar',
'user_id': 1,
'project_id': 1}
c = self.ser.deserialize_context(context_dict)
self.ser.deserialize_context(context)
mock_con.assert_called_once_with(1, 1, foo='bar')
self.assertEqual(1, c.user_id)
self.assertEqual(1, c.project_id)
@mock.patch.object(context, 'Context')
def test_deserialize_context_no_ids(self, mock_con):
context = mock.Mock()
context.copy.return_value = {'foo': 'bar'}
@mock.patch('neutron.policy.check_is_advsvc', return_val=False)
@mock.patch('neutron.policy.check_is_admin', return_val=False)
def test_deserialize_context_no_tenant_id(self, m, n):
context_dict = {'foo': 'bar',
'user_id': 1,
'project_id': 1}
self.ser.deserialize_context(context)
mock_con.assert_called_once_with(None, None, foo='bar')
c = self.ser.deserialize_context(context_dict)
self.assertEqual(1, c.user_id)
self.assertEqual(1, c.project_id)
@mock.patch('neutron.policy.check_is_advsvc', return_val=False)
@mock.patch('neutron.policy.check_is_admin', return_val=False)
def test_deserialize_context_no_ids(self, m, n):
context_dict = {'foo': 'bar'}
c = self.ser.deserialize_context(context_dict)
self.assertIsNone(c.user_id)
self.assertIsNone(c.project_id)
class ServiceTestCase(base.DietTestCase):