diff --git a/etc/manila/api-paste.ini b/etc/manila/api-paste.ini index e3f19e3b..237a1cf0 100644 --- a/etc/manila/api-paste.ini +++ b/etc/manila/api-paste.ini @@ -20,7 +20,7 @@ paste.filter_factory = manila.api.middleware.fault:FaultWrapper.factory paste.filter_factory = manila.api.middleware.auth:NoAuthMiddleware.factory [filter:sizelimit] -paste.filter_factory = manila.api.middleware.sizelimit:RequestBodySizeLimiter.factory +paste.filter_factory = oslo_middleware.sizelimit:RequestBodySizeLimiter.factory [app:apiv1] paste.app_factory = manila.api.v1.router:APIRouter.factory diff --git a/manila/api/middleware/sizelimit.py b/manila/api/middleware/sizelimit.py index 804bbc4d..85624276 100644 --- a/manila/api/middleware/sizelimit.py +++ b/manila/api/middleware/sizelimit.py @@ -16,68 +16,18 @@ Request Body limiting middleware. """ -from oslo_config import cfg from oslo_log import log -import webob.dec -import webob.exc +from oslo_middleware import sizelimit -from manila.i18n import _ -from manila import wsgi +from manila.i18n import _LW -# 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__) -class LimitingReader(object): - """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.""" - +class RequestBodySizeLimiter(sizelimit.RequestBodySizeLimiter): 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) - - @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 diff --git a/manila/api/sizelimit.py b/manila/api/sizelimit.py index 9fc26ce3..a54330a8 100644 --- a/manila/api/sizelimit.py +++ b/manila/api/sizelimit.py @@ -15,8 +15,8 @@ # under the License. from oslo_log import log +from oslo_middleware import sizelimit -from manila.api.middleware import sizelimit from manila.i18n import _LW LOG = log.getLogger(__name__) @@ -24,7 +24,8 @@ LOG = log.getLogger(__name__) class RequestBodySizeLimiter(sizelimit.RequestBodySizeLimiter): def __init__(self, *args, **kwargs): - LOG.warn(_LW('manila.api.sizelimit:RequestBodySizeLimiter is ' - 'deprecated. Please use manila.api.middleware.sizelimit: ' - 'RequestBodySizeLimiter instead')) + 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) diff --git a/manila/opts.py b/manila/opts.py index 154c6b98..a30cc837 100644 --- a/manila/opts.py +++ b/manila/opts.py @@ -21,11 +21,11 @@ import itertools import oslo_concurrency.opts import oslo_log._options +import oslo_middleware.opts import oslo_policy.opts import manila.api.common import manila.api.middleware.auth -import manila.api.middleware.sizelimit import manila.common.config import manila.compute import manila.compute.nova @@ -76,7 +76,6 @@ _global_opt_lists = [ # Keep list alphabetically sorted manila.api.common.api_common_opts, [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.debug_opts, manila.common.config.global_opts, @@ -141,6 +140,7 @@ _opts = [ _opts.extend(oslo_concurrency.opts.list_opts()) _opts.extend(oslo_log._options.list_opts()) +_opts.extend(oslo_middleware.opts.list_opts()) _opts.extend(oslo_policy.opts.list_opts()) diff --git a/manila/tests/api/middleware/test_sizelimit.py b/manila/tests/api/middleware/test_sizelimit.py deleted file mode 100644 index 9c18d9bb..00000000 --- a/manila/tests/api/middleware/test_sizelimit.py +++ /dev/null @@ -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) diff --git a/requirements.txt b/requirements.txt index 8052c8a9..03a3b3f8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,6 +19,7 @@ oslo.db>=1.7.0 # Apache-2.0 oslo.i18n>=1.5.0 # Apache-2.0 oslo.log>=1.0.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.rootwrap>=1.6.0 # Apache-2.0 oslo.serialization>=1.4.0 # Apache-2.0