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
This commit is contained in:
Jamie Lennox 2016-07-11 12:36:56 +10:00
parent 8ad224d4e6
commit 11257a31c1
2 changed files with 13 additions and 21 deletions

View File

@ -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

View File

@ -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