From 742bde97a3db78cfe96975191b3341f4158c3e24 Mon Sep 17 00:00:00 2001 From: Vincent Hou Date: Mon, 17 Jun 2013 16:42:43 +0800 Subject: [PATCH] Add the project name into CinderKeystoneContext. Fixed Bug 1178576. Change-Id: I35cbd40c6f4aba45aeb8722ae5e10db8349d4510 --- cinder/api/middleware/auth.py | 2 ++ cinder/context.py | 8 +++++--- cinder/tests/api/middleware/test_auth.py | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/cinder/api/middleware/auth.py b/cinder/api/middleware/auth.py index 17bda1f94..a953b490f 100644 --- a/cinder/api/middleware/auth.py +++ b/cinder/api/middleware/auth.py @@ -91,6 +91,7 @@ class CinderKeystoneContext(base_wsgi.Middleware): # This is for legacy compatibility project_id = req.headers['X_TENANT'] + project_name = req.headers.get('X_TENANT_NAME') # Get the auth token auth_token = req.headers.get('X_AUTH_TOKEN', req.headers.get('X_STORAGE_TOKEN')) @@ -101,6 +102,7 @@ class CinderKeystoneContext(base_wsgi.Middleware): remote_address = req.headers.get('X-Forwarded-For', remote_address) ctx = context.RequestContext(user_id, project_id, + project_name=project_name, roles=roles, auth_token=auth_token, remote_address=remote_address) diff --git a/cinder/context.py b/cinder/context.py index 951496043..4b50d63dd 100644 --- a/cinder/context.py +++ b/cinder/context.py @@ -43,9 +43,9 @@ class RequestContext(object): """ def __init__(self, user_id, project_id, is_admin=None, read_deleted="no", - roles=None, remote_address=None, timestamp=None, - request_id=None, auth_token=None, overwrite=True, - quota_class=None, **kwargs): + roles=None, project_name=None, remote_address=None, + timestamp=None, request_id=None, auth_token=None, + overwrite=True, quota_class=None, **kwargs): """ :param read_deleted: 'no' indicates deleted records are hidden, 'yes' indicates deleted records are visible, 'only' indicates that @@ -64,6 +64,7 @@ class RequestContext(object): self.user_id = user_id self.project_id = project_id self.roles = roles or [] + self.project_name = project_name self.is_admin = is_admin if self.is_admin is None: self.is_admin = policy.check_is_admin(self.roles) @@ -105,6 +106,7 @@ class RequestContext(object): def to_dict(self): return {'user_id': self.user_id, 'project_id': self.project_id, + 'project_name': self.project_name, 'is_admin': self.is_admin, 'read_deleted': self.read_deleted, 'roles': self.roles, diff --git a/cinder/tests/api/middleware/test_auth.py b/cinder/tests/api/middleware/test_auth.py index 4fca13fe6..879ff99a5 100644 --- a/cinder/tests/api/middleware/test_auth.py +++ b/cinder/tests/api/middleware/test_auth.py @@ -40,16 +40,16 @@ class TestCinderKeystoneContextMiddleware(test.TestCase): self.assertEqual(response.status, '401 Unauthorized') def test_user_only(self): - self.request.headers['X_USER_ID'] = 'testuserid' + self.request.headers['X_USER'] = 'testuser' response = self.request.get_response(self.middleware) self.assertEqual(response.status, '200 OK') - self.assertEqual(self.context.user_id, 'testuserid') + self.assertEqual(self.context.user_id, 'testuser') def test_user_id_only(self): - self.request.headers['X_USER'] = 'testuser' + self.request.headers['X_USER_ID'] = 'testuserid' response = self.request.get_response(self.middleware) self.assertEqual(response.status, '200 OK') - self.assertEqual(self.context.user_id, 'testuser') + self.assertEqual(self.context.user_id, 'testuserid') def test_user_id_trumps_user(self): self.request.headers['X_USER_ID'] = 'testuserid' @@ -57,3 +57,11 @@ class TestCinderKeystoneContextMiddleware(test.TestCase): response = self.request.get_response(self.middleware) self.assertEqual(response.status, '200 OK') self.assertEqual(self.context.user_id, 'testuserid') + + def test_tenant_id_name(self): + self.request.headers['X_USER_ID'] = 'testuserid' + self.request.headers['X_TENANT_NAME'] = 'testtenantname' + response = self.request.get_response(self.middleware) + self.assertEqual(response.status, '200 OK') + self.assertEqual(self.context.project_id, 'testtenantid') + self.assertEqual(self.context.project_name, 'testtenantname') -- 2.45.2