From 11257a31c1a5d4cf309515f8c9523990386f5ab5 Mon Sep 17 00:00:00 2001 From: Jamie Lennox Date: Mon, 11 Jul 2016 12:36:56 +1000 Subject: [PATCH] Use context from_environ to load contexts The from_environ method loads a context object with all the appropriate values that are retrieved from auth_token middleware and other oslo middlewares. This will let oslo add new things to policy enforcement in future without having to manually change all services. To do this we need to pass keyword arguments for the context through to the base class. Related-Bug: #1602081 Change-Id: I0533c8aee3893449b757f1e35fc89a451ae1720c --- neutron/context.py | 19 +++++++++---------- neutron/pecan_wsgi/hooks/context.py | 15 ++++----------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/neutron/context.py b/neutron/context.py index 22a5e451ad7..18e26ecb76c 100644 --- a/neutron/context.py +++ b/neutron/context.py @@ -32,21 +32,20 @@ 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): + def __init__(self, user_id=None, tenant_id=None, is_admin=None, + timestamp=None, tenant_name=None, user_name=None, + is_advsvc=None, **kwargs): """Object initialization. :param overwrite: Set to False to ensure that the greenthread local copy of the index is not overwritten. """ - super(ContextBase, self).__init__(auth_token=auth_token, - user=user_id, tenant=tenant_id, - is_admin=is_admin, - request_id=request_id, - overwrite=overwrite, - roles=roles) + # NOTE(jamielennox): We maintain these arguments in order for tests + # that pass arguments positionally. + kwargs.setdefault('user', user_id) + kwargs.setdefault('tenant', tenant_id) + super(ContextBase, self).__init__(is_admin=is_admin, **kwargs) + self.user_name = user_name self.tenant_name = tenant_name diff --git a/neutron/pecan_wsgi/hooks/context.py b/neutron/pecan_wsgi/hooks/context.py index 7c3f9a339d7..ae38f6393ad 100644 --- a/neutron/pecan_wsgi/hooks/context.py +++ b/neutron/pecan_wsgi/hooks/context.py @@ -39,22 +39,15 @@ class ContextHook(hooks.PecanHook): priority = 95 def before(self, state): - user_id = state.request.headers.get('X-User-Id') - user_id = state.request.headers.get('X-User', user_id) user_name = state.request.headers.get('X-User-Name', '') - tenant_id = state.request.headers.get('X-Project-Id') tenant_name = state.request.headers.get('X-Project-Name') - auth_token = state.request.headers.get('X-Auth-Token') - roles = state.request.headers.get('X-Roles', '').split(',') - roles = [r.strip() for r in roles] - creds = {'roles': roles} req_id = state.request.headers.get(request_id.ENV_REQUEST_ID) # TODO(kevinbenton): is_admin logic # Create a context with the authentication data - ctx = context.Context(user_id, tenant_id=tenant_id, - roles=creds['roles'], - user_name=user_name, tenant_name=tenant_name, - request_id=req_id, auth_token=auth_token) + ctx = context.Context.from_environ(state.request.environ, + user_name=user_name, + tenant_name=tenant_name, + request_id=req_id) # Inject the context... state.request.context['neutron_context'] = ctx