Switch to oslo.context
Nuke our copy of context.py and switch over to the oslo.context library. Change-Id: I192238c4de8ccc3e740132219a67e22bb45cb8d0
This commit is contained in:

committed by
Davanum Srinivas (dims)

parent
a5b9c2eccf
commit
4ca2b0293b
@@ -1,129 +0,0 @@
|
||||
# Copyright 2011 OpenStack Foundation.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""
|
||||
Simple class that stores security context information in the web request.
|
||||
|
||||
Projects should subclass this class if they wish to enhance the request
|
||||
context or provide additional information in their specific WSGI pipeline.
|
||||
"""
|
||||
|
||||
import itertools
|
||||
import uuid
|
||||
|
||||
# NOTE(dims): We need this variable for ContextAdapter and ContextFormatter
|
||||
_config = None
|
||||
|
||||
|
||||
def generate_request_id():
|
||||
return b'req-' + str(uuid.uuid4()).encode('ascii')
|
||||
|
||||
|
||||
class RequestContext(object):
|
||||
|
||||
"""Helper class to represent useful information about a request context.
|
||||
|
||||
Stores information about the security context under which the user
|
||||
accesses the system, as well as additional request information.
|
||||
"""
|
||||
|
||||
user_idt_format = '{user} {tenant} {domain} {user_domain} {p_domain}'
|
||||
|
||||
def __init__(self, auth_token=None, user=None, tenant=None, domain=None,
|
||||
user_domain=None, project_domain=None, is_admin=False,
|
||||
read_only=False, show_deleted=False, request_id=None,
|
||||
instance_uuid=None):
|
||||
self.auth_token = auth_token
|
||||
self.user = user
|
||||
self.tenant = tenant
|
||||
self.domain = domain
|
||||
self.user_domain = user_domain
|
||||
self.project_domain = project_domain
|
||||
self.is_admin = is_admin
|
||||
self.read_only = read_only
|
||||
self.show_deleted = show_deleted
|
||||
self.instance_uuid = instance_uuid
|
||||
if not request_id:
|
||||
request_id = generate_request_id()
|
||||
self.request_id = request_id
|
||||
|
||||
def to_dict(self):
|
||||
user_idt = (
|
||||
self.user_idt_format.format(user=self.user or '-',
|
||||
tenant=self.tenant or '-',
|
||||
domain=self.domain or '-',
|
||||
user_domain=self.user_domain or '-',
|
||||
p_domain=self.project_domain or '-'))
|
||||
|
||||
return {'user': self.user,
|
||||
'tenant': self.tenant,
|
||||
'domain': self.domain,
|
||||
'user_domain': self.user_domain,
|
||||
'project_domain': self.project_domain,
|
||||
'is_admin': self.is_admin,
|
||||
'read_only': self.read_only,
|
||||
'show_deleted': self.show_deleted,
|
||||
'auth_token': self.auth_token,
|
||||
'request_id': self.request_id,
|
||||
'instance_uuid': self.instance_uuid,
|
||||
'user_identity': user_idt}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, ctx):
|
||||
return cls(
|
||||
auth_token=ctx.get("auth_token"),
|
||||
user=ctx.get("user"),
|
||||
tenant=ctx.get("tenant"),
|
||||
domain=ctx.get("domain"),
|
||||
user_domain=ctx.get("user_domain"),
|
||||
project_domain=ctx.get("project_domain"),
|
||||
is_admin=ctx.get("is_admin", False),
|
||||
read_only=ctx.get("read_only", False),
|
||||
show_deleted=ctx.get("show_deleted", False),
|
||||
request_id=ctx.get("request_id"),
|
||||
instance_uuid=ctx.get("instance_uuid"))
|
||||
|
||||
|
||||
def get_admin_context(show_deleted=False):
|
||||
context = RequestContext(None,
|
||||
tenant=None,
|
||||
is_admin=True,
|
||||
show_deleted=show_deleted)
|
||||
return context
|
||||
|
||||
|
||||
def get_context_from_function_and_args(function, args, kwargs):
|
||||
"""Find an arg of type RequestContext and return it.
|
||||
|
||||
This is useful in a couple of decorators where we don't
|
||||
know much about the function we're wrapping.
|
||||
"""
|
||||
|
||||
for arg in itertools.chain(kwargs.values(), args):
|
||||
if isinstance(arg, RequestContext):
|
||||
return arg
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def is_user_context(context):
|
||||
"""Indicates if the request context is a normal user."""
|
||||
if not context:
|
||||
return False
|
||||
if context.is_admin:
|
||||
return False
|
||||
if not context.user_id or not context.project_id:
|
||||
return False
|
||||
return True
|
@@ -22,7 +22,6 @@ from six import moves
|
||||
|
||||
from oslo.serialization import jsonutils
|
||||
from oslo_log import _local
|
||||
from oslo_log import context as ctx
|
||||
|
||||
|
||||
def _dictify_context(context):
|
||||
@@ -33,6 +32,16 @@ def _dictify_context(context):
|
||||
return context
|
||||
|
||||
|
||||
# A configuration object is given to us when the application registers
|
||||
# the logging options.
|
||||
_CONF = None
|
||||
|
||||
|
||||
def _store_global_conf(conf):
|
||||
global _CONF
|
||||
_CONF = conf
|
||||
|
||||
|
||||
def _update_record_with_context(record):
|
||||
"""Given a log record, update it with context information.
|
||||
|
||||
@@ -143,7 +152,7 @@ class ContextFormatter(logging.Formatter):
|
||||
|
||||
self.project = kwargs.pop('project', 'unknown')
|
||||
self.version = kwargs.pop('version', 'unknown')
|
||||
self.conf = kwargs.pop('config', ctx._config)
|
||||
self.conf = kwargs.pop('config', _CONF)
|
||||
|
||||
logging.Formatter.__init__(self, *args, **kwargs)
|
||||
|
||||
|
@@ -44,7 +44,6 @@ _PY26 = sys.version_info[0:2] == (2, 6)
|
||||
|
||||
from oslo_log._i18n import _
|
||||
from oslo_log import _options
|
||||
from oslo_log import context as ctx
|
||||
from oslo_log import formatters
|
||||
from oslo_log import handlers
|
||||
|
||||
@@ -149,14 +148,11 @@ def _load_log_config(log_config_append):
|
||||
|
||||
|
||||
def register_options(conf):
|
||||
# NOTE(dims): We need this global variable until we
|
||||
# deprecate ContextAdapter and ContextFormatter
|
||||
ctx._config = conf
|
||||
|
||||
conf.register_cli_opts(_options.common_cli_opts)
|
||||
conf.register_cli_opts(_options.logging_cli_opts)
|
||||
conf.register_opts(_options.generic_log_opts)
|
||||
conf.register_opts(_options.log_opts)
|
||||
formatters._store_global_conf(conf)
|
||||
|
||||
|
||||
def setup(conf, product_name, version='unknown'):
|
||||
|
@@ -1,57 +0,0 @@
|
||||
# Copyright 2011 OpenStack Foundation.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from oslotest import base as test_base
|
||||
|
||||
from oslo_log import context
|
||||
|
||||
|
||||
class ContextTest(test_base.BaseTestCase):
|
||||
|
||||
def test_context(self):
|
||||
ctx = context.RequestContext()
|
||||
self.assertTrue(ctx)
|
||||
|
||||
def test_admin_context_show_deleted_flag_default(self):
|
||||
ctx = context.get_admin_context()
|
||||
self.assertFalse(ctx.show_deleted)
|
||||
|
||||
def test_from_dict(self):
|
||||
dct = {
|
||||
"auth_token": "token1",
|
||||
"user": "user1",
|
||||
"tenant": "tenant1",
|
||||
"domain": "domain1",
|
||||
"user_domain": "user_domain1",
|
||||
"project_domain": "project_domain1",
|
||||
"is_admin": True,
|
||||
"read_only": True,
|
||||
"show_deleted": True,
|
||||
"request_id": "request1",
|
||||
"instance_uuid": "instance1",
|
||||
"extra_data": "foo"
|
||||
}
|
||||
ctx = context.RequestContext.from_dict(dct)
|
||||
self.assertEqual("token1", ctx.auth_token)
|
||||
self.assertEqual("user1", ctx.user)
|
||||
self.assertEqual("tenant1", ctx.tenant)
|
||||
self.assertEqual("domain1", ctx.domain)
|
||||
self.assertEqual("user_domain1", ctx.user_domain)
|
||||
self.assertEqual("project_domain1", ctx.project_domain)
|
||||
self.assertTrue(ctx.is_admin)
|
||||
self.assertTrue(ctx.read_only)
|
||||
self.assertTrue(ctx.show_deleted)
|
||||
self.assertEqual("request1", ctx.request_id)
|
||||
self.assertEqual("instance1", ctx.instance_uuid)
|
@@ -24,12 +24,12 @@ from oslo.config import cfg
|
||||
from oslo.config import fixture as fixture_config # noqa
|
||||
from oslo.i18n import fixture as fixture_trans
|
||||
from oslo.serialization import jsonutils
|
||||
from oslo_context import context
|
||||
from oslotest import base as test_base
|
||||
import six
|
||||
|
||||
from oslo_log import _local
|
||||
from oslo_log import _options
|
||||
from oslo_log import context
|
||||
from oslo_log import formatters
|
||||
from oslo_log import handlers
|
||||
from oslo_log import log
|
||||
|
@@ -7,6 +7,7 @@ Babel>=1.3
|
||||
six>=1.7.0
|
||||
iso8601>=0.1.9
|
||||
oslo.config>=1.4.0 # Apache-2.0
|
||||
oslo.context>=0.1.0 # Apache-2.0
|
||||
oslo.i18n>=1.0.0 # Apache-2.0
|
||||
oslo.utils>=1.1.0 # Apache-2.0
|
||||
oslo.serialization>=1.0.0 # Apache-2.0
|
||||
|
Reference in New Issue
Block a user