]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Move to the oslo.middleware library
authorJay S. Bryant <jsbryant@us.ibm.com>
Wed, 11 Mar 2015 01:15:00 +0000 (20:15 -0500)
committerJay S. Bryant <jsbryant@us.ibm.com>
Thu, 12 Mar 2015 18:57:23 +0000 (13:57 -0500)
This patch moves Cinder to using olso.middleware, updates us so
we are using the oslo_middleware namespace and syncs the latest
middleware code from oslo-incubator to support grenade jobs.

The details for the middleware sync from oslo-incubator are as follows:

Current HEAD in OSLO:
---------------------
commit e589dde0721a0a67e4030813e582afec6e70d042
Date:  Wed Feb 18 03:08:12 2015 +0000
Merge "Have a little fun with release notes"

Changes merged with this patch:
---------------------
__init__.py
  4ffc4c87 - Add middleware.request_id shim for Kilo
  4504e4f4 - Remove middleware

catch_errors.py
  a01a8527 - Use oslo_middleware instead of deprecated oslo.middleware
  ce8f8fa4 - Add middleware.catch_errors shim for Kilo
  4504e4f4 - Remove middleware
  5d40e143 - Remove code that moved to oslo.i18n
  76183592 - add deprecation note to middleware
  463e6916 - remove oslo log from middleware
  fcf517d7 - Update oslo log messages with translation domains

request_id.py
  a01a8527 - Use oslo_middleware instead of deprecated oslo.middleware
  66d8d613 - Fix oslo.middleware deprecation error
  4ffc4c87 - Add middleware.request_id shim for Kilo
  4504e4f4 - Remove middleware
  76183592 - add deprecation note to middleware
  d7bd9dc3 - Don't store the request ID value in middleware as class variable

Some notes on this change.  It is based on the change made in Nova:
https://review.openstack.org/#/c/130771 and is the recommended method
for cleaning up the unused portions of middleware from oslo-incubator,
moving to the oslo.middleware library and not breaking grenade in the
gate.

Change-Id: Ia99ab479cb8ef63a0db1a1208cc2501abba6132c

cinder/api/middleware/auth.py
cinder/api/middleware/sizelimit.py
cinder/openstack/common/middleware/base.py [deleted file]
cinder/openstack/common/middleware/catch_errors.py
cinder/openstack/common/middleware/request_id.py
cinder/tests/api/middleware/test_auth.py
cinder/tests/api/middleware/test_sizelimit.py [deleted file]
etc/cinder/api-paste.ini
requirements.txt
setup.cfg

index 66e44a13ff7537f543632ee4b8d84d6c2d7dc240..9789e1b146adf9ef87a3425a687fcdf30b18f376 100644 (file)
@@ -22,6 +22,7 @@ import os
 
 from oslo_config import cfg
 from oslo_log import log as logging
+from oslo_middleware import request_id
 from oslo_serialization import jsonutils
 import webob.dec
 import webob.exc
@@ -29,7 +30,6 @@ import webob.exc
 from cinder.api.openstack import wsgi
 from cinder import context
 from cinder.i18n import _
-from cinder.openstack.common.middleware import request_id
 from cinder import wsgi as base_wsgi
 
 
index 97a4d125f6d8e6953a5be2eb1f149b65ebecb7c3..671945d395b9f75620e45826f02af453b3deb55d 100644 (file)
 #    under the License.
 """
 Request Body limiting middleware.
-
+Compatibility shim for Kilo, while operators migrate to oslo.middleware.
 """
 
 
 from oslo_config import cfg
-from oslo_log import log as logging
-import webob.dec
-import webob.exc
+from oslo_middleware import sizelimit
 
-from cinder.i18n import _
-from cinder import wsgi
+from cinder.openstack.common import versionutils
 
 
 # Default request size is 112k
@@ -34,52 +31,9 @@ max_request_body_size_opt = cfg.IntOpt('osapi_max_request_body_size',
 CONF = cfg.CONF
 CONF.register_opt(max_request_body_size_opt)
 
-LOG = logging.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):
+@versionutils.deprecated(as_of=versionutils.deprecated.KILO,
+                         in_favor_of='oslo_middleware.RequestBodySizeLimiter')
+class RequestBodySizeLimiter(sizelimit.RequestBodySizeLimiter):
     """Add a 'cinder.context' to WSGI environ."""
