Remove incubative openstack.common.context module
glance_store library needn't to use openstack.common.context module really, the only relevancy is swift store unit test, which would be better to use mock instead however. The patch changed swift store unit test to use mock easily, and removed glance_store.openstack namespace due to context module is the last one incubative module synced from oslo-inc. We might use graduated oslo library in future neatly and hopefully. Change-Id: I4442735e4f4350eb01dfd49f4cf8b12a141ce0f7 Signed-off-by: Zhi Yan Liu <zhiyanl@cn.ibm.com>
This commit is contained in:
parent
b8d62633ec
commit
6aa9dd1ec8
@ -1,127 +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
|
||||
|
||||
|
||||
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, service_catalog=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
|
||||
self.service_catalog = service_catalog
|
||||
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
|
@ -1,7 +0,0 @@
|
||||
[DEFAULT]
|
||||
|
||||
# The list of modules to copy from openstack-common
|
||||
module=context
|
||||
|
||||
# The base module to hold the copy of openstack.common
|
||||
base=glance_store
|
@ -41,7 +41,6 @@ from glance_store.common import auth
|
||||
from glance_store.common import utils
|
||||
from glance_store import exceptions
|
||||
from glance_store import location
|
||||
from glance_store.openstack.common import context
|
||||
from glance_store.tests import base
|
||||
from tests.unit import test_store_capabilities
|
||||
|
||||
@ -255,7 +254,7 @@ class SwiftTests(object):
|
||||
(self.swift_store_user, FAKE_UUID))
|
||||
self.config(swift_store_multi_tenant=True)
|
||||
# NOTE(markwash): ensure the image is found
|
||||
ctxt = context.RequestContext()
|
||||
ctxt = mock.MagicMock()
|
||||
size = backend.get_size_from_backend(uri, context=ctxt)
|
||||
self.assertEqual(size, 5120)
|
||||
|
||||
@ -282,7 +281,7 @@ class SwiftTests(object):
|
||||
uri = "swift://%s:key@auth_address/glance/%s" % (
|
||||
self.swift_store_user, FAKE_UUID)
|
||||
loc = location.get_location_from_uri(uri, conf=self.conf)
|
||||
ctxt = context.RequestContext()
|
||||
ctxt = mock.MagicMock()
|
||||
(image_swift, image_size) = self.store.get(loc, context=ctxt)
|
||||
resp_full = ''.join([chunk for chunk in image_swift.wrapped])
|
||||
resp_half = resp_full[:len(resp_full) / 2]
|
||||
@ -309,7 +308,7 @@ class SwiftTests(object):
|
||||
"swift+http://%s:key@auth_address/glance/%s" %
|
||||
(self.swift_store_user, FAKE_UUID), conf=self.conf)
|
||||
|
||||
ctxt = context.RequestContext()
|
||||
ctxt = mock.MagicMock()
|
||||
(image_swift, image_size) = self.store.get(loc, context=ctxt)
|
||||
self.assertEqual(image_size, 5120)
|
||||
|
||||
@ -626,7 +625,7 @@ class SwiftTests(object):
|
||||
self.config(swift_store_multiple_containers_seed=2)
|
||||
fake_get_endpoint = FakeGetEndpoint('https://some_endpoint')
|
||||
self.stubs.Set(auth, 'get_endpoint', fake_get_endpoint)
|
||||
ctxt = context.RequestContext(
|
||||
ctxt = mock.MagicMock(
|
||||
user='user', tenant='tenant', auth_token='123',
|
||||
service_catalog={})
|
||||
store = swift.MultiTenantStore(self.conf)
|
||||
@ -944,7 +943,7 @@ class SwiftTests(object):
|
||||
store.configure()
|
||||
uri = "swift+http://storeurl/glance/%s" % FAKE_UUID
|
||||
loc = location.get_location_from_uri(uri, conf=self.conf)
|
||||
ctxt = context.RequestContext()
|
||||
ctxt = mock.MagicMock()
|
||||
store.set_acls(loc, public=True, context=ctxt)
|
||||
container_headers = swiftclient.client.head_container('x', 'y',
|
||||
'glance')
|
||||
@ -961,7 +960,7 @@ class SwiftTests(object):
|
||||
uri = "swift+http://storeurl/glance/%s" % FAKE_UUID
|
||||
loc = location.get_location_from_uri(uri, conf=self.conf)
|
||||
read_tenants = ['matt', 'mark']
|
||||
ctxt = context.RequestContext()
|
||||
ctxt = mock.MagicMock()
|
||||
store.set_acls(loc, read_tenants=read_tenants, context=ctxt)
|
||||
container_headers = swiftclient.client.head_container('x', 'y',
|
||||
'glance')
|
||||
@ -978,7 +977,7 @@ class SwiftTests(object):
|
||||
uri = "swift+http://storeurl/glance/%s" % FAKE_UUID
|
||||
loc = location.get_location_from_uri(uri, conf=self.conf)
|
||||
read_tenants = ['frank', 'jim']
|
||||
ctxt = context.RequestContext()
|
||||
ctxt = mock.MagicMock()
|
||||
store.set_acls(loc, write_tenants=read_tenants, context=ctxt)
|
||||
container_headers = swiftclient.client.head_container('x', 'y',
|
||||
'glance')
|
||||
@ -1092,7 +1091,7 @@ class TestSingleTenantStoreConnections(base.StoreBaseTest):
|
||||
'endpoint_type': 'publicURL'})
|
||||
|
||||
def test_connection_with_conf_endpoint(self):
|
||||
ctx = context.RequestContext(user='tenant:user1', tenant='tenant')
|
||||
ctx = mock.MagicMock(user='tenant:user1', tenant='tenant')
|
||||
self.config(swift_store_endpoint='https://internal.com')
|
||||
self.store.configure()
|
||||
connection = self.store.get_connection(self.location, context=ctx)
|
||||
@ -1207,7 +1206,7 @@ class TestMultiTenantStoreConnections(base.StoreBaseTest):
|
||||
moxfixture = self.useFixture(moxstubout.MoxStubout())
|
||||
self.stubs = moxfixture.stubs
|
||||
self.stubs.Set(swiftclient, 'Connection', FakeConnection)
|
||||
self.context = context.RequestContext(
|
||||
self.context = mock.MagicMock(
|
||||
user='tenant:user1', tenant='tenant', auth_token='0123')
|
||||
self.store = swift.MultiTenantStore(self.conf)
|
||||
specs = {'scheme': 'swift',
|
||||
@ -1263,7 +1262,7 @@ class TestMultiTenantStoreContext(base.StoreBaseTest):
|
||||
store.configure()
|
||||
uri = "swift+http://127.0.0.1/glance_123/123"
|
||||
loc = location.get_location_from_uri(uri, conf=self.conf)
|
||||
ctx = context.RequestContext(
|
||||
ctx = mock.MagicMock(
|
||||
service_catalog=self.service_catalog, user='tenant:user1',
|
||||
tenant='tenant', auth_token='0123')
|
||||
|
||||
@ -1283,7 +1282,7 @@ class TestMultiTenantStoreContext(base.StoreBaseTest):
|
||||
store = Store(self.conf)
|
||||
store.configure()
|
||||
pseudo_file = StringIO.StringIO('Some data')
|
||||
ctx = context.RequestContext(
|
||||
ctx = mock.MagicMock(
|
||||
service_catalog=self.service_catalog, user='tenant:user1',
|
||||
tenant='tenant', auth_token='0123')
|
||||
store.add('123', pseudo_file, pseudo_file.len,
|
||||
@ -1356,7 +1355,7 @@ class TestCreatingLocations(base.StoreBaseTest):
|
||||
self.config(swift_store_container='container')
|
||||
fake_get_endpoint = FakeGetEndpoint('https://some_endpoint')
|
||||
self.stubs.Set(auth, 'get_endpoint', fake_get_endpoint)
|
||||
ctxt = context.RequestContext(
|
||||
ctxt = mock.MagicMock(
|
||||
user='user', tenant='tenant', auth_token='123',
|
||||
service_catalog={})
|
||||
store = swift.MultiTenantStore(self.conf)
|
||||
@ -1373,7 +1372,7 @@ class TestCreatingLocations(base.StoreBaseTest):
|
||||
def test_multi_tenant_location_http(self):
|
||||
fake_get_endpoint = FakeGetEndpoint('http://some_endpoint')
|
||||
self.stubs.Set(auth, 'get_endpoint', fake_get_endpoint)
|
||||
ctxt = context.RequestContext(
|
||||
ctxt = mock.MagicMock(
|
||||
user='user', tenant='tenant', auth_token='123',
|
||||
service_catalog={})
|
||||
store = swift.MultiTenantStore(self.conf)
|
||||
@ -1386,7 +1385,7 @@ class TestCreatingLocations(base.StoreBaseTest):
|
||||
self.config(swift_store_region='WestCarolina')
|
||||
fake_get_endpoint = FakeGetEndpoint('https://some_endpoint')
|
||||
self.stubs.Set(auth, 'get_endpoint', fake_get_endpoint)
|
||||
ctxt = context.RequestContext(
|
||||
ctxt = mock.MagicMock(
|
||||
user='user', tenant='tenant', auth_token='123',
|
||||
service_catalog={})
|
||||
store = swift.MultiTenantStore(self.conf)
|
||||
@ -1398,7 +1397,7 @@ class TestCreatingLocations(base.StoreBaseTest):
|
||||
self.config(swift_store_service_type='toy-store')
|
||||
fake_get_endpoint = FakeGetEndpoint('https://some_endpoint')
|
||||
self.stubs.Set(auth, 'get_endpoint', fake_get_endpoint)
|
||||
ctxt = context.RequestContext(
|
||||
ctxt = mock.MagicMock(
|
||||
user='user', tenant='tenant', auth_token='123',
|
||||
service_catalog={})
|
||||
store = swift.MultiTenantStore(self.conf)
|
||||
@ -1410,7 +1409,7 @@ class TestCreatingLocations(base.StoreBaseTest):
|
||||
self.config(swift_store_endpoint_type='InternalURL')
|
||||
fake_get_endpoint = FakeGetEndpoint('https://some_endpoint')
|
||||
self.stubs.Set(auth, 'get_endpoint', fake_get_endpoint)
|
||||
ctxt = context.RequestContext(
|
||||
ctxt = mock.MagicMock(
|
||||
user='user', tenant='tenant', auth_token='123',
|
||||
service_catalog={})
|
||||
store = swift.MultiTenantStore(self.conf)
|
||||
|
Loading…
Reference in New Issue
Block a user