Move to the oslo.middleware library

This patch moves Manila to using olso.middleware, updates it to
use the sizelimit middleware in oslo_middleware namespace.

Change-Id: I10c3cbeb9d43b504f14375df4dac87b323fab5bf
Signed-off-by: Lin Yang <lin.a.yang@intel.com>
This commit is contained in:
Lin Yang 2015-04-23 18:15:36 +08:00
parent 2f884924b9
commit 2b49e807d3
6 changed files with 16 additions and 165 deletions

View File

@ -20,7 +20,7 @@ paste.filter_factory = manila.api.middleware.fault:FaultWrapper.factory
paste.filter_factory = manila.api.middleware.auth:NoAuthMiddleware.factory paste.filter_factory = manila.api.middleware.auth:NoAuthMiddleware.factory
[filter:sizelimit] [filter:sizelimit]
paste.filter_factory = manila.api.middleware.sizelimit:RequestBodySizeLimiter.factory paste.filter_factory = oslo_middleware.sizelimit:RequestBodySizeLimiter.factory
[app:apiv1] [app:apiv1]
paste.app_factory = manila.api.v1.router:APIRouter.factory paste.app_factory = manila.api.v1.router:APIRouter.factory

View File

@ -16,68 +16,18 @@ Request Body limiting middleware.
""" """
from oslo_config import cfg
from oslo_log import log from oslo_log import log
import webob.dec from oslo_middleware import sizelimit
import webob.exc
from manila.i18n import _ from manila.i18n import _LW
from manila import wsgi
# default request size is 112k
max_request_body_size_opt = cfg.IntOpt(
'osapi_max_request_body_size',
default=114688,
help='Maximum size for the body of a request.')
CONF = cfg.CONF
CONF.register_opt(max_request_body_size_opt)
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
class LimitingReader(object): class RequestBodySizeLimiter(sizelimit.RequestBodySizeLimiter):
"""Reader to limit the size of an incoming request."""
def __init__(self, data, limit):
"""Initialize LimitingReader.
:param data: Underlying data object
:param limit: maximum number of bytes the reader should allow
"""
self.data = data
self.limit = limit
self.bytes_read = 0
def __iter__(self):
for chunk in self.data:
self.bytes_read += len(chunk)
if self.bytes_read > self.limit:
msg = _("Request is too large.")
raise webob.exc.HTTPRequestEntityTooLarge(explanation=msg)
else:
yield chunk
def read(self, i=None):
result = self.data.read(i)
self.bytes_read += len(result)
if self.bytes_read > self.limit:
msg = _("Request is too large.")
raise webob.exc.HTTPRequestEntityTooLarge(explanation=msg)
return result
class RequestBodySizeLimiter(wsgi.Middleware):
"""Add a 'manila.context' to WSGI environ."""
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
LOG.warn(_LW('manila.api.sizelimit:RequestBodySizeLimiter and '
'manila.api.middleware.sizelimit:RequestBodySizeLimiter '
'are deprecated. Please use oslo_middleware.sizelimit: '
'RequestBodySizeLimiter instead.'))
super(RequestBodySizeLimiter, self).__init__(*args, **kwargs) super(RequestBodySizeLimiter, self).__init__(*args, **kwargs)
@webob.dec.wsgify(RequestClass=wsgi.Request)
def __call__(self, req):
if req.content_length > CONF.osapi_max_request_body_size:
msg = _("Request is too large.")
raise webob.exc.HTTPRequestEntityTooLarge(explanation=msg)
if req.content_length is None and req.is_body_readable:
limiter = LimitingReader(req.body_file,
CONF.osapi_max_request_body_size)
req.body_file = limiter
return self.application

View File

@ -15,8 +15,8 @@
# under the License. # under the License.
from oslo_log import log from oslo_log import log
from oslo_middleware import sizelimit
from manila.api.middleware import sizelimit
from manila.i18n import _LW from manila.i18n import _LW
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
@ -24,7 +24,8 @@ LOG = log.getLogger(__name__)
class RequestBodySizeLimiter(sizelimit.RequestBodySizeLimiter): class RequestBodySizeLimiter(sizelimit.RequestBodySizeLimiter):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
LOG.warn(_LW('manila.api.sizelimit:RequestBodySizeLimiter is ' LOG.warn(_LW('manila.api.sizelimit:RequestBodySizeLimiter and '
'deprecated. Please use manila.api.middleware.sizelimit: ' 'manila.api.middleware.sizelimit:RequestBodySizeLimiter '
'RequestBodySizeLimiter instead')) 'are deprecated. Please use oslo_middleware.sizelimit: '
'RequestBodySizeLimiter instead.'))
super(RequestBodySizeLimiter, self).__init__(*args, **kwargs) super(RequestBodySizeLimiter, self).__init__(*args, **kwargs)

View File

