Reorganize context module

* Move RequestContext class to glance.context
* Move context middlewares to glance.api.middleware
* Update tests to reflect move
* Update paste configs

Related to bp glance-request-tracking

Change-Id: I289b546ec28c973a3022be779ce378ae2febb340
This commit is contained in:
Brian Waldon 2012-07-09 08:05:28 -07:00
parent 320f809ba1
commit ce899351da
17 changed files with 83 additions and 63 deletions

View File

@ -59,10 +59,10 @@ paste.filter_factory = glance.api.middleware.cache:CacheFilter.factory
paste.filter_factory = glance.api.middleware.cache_manage:CacheManageFilter.factory
[filter:context]
paste.filter_factory = glance.common.context:ContextMiddleware.factory
paste.filter_factory = glance.api.middleware.context:ContextMiddleware.factory
[filter:unauthenticated-context]
paste.filter_factory = glance.common.context:UnauthenticatedContextMiddleware.factory
paste.filter_factory = glance.api.middleware.context:UnauthenticatedContextMiddleware.factory
[filter:authtoken]
paste.filter_factory = keystone.middleware.auth_token:filter_factory

View File

@ -14,10 +14,10 @@ pipeline = authtoken context registryapp
paste.app_factory = glance.registry.api.v1:API.factory
[filter:context]
paste.filter_factory = glance.common.context:ContextMiddleware.factory
paste.filter_factory = glance.api.middleware.context:ContextMiddleware.factory
[filter:unauthenticated-context]
paste.filter_factory = glance.common.context:UnauthenticatedContextMiddleware.factory
paste.filter_factory = glance.api.middleware.context:UnauthenticatedContextMiddleware.factory
[filter:authtoken]
paste.filter_factory = keystone.middleware.auth_token:filter_factory

View File

