summaryrefslogtreecommitdiff
path: root/oslo_context
diff options
context:
space:
mode:
authorJamie Lennox <jamielennox@gmail.com>2016-01-06 11:29:02 +1100
committerJamie Lennox <jamielennox@gmail.com>2016-01-11 10:59:19 +1100
commit1f3719dfcfddd2075055ad234801c3ec23c4ff83 (patch)
tree9f720a1784cd33da980c93669f666f5074c134a3 /oslo_context
parent04e40fa8d4a94c9dbc0b9d8ff6aef735c095f603 (diff)
downloadoslo-context-1f3719dfcfddd2075055ad234801c3ec23c4ff83.tar.gz
Provide a helper to load a context from environment
The intention here is to provide better integration between what oslo.context expects and what keystonemiddleware provides. As auth_token middleware changes each service has to react independently to the new options and the way they build contexts. By providing a helper here we can improve the consistency across services to standardize the options and provide a common way to react to auth_token middleware improvements. Change-Id: Ic590b3ee359e9a9c27eef3d11c0738066f475881
Diffstat (limited to 'oslo_context')
-rw-r--r--oslo_context/context.py22
-rw-r--r--oslo_context/tests/test_context.py27
2 files changed, 49 insertions, 0 deletions
diff --git a/oslo_context/context.py b/oslo_context/context.py
index 5d06cb8..9dd4001 100644
--- a/oslo_context/context.py
+++ b/oslo_context/context.py
@@ -106,6 +106,28 @@ class RequestContext(object):
request_id=ctx.get("request_id"),
resource_uuid=ctx.get("resource_uuid"))
+ @classmethod
+ def from_environ(cls, environ, **kwargs):
+ """Load a context object from a request environment.
+
+ If keyword arguments are provided then they override the values in the
+ request environment.
+
+ :param environ: The environment dictionary associated with a request.
+ :type environ: dict
+ """
+ # Load a new context object from the environment variables set by
+ # auth_token middleware. See:
+ # http://docs.openstack.org/developer/keystonemiddleware/api/keystonemiddleware.auth_token.html#what-auth-token-adds-to-the-request-for-use-by-the-openstack-service
+ kwargs.setdefault('auth_token', environ.get('HTTP_X_AUTH_TOKEN'))
+ kwargs.setdefault('user', environ.get('HTTP_X_USER_ID'))
+ kwargs.setdefault('tenant', environ.get('HTTP_X_PROJECT_ID'))
+ kwargs.setdefault('user_domain', environ.get('HTTP_X_USER_DOMAIN_ID'))
+ kwargs.setdefault('project_domain',
+ environ.get('HTTP_X_PROJECT_DOMAIN_ID'))
+
+ return cls(**kwargs)
+
def get_admin_context(show_deleted=False):
"""Create an administrator context."""
diff --git a/oslo_context/tests/test_context.py b/oslo_context/tests/test_context.py
index 9e6b252..531d762 100644
--- a/oslo_context/tests/test_context.py
+++ b/oslo_context/tests/test_context.py
@@ -13,12 +13,18 @@
# License for the specific language governing permissions and limitations
# under the License.
+import uuid
+
from oslotest import base as test_base
from oslo_context import context
from oslo_context import fixture
+class Object(object):
+ pass
+
+
class ContextTest(test_base.BaseTestCase):
def setUp(self):
@@ -92,3 +98,24 @@ class ContextTest(test_base.BaseTestCase):
self.assertFalse(context.is_user_context(ctx))
ctx = context.RequestContext(is_admin=False)
self.assertTrue(context.is_user_context(ctx))
+
+ def test_from_environ_variables(self):
+ auth_token = uuid.uuid4().hex
+ user_id = uuid.uuid4().hex
+ project_id = uuid.uuid4().hex
+ user_domain_id = uuid.uuid4().hex
+ project_domain_id = uuid.uuid4().hex
+
+ environ = {'HTTP_X_AUTH_TOKEN': auth_token,
+ 'HTTP_X_USER_ID': user_id,
+ 'HTTP_X_PROJECT_ID': project_id,
+ 'HTTP_X_USER_DOMAIN_ID': user_domain_id,
+ 'HTTP_X_PROJECT_DOMAIN_ID': project_domain_id}
+
+ ctx = context.RequestContext.from_environ(environ)
+
+ self.assertEqual(auth_token, ctx.auth_token)
+ self.assertEqual(user_id, ctx.user)
+ self.assertEqual(project_id, ctx.tenant)
+ self.assertEqual(user_domain_id, ctx.user_domain)
+ self.assertEqual(project_domain_id, ctx.project_domain)