From 2b9a264cc98fe91329412c3ff7c713e63638441e Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 16 May 2019 17:43:44 +0200 Subject: [PATCH 2/3] debian: add InternalIpMiddleware (#29149) It sets DEBUG=True when current request IP is in settings.INTERNAL_IPS. --- debian/debian_config_common.py | 2 ++ hobo/middleware/debug.py | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 hobo/middleware/debug.py diff --git a/debian/debian_config_common.py b/debian/debian_config_common.py index 0a0ece1..765422a 100644 --- a/debian/debian_config_common.py +++ b/debian/debian_config_common.py @@ -376,6 +376,7 @@ if 'MIDDLEWARE_CLASSES' in globals(): MIDDLEWARE_CLASSES = ( 'hobo.middleware.utils.StoreRequestMiddleware', 'hobo.middleware.xforwardedfor.XForwardedForMiddleware', + 'hobo.middleware.debug.InternalIPMiddleware', ) + MIDDLEWARE_CLASSES MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + ( @@ -384,6 +385,7 @@ else: MIDDLEWARE = ( 'hobo.middleware.utils.StoreRequestMiddleware', 'hobo.middleware.xforwardedfor.XForwardedForMiddleware', + 'hobo.middleware.debug.InternalIPMiddleware', ) + MIDDLEWARE MIDDLEWARE = MIDDLEWARE + ( diff --git a/hobo/middleware/debug.py b/hobo/middleware/debug.py new file mode 100644 index 0000000..d73e826 --- /dev/null +++ b/hobo/middleware/debug.py @@ -0,0 +1,49 @@ +# hobo - portal to configure and deploy applications +# Copyright (C) 2019 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.conf import settings + + +class InternalIPMiddleware(object): + def __init__(self, get_response=None): + self.get_response = get_response + + def process_request(self, request): + internal_ips = getattr(settings, 'INTERNAL_IPS', []) + try: + if request.META['REMOTE_ADDR'] in internal_ips: + self.old_value = settings.DEBUG + settings.DEBUG = True + except TypeError: + pass + return None + + def process_response(self, request, response): + if hasattr(self, 'old_value'): + settings.DEBUG = self.old_value + del self.old_value + return response + + def __call__(self, request): + old_value = settings.DEBUG + internal_ips = getattr(settings, 'INTERNAL_IPS', []) + set_debug = request.META['REMOTE_ADDR'] in internal_ips + try: + if set_debug: + settings.DEBUG = True + return self.get_response(request) + finally: + settings.DEBUG = old_value -- 2.24.0