@ -1,6 +1,6 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 OpenStack LLC.
# Copyright 2011-2012 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
@ -17,10 +17,11 @@
import webob.exc
from glance.common import exception
from glance.common import wsgi
import glance.context
from glance.openstack.common import cfg
context_opts = [
cfg.BoolOpt('owner_is_tenant', default=True),
cfg.StrOpt('admin_role', default='admin'),
@ -31,37 +32,6 @@ CONF = cfg.CONF
CONF.register_opts(context_opts)
class RequestContext(object):
"""
Stores information about the security context under which the user
accesses the system, as well as additional request information.
"""
def __init__(self, auth_tok=None, user=None, tenant=None, roles=None,
is_admin=False, read_only=False, show_deleted=False,
owner_is_tenant=True):
self.auth_tok = auth_tok
self.user = user
self.tenant = tenant
self.roles = roles or []
self.is_admin = is_admin
self.read_only = read_only
self._show_deleted = show_deleted
self.owner_is_tenant = owner_is_tenant
@property
def owner(self):
"""Return the owner to correlate with an image."""
return self.tenant if self.owner_is_tenant else self.user
@property
def show_deleted(self):
"""Admins can see deleted by default"""
if self._show_deleted or self.is_admin:
return True
return False
class ContextMiddleware(wsgi.Middleware):
def __init__(self, app):
@ -70,7 +40,7 @@ class ContextMiddleware(wsgi.Middleware):
def process_request(self, req):
"""Convert authentication information into a request context
Generate a RequestContext object from the available
Generate a glance.context.RequestContext object from the available
authentication headers and store on the 'context' attribute
of the req object.
@ -94,7 +64,7 @@ class ContextMiddleware(wsgi.Middleware):
'is_admin': False,
'read_only': True,
}
return RequestContext(**kwargs)
return glance.context.RequestContext(**kwargs)
def _get_authenticated_context(self, req):
#NOTE(bcwaldon): X-Roles is a csv string, but we need to parse
@ -114,7 +84,7 @@ class ContextMiddleware(wsgi.Middleware):
'owner_is_tenant': CONF.owner_is_tenant,
}
return RequestContext(**kwargs)
return glance.context.RequestContext(**kwargs)
class UnauthenticatedContextMiddleware(wsgi.Middleware):
@ -131,4 +101,4 @@ class UnauthenticatedContextMiddleware(wsgi.Middleware):
'is_admin': True,
}
req.context = RequestContext(**kwargs)
req.context = glance.context.RequestContext(**kwargs)

47
glance/context.py Normal file
View File

@ -0,0 +1,47 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011-2012 OpenStack LLC.
# 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.
class RequestContext(object):
"""
Stores information about the security context under which the user
accesses the system, as well as additional request information.
"""
def __init__(self, auth_tok=None, user=None, tenant=None, roles=None,
is_admin=False, read_only=False, show_deleted=False,
owner_is_tenant=True):
self.auth_tok = auth_tok
self.user = user
self.tenant = tenant
self.roles = roles or []
self.is_admin = is_admin
self.read_only = read_only
self._show_deleted = show_deleted
self.owner_is_tenant = owner_is_tenant
@property
def owner(self):
"""Return the owner to correlate with an image."""
return self.tenant if self.owner_is_tenant else self.user
@property
def show_deleted(self):
"""Admins can see deleted by default"""
if self._show_deleted or self.is_admin:
return True
return False

View File

@ -23,8 +23,8 @@ import logging
import eventlet
from glance.common import context
from glance.common import exception
from glance import context
from glance.image_cache import base
from glance import registry
import glance.store

View File

@ -303,11 +303,11 @@ paste.filter_factory =
glance.api.middleware.cache_manage:CacheManageFilter.factory
[filter:context]
paste.filter_factory = glance.common.context:ContextMiddleware.factory
paste.filter_factory = glance.api.middleware.context:ContextMiddleware.factory
[filter:unauthenticated-context]
paste.filter_factory =
glance.common.context:UnauthenticatedContextMiddleware.factory
glance.api.middleware.context:UnauthenticatedContextMiddleware.factory
[filter:fakeauth]
paste.filter_factory = glance.tests.utils:FakeAuthMiddleware.factory
@ -359,11 +359,11 @@ pipeline = fakeauth context registryapp
paste.app_factory = glance.registry.api.v1:API.factory
[filter:context]
paste.filter_factory = glance.common.context:ContextMiddleware.factory
paste.filter_factory = glance.api.middleware.context:ContextMiddleware.factory
[filter:unauthenticated-context]
paste.filter_factory =
glance.common.context:UnauthenticatedContextMiddleware.factory
glance.api.middleware.context:UnauthenticatedContextMiddleware.factory
[filter:fakeauth]
paste.filter_factory = glance.tests.utils:FakeAuthMiddleware.factory

View File

@ -19,9 +19,9 @@
import copy
import datetime
from glance.common import context
from glance.common import exception
from glance.common import utils
from glance import context
# The default sort order of results is whatever sort key is specified,

View File

@ -28,9 +28,9 @@ except ImportError:
import routes
import webob
from glance.api.middleware import context
from glance.api.v1 import router
import glance.common.client
from glance.common import context
from glance.registry.api import v1 as rserver
from glance.tests import utils

View File

@ -21,9 +21,9 @@ import tempfile
from glance import client
from glance.common import client as base_client
from glance.common import context
from glance.common import exception
from glance.common import utils
from glance import context
from glance.db.sqlalchemy import api as db_api
from glance.db.sqlalchemy import models as db_models
from glance.openstack.common import timeutils

View File

@ -21,8 +21,8 @@ import tempfile
import stubout
from glance.api.middleware import context
from glance.common import config
from glance.common import context
from glance.image_cache import pruner
from glance.tests import utils as test_utils

View File

@ -15,7 +15,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from glance.common import context
from glance import context
from glance.tests.unit import utils as unit_utils
from glance.tests import utils

View File

@ -1,7 +1,7 @@
import webob
from glance.common import context
from glance.api.middleware import context
from glance.tests.unit import base

View File

@ -20,7 +20,8 @@ import StringIO
import stubout
from glance.common import config
from glance.common import exception, context
from glance.common import exception
from glance import context
from glance.db.sqlalchemy import api as db_api
from glance.registry import configure_registry_client
from glance.store import (delete_from_backend,

View File

@ -15,9 +15,9 @@
import logging
import glance.common.context
from glance.common import exception
from glance.common import wsgi
import glance.context
import glance.db.simple.api as simple_db
@ -44,7 +44,7 @@ def get_fake_request(path='', method='POST', is_admin=False):
'is_admin': is_admin,
}
req.context = glance.common.context.RequestContext(**kwargs)
req.context = glance.context.RequestContext(**kwargs)
return req

View File

@ -25,10 +25,11 @@ from sqlalchemy import exc
import stubout
import webob
import glance.api.middleware.context as context_middleware
from glance.api.v1 import images
from glance.api.v1 import router
from glance.common import context
from glance.common import utils
import glance.context
from glance.db.sqlalchemy import api as db_api
from glance.db.sqlalchemy import models as db_models
from glance.openstack.common import timeutils
@ -128,7 +129,7 @@ class TestRegistryAPI(base.IsolatedUnitTest):
'size': 19,
'location': "file:///%s/%s" % (self.test_dir, UUID2),
'properties': {}}]
self.context = context.RequestContext(is_admin=True)
self.context = glance.context.RequestContext(is_admin=True)
db_api.configure_db()
self.destroy_fixtures()
self.create_fixtures()
@ -1999,7 +2000,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
'size': 19,
'location': "file:///%s/%s" % (self.test_dir, UUID2),
'properties': {}}]
self.context = context.RequestContext(is_admin=True)
self.context = glance.context.RequestContext(is_admin=True)
db_api.configure_db()
self.destroy_fixtures()
self.create_fixtures()
@ -3007,9 +3008,10 @@ class TestImageSerializer(base.IsolatedUnitTest):
super(TestImageSerializer, self).setUp()
self.receiving_user = 'fake_user'
self.receiving_tenant = 2
self.context = context.RequestContext(is_admin=True,
user=self.receiving_user,
tenant=self.receiving_tenant)
self.context = glance.context.RequestContext(
is_admin=True,
user=self.receiving_user,
tenant=self.receiving_tenant)
self.serializer = images.ImageSerializer()
def image_iter():

View File

@ -28,9 +28,9 @@ import unittest
import nose.plugins.skip
from glance.common import config
from glance.common import context
from glance.common import utils
from glance.common import wsgi
from glance import context
from glance.openstack.common import cfg
from glance import store

View File

@ -5,7 +5,7 @@ import sys
import keystoneclient.v2_0.client
import glance.common.context
import glance.context
from glance.openstack.common import cfg
import glance.registry.context
import glance.db.sqlalchemy.api as db_api