-
-    def __init__(self, *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
+    pass
diff --git a/cinder/openstack/common/middleware/base.py b/cinder/openstack/common/middleware/base.py
deleted file mode 100644 (file)
index 464a1cc..0000000
+++ /dev/null
@@ -1,56 +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.
-
-"""Base class(es) for WSGI Middleware."""
-
-import webob.dec
-
-
-class Middleware(object):
-    """Base WSGI middleware wrapper.
-
-    These classes require an application to be initialized that will be called
-    next.  By default the middleware will simply call its wrapped app, or you
-    can override __call__ to customize its behavior.
-    """
-
-    @classmethod
-    def factory(cls, global_conf, **local_conf):
-        """Factory method for paste.deploy."""
-        return cls
-
-    def __init__(self, application):
-        self.application = application
-
-    def process_request(self, req):
-        """Called on each request.
-
-        If this returns None, the next application down the stack will be
-        executed. If it returns a response then that response will be returned
-        and execution will stop here.
-        """
-        return None
-
-    def process_response(self, response):
-        """Do whatever you'd like to the response."""
-        return response
-
-    @webob.dec.wsgify
-    def __call__(self, req):
-        response = self.process_request(req)
-        if response:
-            return response
-        response = req.get_response(self.application)
-        return self.process_response(response)
index 727372bf1c2a746ec39b103bf2506bc385a58290..db5356c9740d887e144dc9e5b1347999e5b10245 100644 (file)
 
 """Compatibility shim for Kilo, while operators migrate to oslo.middleware."""
 
-from oslo.middleware import catch_errors
+from oslo_middleware import catch_errors
 
 from cinder.openstack.common import versionutils
 
 
 @versionutils.deprecated(as_of=versionutils.deprecated.KILO,
-                         in_favor_of='oslo.middleware.CatchErrors')
+                         in_favor_of='oslo_middleware.CatchErrors')
 class CatchErrorsMiddleware(catch_errors.CatchErrors):
     pass
index d5d0bfc4c920a0295d6905f1ede94aa10f9e963a..8c6c11c47a46f7a8c400d2cd66a597053b20ee65 100644 (file)
@@ -12,7 +12,7 @@
 
 """Compatibility shim for Kilo, while operators migrate to oslo.middleware."""
 
-from oslo.middleware import request_id
+from oslo_middleware import request_id
 
 from cinder.openstack.common import versionutils
 
@@ -22,6 +22,6 @@ HTTP_RESP_HEADER_REQUEST_ID = 'x-openstack-request-id'
 
 
 @versionutils.deprecated(as_of=versionutils.deprecated.KILO,
-                         in_favor_of='oslo.middleware.RequestId')
+                         in_favor_of='oslo_middleware.RequestId')
 class RequestIdMiddleware(request_id.RequestId):
     pass
index bf602d7d4c9548e5f160eac31a0a00d57cc5e466..fbcde10191f7e3214e2109002dc06e1c2092ee59 100644 (file)
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from oslo_middleware import request_id
 import webob
 
 import cinder.api.middleware.auth
-from cinder.openstack.common.middleware import request_id
 from cinder import test
 
 
diff --git a/cinder/tests/api/middleware/test_sizelimit.py b/cinder/tests/api/middleware/test_sizelimit.py
deleted file mode 100644 (file)
index 3b0a778..0000000
+++ /dev/null
@@ -1,102 +0,0 @@
-# Copyright (c) 2012 OpenStack Foundation
-#
-#    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 cinder.api.middleware import sizelimit
-from cinder 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)
index ba922d5f81470a775842eccd4af3c4f306b49a9f..b2822b044db0b9ef9e8ccede69694eb4c163cbd0 100644 (file)
@@ -21,7 +21,7 @@ keystone = request_id faultwrap sizelimit osprofiler authtoken keystonecontext a
 keystone_nolimit = request_id faultwrap sizelimit osprofiler authtoken keystonecontext apiv2
 
 [filter:request_id]
-paste.filter_factory = cinder.openstack.common.middleware.request_id:RequestIdMiddleware.factory
+paste.filter_factory = oslo_middleware.request_id:RequestId.factory
 
 [filter:faultwrap]
 paste.filter_factory = cinder.api.middleware.fault:FaultWrapper.factory
index 607349893a881cf90821ebe0b808bb6e4d5ccb77..c426d3aa80d441f9391b625f4e33700c9d57c5b6 100644 (file)
@@ -18,6 +18,7 @@ oslo.context>=0.1.0
 oslo.db>=1.4.1  # Apache-2.0
 oslo.log>=0.4.0  # Apache-2.0
 oslo.messaging>=1.6.0  # Apache-2.0
+oslo.middleware>=0.3.0 # Apache-2.0
 oslo.rootwrap>=1.5.0  # Apache-2.0
 oslo.serialization>=1.2.0               # Apache-2.0
 oslo.utils>=1.2.0                       # Apache-2.0
index f6f0bf6aba9278ff963f6903a49f47959f914ded..36b6f18b080acf5f9aab8493853b3c30a699538f 100644 (file)
--- a/setup.cfg
+++ b/setup.cfg
@@ -59,6 +59,10 @@ oslo_messaging.notify.drivers =
     cinder.openstack.common.notifier.rpc_notifier2 = oslo_messaging.notify._impl_messaging:MessagingV2Driver
     cinder.openstack.common.notifier.rpc_notifier = oslo_messaging.notify._impl_messaging:MessagingDriver
     cinder.openstack.common.notifier.test_notifier = oslo_messaging.notify._impl_test:TestDriver
+# These are for backwards compatibility with Juno middleware configurations
+oslo_middleware =
+    cinder.api.middleware.sizelimit = oslo_middleware.sizelimit
+    cinder.openstack.common.middleware.request_id = oslo_middleware.request_id
 
 cinder.database.migration_backend =
     sqlalchemy = oslo_db.sqlalchemy.migration