debian: add InternalIpMiddleware (#29149)
It sets DEBUG=True when current request IP is in settings.INTERNAL_IPS.
This commit is contained in:
parent
afe4b98f64
commit
e1647d8f56
|
@ -323,11 +323,13 @@ if PROJECT_NAME != 'wcs':
|
|||
MIDDLEWARE_CLASSES = (
|
||||
'hobo.multitenant.middleware.TenantMiddleware',
|
||||
'hobo.middleware.CookiesSameSiteFixMiddleware',
|
||||
'hobo.middleware.debug.InternalIPMiddleware',
|
||||
) + MIDDLEWARE_CLASSES
|
||||
else:
|
||||
MIDDLEWARE = (
|
||||
'hobo.multitenant.middleware.TenantMiddleware',
|
||||
'hobo.middleware.CookiesSameSiteFixMiddleware',
|
||||
'hobo.middleware.debug.InternalIPMiddleware',
|
||||
) + MIDDLEWARE
|
||||
|
||||
DATABASES = {
|
||||
|
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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
|
|
@ -3,8 +3,11 @@ import logging
|
|||
from django.conf.urls import url
|
||||
from django.http import HttpResponse
|
||||
|
||||
|
||||
def helloworld(request):
|
||||
logging.getLogger(__name__).error('wat!')
|
||||
if 'raise' in request.GET:
|
||||
raise Exception('wat!')
|
||||
return HttpResponse('Hello world %s' % request.META['REMOTE_ADDR'])
|
||||
|
||||
urlpatterns = [
|
|
@ -5,6 +5,8 @@ import json
|
|||
|
||||
import pytest
|
||||
|
||||
import django_webtest
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def tenants(transactional_db, request, settings):
|
||||
|
@ -92,3 +94,11 @@ def tenants(transactional_db, request, settings):
|
|||
shutil.rmtree(base)
|
||||
request.addfinalizer(fin)
|
||||
return tenants
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(request):
|
||||
wtm = django_webtest.WebTestMixin()
|
||||
wtm._patch_settings()
|
||||
yield django_webtest.DjangoTestApp()
|
||||
wtm._unpatch_settings()
|
||||
|
|
|
@ -67,7 +67,7 @@ DATABASES['default']['TEST'] = {
|
|||
TENANT_APPS = ('django.contrib.auth', 'django.contrib.sessions', 'django.contrib.contenttypes',
|
||||
'hobo.agent.common', 'mellon')
|
||||
|
||||
ROOT_URLCONF = 'hobo.agent.test_urls'
|
||||
ROOT_URLCONF = 'hobo.test_urls'
|
||||
CACHES = {
|
||||
'default': {
|
||||
'BACKEND': 'hobo.multitenant.cache.TenantCache',
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
from __future__ import unicode_literals
|
||||
import pytest
|
||||
|
||||
from hobo.multitenant.middleware import TenantMiddleware
|
||||
|
||||
|
||||
|
@ -13,3 +16,17 @@ def test_hostname2schema():
|
|||
# and it matches the suffix
|
||||
assert shortened[-20:] == ('x' * 20)
|
||||
|
||||
|
||||
def test_internalipmiddleware(app, tenants, settings):
|
||||
settings.INTERNAL_IPS = []
|
||||
settings.ALLOWED_HOSTS = ['*']
|
||||
settings.DEBUG_PROPAGATE_EXCEPTIONS = False
|
||||
app.get('/?raise', status=404)
|
||||
response = app.get('/?raise', status=500, extra_environ={'HTTP_HOST': tenants[0].domain_url})
|
||||
assert response.text == '<h1>Server Error (500)</h1>'
|
||||
|
||||
settings.INTERNAL_IPS = ['127.0.0.1']
|
||||
|
||||
response = app.get('/?raise', status=500, extra_environ={'HTTP_HOST': tenants[0].domain_url})
|
||||
assert 'You\'re seeing this error because you have' in response.text
|
||||
|
||||
|
|
Loading…
Reference in New Issue