Use oslo.context class method to construct context object
oslo_context.Context.from_environ provides a more generic way to contruct a context object from request environment. We can support more new attributes supported in keystonemiddleware without changing our code. Partial-Bug: #1674349 Closes-Bug: #1621073 In the unit test, context.tenant_name is replaced to context.project_name as it will be the recommended way to access project name now. Note that equivalency of project_name and tenant_name will be ensured by a depending neutron-lib patch [1], so this change affects nobody. [1] https://review.openstack.org/#/c/448537/ Depends-On: Ieec57d9ea8d95e55499a17e2c04da5e3e78a1557 Change-Id: Ie48aa843ca8c852b1e93e760d2e3e8aaa38aed56
This commit is contained in:
parent
42d2fc2d8c
commit
75c34838ef
@ -16,7 +16,6 @@ from neutron_lib import context
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log as logging
|
||||
from oslo_middleware import base
|
||||
from oslo_middleware import request_id
|
||||
import webob.dec
|
||||
import webob.exc
|
||||
|
||||
@ -28,34 +27,12 @@ class NeutronKeystoneContext(base.ConfigurableMiddleware):
|
||||
|
||||
@webob.dec.wsgify
|
||||
def __call__(self, req):
|
||||
# Determine the user ID
|
||||
user_id = req.headers.get('X_USER_ID')
|
||||
if not user_id:
|
||||
ctx = context.Context.from_environ(req.environ)
|
||||
|
||||
if not ctx.user_id:
|
||||
LOG.debug("X_USER_ID is not found in request")
|
||||
return webob.exc.HTTPUnauthorized()
|
||||
|
||||
# Determine the tenant
|
||||
tenant_id = req.headers.get('X_PROJECT_ID')
|
||||
|
||||
# Suck out the roles
|
||||
roles = [r.strip() for r in req.headers.get('X_ROLES', '').split(',')]
|
||||
|
||||
# Human-friendly names
|
||||
tenant_name = req.headers.get('X_PROJECT_NAME')
|
||||
user_name = req.headers.get('X_USER_NAME')
|
||||
|
||||
# Use request_id if already set
|
||||
req_id = req.environ.get(request_id.ENV_REQUEST_ID)
|
||||
|
||||
# Get the auth token
|
||||
auth_token = req.headers.get('X_AUTH_TOKEN',
|
||||
req.headers.get('X_STORAGE_TOKEN'))
|
||||
|
||||
# Create a context with the authentication data
|
||||
ctx = context.Context(user_id, tenant_id, roles=roles,
|
||||
user_name=user_name, tenant_name=tenant_name,
|
||||
request_id=req_id, auth_token=auth_token)
|
||||
|
||||
# Inject the context...
|
||||
req.environ['neutron.context'] = ctx
|
||||
|
||||
|
@ -86,7 +86,7 @@ class NeutronKeystoneContextTestCase(base.BaseTestCase):
|
||||
self.assertEqual('testuserid', self.context.user_id)
|
||||
self.assertEqual('testusername', self.context.user_name)
|
||||
self.assertEqual('testtenantid', self.context.tenant_id)
|
||||
self.assertEqual('testtenantname', self.context.tenant_name)
|
||||
self.assertEqual('testtenantname', self.context.project_name)
|
||||
|
||||
def test_request_id_extracted_from_env(self):
|
||||
req_id = 'dummy-request-id'
|
||||
|
Loading…
Reference in New Issue
Block a user