From c78b156723cb4895be2ee0a55e4b9d9726b5b1c1 Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Thu, 20 Aug 2015 07:52:59 +0200 Subject: Restore backward compat of paste factory Some application inherits from our middleware class When we homogenize the signature and configuration handling of all middlewares we break them. This change fixes that. Closes-bug: #1486735 Change-Id: I40c3d59110c6f8c5a1b3d3ccc734dc441069b025 --- oslo_middleware/base.py | 24 ++++++++++++++++++--- oslo_middleware/catch_errors.py | 2 +- oslo_middleware/correlation_id.py | 2 +- oslo_middleware/cors.py | 6 +++--- oslo_middleware/debug.py | 2 +- oslo_middleware/healthcheck/__init__.py | 2 +- oslo_middleware/request_id.py | 2 +- oslo_middleware/sizelimit.py | 2 +- oslo_middleware/ssl.py | 6 +++--- oslo_middleware/tests/test_base.py | 38 +++++++++++++++++++++------------ 10 files changed, 57 insertions(+), 29 deletions(-) diff --git a/oslo_middleware/base.py b/oslo_middleware/base.py index 475dc83..fd45d9a 100644 --- a/oslo_middleware/base.py +++ b/oslo_middleware/base.py @@ -21,7 +21,7 @@ import webob.dec from oslo_config import cfg -class Middleware(object): +class ConfigurableMiddleware(object): """Base WSGI middleware wrapper. These classes require an application to be initialized that will be called @@ -41,14 +41,19 @@ class Middleware(object): return middleware_filter def __init__(self, application, conf=None): + """Base middleware constructor + + :param conf: a dict of options or a cfg.ConfigOpts object + """ self.application = application + # NOTE(sileht): If the configuration come from oslo.config # just use it. if isinstance(conf, cfg.ConfigOpts): - self.conf = [] + self.conf = {} self.oslo_conf = conf else: - self.conf = conf or [] + self.conf = conf or {} if "oslo_config_project" in self.conf: if 'oslo_config_file' in self.conf: default_config_files = [self.conf['oslo_config_file']] @@ -96,3 +101,16 @@ class Middleware(object): if 'request' in args: return self.process_response(response, request=req) return self.process_response(response) + + +class Middleware(ConfigurableMiddleware): + """Legacy base WSGI middleware wrapper. + + Legacy interface that doesn't pass configuration options + to the middleware when it's loaded via paste.deploy. + """ + + @classmethod + def factory(cls, global_conf, **local_conf): + """Factory method for paste.deploy.""" + return cls diff --git a/oslo_middleware/catch_errors.py b/oslo_middleware/catch_errors.py index 5004859..f2c4d69 100644 --- a/oslo_middleware/catch_errors.py +++ b/oslo_middleware/catch_errors.py @@ -25,7 +25,7 @@ from oslo_middleware import base LOG = logging.getLogger(__name__) -class CatchErrors(base.Middleware): +class CatchErrors(base.ConfigurableMiddleware): """Middleware that provides high-level error handling. It catches all exceptions from subsequent applications in WSGI pipeline diff --git a/oslo_middleware/correlation_id.py b/oslo_middleware/correlation_id.py index 54d62ec..773dcba 100644 --- a/oslo_middleware/correlation_id.py +++ b/oslo_middleware/correlation_id.py @@ -18,7 +18,7 @@ import uuid from oslo_middleware import base -class CorrelationId(base.Middleware): +class CorrelationId(base.ConfigurableMiddleware): "Middleware that attaches a correlation id to WSGI request" def process_request(self, req): diff --git a/oslo_middleware/cors.py b/oslo_middleware/cors.py index 69c23cf..f55c672 100644 --- a/oslo_middleware/cors.py +++ b/oslo_middleware/cors.py @@ -53,7 +53,7 @@ CORS_OPTS = [ ] -class CORS(base.Middleware): +class CORS(base.ConfigurableMiddleware): """CORS Middleware. This middleware allows a WSGI app to serve CORS headers for multiple @@ -71,8 +71,8 @@ class CORS(base.Middleware): 'Pragma' ] - def __init__(self, application, conf=None): - super(CORS, self).__init__(application, conf) + def __init__(self, application, *args, **kwargs): + super(CORS, self).__init__(application, *args, **kwargs) # Begin constructing our configuration hash. self.allowed_origins = {} self._init_conf() diff --git a/oslo_middleware/debug.py b/oslo_middleware/debug.py index bdbf539..fb2fc82 100644 --- a/oslo_middleware/debug.py +++ b/oslo_middleware/debug.py @@ -25,7 +25,7 @@ import webob.dec from oslo_middleware import base -class Debug(base.Middleware): +class Debug(base.ConfigurableMiddleware): """Helper class that returns debug information. Can be inserted into any WSGI application chain to get information about diff --git a/oslo_middleware/healthcheck/__init__.py b/oslo_middleware/healthcheck/__init__.py index 8c89564..bdbbd24 100644 --- a/oslo_middleware/healthcheck/__init__.py +++ b/oslo_middleware/healthcheck/__init__.py @@ -21,7 +21,7 @@ import webob.response from oslo_middleware import base -class Healthcheck(base.Middleware): +class Healthcheck(base.ConfigurableMiddleware): """Healthcheck middleware used for monitoring. If the path is /healthcheck, it will respond 200 with "OK" as the body. diff --git a/oslo_middleware/request_id.py b/oslo_middleware/request_id.py index be9ae49..31a433b 100644 --- a/oslo_middleware/request_id.py +++ b/oslo_middleware/request_id.py @@ -23,7 +23,7 @@ ENV_REQUEST_ID = 'openstack.request_id' HTTP_RESP_HEADER_REQUEST_ID = 'x-openstack-request-id' -class RequestId(base.Middleware): +class RequestId(base.ConfigurableMiddleware): """Middleware that ensures request ID. It ensures to assign request ID for each API request and set it to diff --git a/oslo_middleware/sizelimit.py b/oslo_middleware/sizelimit.py index bbe8b6d..bba9886 100644 --- a/oslo_middleware/sizelimit.py +++ b/oslo_middleware/sizelimit.py @@ -75,7 +75,7 @@ class LimitingReader(object): return result -class RequestBodySizeLimiter(base.Middleware): +class RequestBodySizeLimiter(base.ConfigurableMiddleware): """Limit the size of incoming requests.""" def __init__(self, application, conf=None): diff --git a/oslo_middleware/ssl.py b/oslo_middleware/ssl.py index dec22ad..8f0f01c 100644 --- a/oslo_middleware/ssl.py +++ b/oslo_middleware/ssl.py @@ -23,7 +23,7 @@ OPTS = [ ] -class SSLMiddleware(base.Middleware): +class SSLMiddleware(base.ConfigurableMiddleware): """SSL termination proxies middleware. This middleware overloads wsgi.url_scheme with the one provided in @@ -31,8 +31,8 @@ class SSLMiddleware(base.Middleware): termination proxy. """ - def __init__(self, application, conf=None): - super(SSLMiddleware, self).__init__(application, conf) + def __init__(self, application, *args, **kwargs): + super(SSLMiddleware, self).__init__(application, *args, **kwargs) self.oslo_conf.register_opts(OPTS, group='oslo_middleware') def process_request(self, req): diff --git a/oslo_middleware/tests/test_base.py b/oslo_middleware/tests/test_base.py index 7124a49..ad48da5 100644 --- a/oslo_middleware/tests/test_base.py +++ b/oslo_middleware/tests/test_base.py @@ -14,28 +14,25 @@ import webob +from oslo_middleware.base import ConfigurableMiddleware from oslo_middleware.base import Middleware from oslotest.base import BaseTestCase +@webob.dec.wsgify +def application(req): + return 'Hello, World!!!' + + class TestBase(BaseTestCase): """Test the base middleware class.""" - def setUp(self): - """Setup the tests.""" - super(BaseTestCase, self).setUp() - def test_extend_with_request(self): """Assert that a newer middleware behaves as appropriate. This tests makes sure that the request is passed to the middleware's implementation. """ - # Create an application. - @webob.dec.wsgify - def application(req): - return 'Hello, World!!!' - # Bootstrap the application self.application = RequestBase(application) @@ -52,11 +49,6 @@ class TestBase(BaseTestCase): middleware's implementation, and that there are no other expected errors. """ - # Create an application. - @webob.dec.wsgify - def application(req): - return 'Hello, World!!!' - # Bootstrap the application self.application = NoRequestBase(application) @@ -66,6 +58,16 @@ class TestBase(BaseTestCase): self.assertTrue(self.application.called_without_request) + def test_paste_deploy_legacy(self): + app = LegacyMiddlewareTest.factory( + {'global': True}, local=True)(application) + self.assertEqual(app.conf, {}) + + def test_paste_deploy_configurable(self): + app = ConfigurableMiddlewareTest.factory( + {'global': True}, local=True)(application) + self.assertEqual(app.conf, {'global': True, 'local': True}) + class NoRequestBase(Middleware): """Test middleware, implements old model.""" @@ -79,3 +81,11 @@ class RequestBase(Middleware): def process_response(self, response, request): self.called_with_request = True return response + + +class ConfigurableMiddlewareTest(ConfigurableMiddleware): + pass + + +class LegacyMiddlewareTest(Middleware): + pass -- cgit v1.2.1