@ -21,11 +21,11 @@ import itertools
import oslo_concurrency.opts import oslo_concurrency.opts
import oslo_log._options import oslo_log._options
import oslo_middleware.opts
import oslo_policy.opts import oslo_policy.opts
import manila.api.common import manila.api.common
import manila.api.middleware.auth import manila.api.middleware.auth
import manila.api.middleware.sizelimit
import manila.common.config import manila.common.config
import manila.compute import manila.compute
import manila.compute.nova import manila.compute.nova
@ -76,7 +76,6 @@ _global_opt_lists = [
# Keep list alphabetically sorted # Keep list alphabetically sorted
manila.api.common.api_common_opts, manila.api.common.api_common_opts,
[manila.api.middleware.auth.use_forwarded_for_opt], [manila.api.middleware.auth.use_forwarded_for_opt],
[manila.api.middleware.sizelimit.max_request_body_size_opt],
manila.common.config.core_opts, manila.common.config.core_opts,
manila.common.config.debug_opts, manila.common.config.debug_opts,
manila.common.config.global_opts, manila.common.config.global_opts,
@ -141,6 +140,7 @@ _opts = [
_opts.extend(oslo_concurrency.opts.list_opts()) _opts.extend(oslo_concurrency.opts.list_opts())
_opts.extend(oslo_log._options.list_opts()) _opts.extend(oslo_log._options.list_opts())
_opts.extend(oslo_middleware.opts.list_opts())
_opts.extend(oslo_policy.opts.list_opts()) _opts.extend(oslo_policy.opts.list_opts())

View File

@ -1,101 +0,0 @@
# Copyright (c) 2012 OpenStack, LLC
#
# 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 oslo_config import cfg
import six
import webob
from manila.api.middleware import sizelimit
from manila import test
CONF = cfg.CONF
MAX_REQUEST_BODY_SIZE = CONF.osapi_max_request_body_size
class TestLimitingReader(test.TestCase):
def test_limiting_reader(self):
BYTES = 1024
bytes_read = 0
data = six.StringIO("*" * BYTES)
for chunk in sizelimit.LimitingReader(data, BYTES):
bytes_read += len(chunk)
self.assertEqual(bytes_read, BYTES)
bytes_read = 0
data = six.StringIO("*" * BYTES)
reader = sizelimit.LimitingReader(data, BYTES)
byte = reader.read(1)
while len(byte) != 0:
bytes_read += 1
byte = reader.read(1)
self.assertEqual(bytes_read, BYTES)
def test_limiting_reader_fails(self):
BYTES = 1024
def _consume_all_iter():
bytes_read = 0
data = six.StringIO("*" * BYTES)
for chunk in sizelimit.LimitingReader(data, BYTES - 1):
bytes_read += len(chunk)
self.assertRaises(webob.exc.HTTPRequestEntityTooLarge,
_consume_all_iter)
def _consume_all_read():
bytes_read = 0
data = six.StringIO("*" * BYTES)
reader = sizelimit.LimitingReader(data, BYTES - 1)
byte = reader.read(1)
while len(byte) != 0:
bytes_read += 1
byte = reader.read(1)
self.assertRaises(webob.exc.HTTPRequestEntityTooLarge,
_consume_all_read)
class TestRequestBodySizeLimiter(test.TestCase):
def setUp(self):
super(TestRequestBodySizeLimiter, self).setUp()
@webob.dec.wsgify()
def fake_app(req):
return webob.Response(req.body)
self.middleware = sizelimit.RequestBodySizeLimiter(fake_app)
self.request = webob.Request.blank('/', method='POST')
def test_content_length_acceptable(self):
self.request.headers['Content-Length'] = MAX_REQUEST_BODY_SIZE
self.request.body = "0" * MAX_REQUEST_BODY_SIZE
response = self.request.get_response(self.middleware)
self.assertEqual(response.status_int, 200)
def test_content_length_too_large(self):
self.request.headers['Content-Length'] = MAX_REQUEST_BODY_SIZE + 1
self.request.body = "0" * (MAX_REQUEST_BODY_SIZE + 1)
response = self.request.get_response(self.middleware)
self.assertEqual(response.status_int, 413)
def test_request_too_large_no_content_length(self):
self.request.body = "0" * (MAX_REQUEST_BODY_SIZE + 1)
self.request.headers['Content-Length'] = None
response = self.request.get_response(self.middleware)
self.assertEqual(response.status_int, 413)

View File

@ -19,6 +19,7 @@ oslo.db>=1.7.0 # Apache-2.0
oslo.i18n>=1.5.0 # Apache-2.0 oslo.i18n>=1.5.0 # Apache-2.0
oslo.log>=1.0.0 # Apache-2.0 oslo.log>=1.0.0 # Apache-2.0
oslo.messaging>=1.8.0 # Apache-2.0 oslo.messaging>=1.8.0 # Apache-2.0
oslo.middleware>=1.0.0 # Apache-2.0
oslo.policy>=0.3.1 # Apache-2.0 oslo.policy>=0.3.1 # Apache-2.0
oslo.rootwrap>=1.6.0 # Apache-2.0 oslo.rootwrap>=1.6.0 # Apache-2.0
oslo.serialization>=1.4.0 # Apache-2.0 oslo.serialization>=1.4.0 # Apache-2.0