Projet

Général

Profil

0002-debian-add-InternalIpMiddleware-29149.patch

Benjamin Dauvergne, 21 avril 2020 00:27

Télécharger (5,67 ko)

Voir les différences:

Subject: [PATCH 2/2] 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 ++++++++++++++++++++++++++++
 hobo/{agent => }/test_urls.py        |  3 ++
 tests_multitenant/conftest.py        | 10 ++++++
 tests_multitenant/test_middleware.py | 18 ++++++++++
 5 files changed, 82 insertions(+)
 create mode 100644 hobo/middleware/debug.py
 rename hobo/{agent => }/test_urls.py (81%)
debian/debian_config_common.py
385 385
    MIDDLEWARE_CLASSES = (
386 386
        'hobo.middleware.utils.StoreRequestMiddleware',
387 387
        'hobo.middleware.xforwardedfor.XForwardedForMiddleware',
388
        'hobo.middleware.debug.InternalIPMiddleware',
388 389
    ) + MIDDLEWARE_CLASSES
389 390

  
390 391
    MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + (
......
393 394
    MIDDLEWARE = (
394 395
        'hobo.middleware.utils.StoreRequestMiddleware',
395 396
        'hobo.middleware.xforwardedfor.XForwardedForMiddleware',
397
        'hobo.middleware.debug.InternalIPMiddleware',
396 398
    ) + MIDDLEWARE
397 399

  
398 400
    MIDDLEWARE = MIDDLEWARE + (
hobo/middleware/debug.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2019  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.conf import settings
18

  
19

  
20
class InternalIPMiddleware(object):
21
    def __init__(self, get_response=None):
22
        self.get_response = get_response
23

  
24
    def process_request(self, request):
25
        internal_ips = getattr(settings, 'INTERNAL_IPS', [])
26
        try:
27
            if request.META['REMOTE_ADDR'] in internal_ips:
28
                self.old_value = settings.DEBUG
29
                settings.DEBUG = True
30
        except TypeError:
31
            pass
32
        return None
33

  
34
    def process_response(self, request, response):
35
        if hasattr(self, 'old_value'):
36
            settings.DEBUG = self.old_value
37
            del self.old_value
38
        return response
39

  
40
    def __call__(self, request):
41
        old_value = settings.DEBUG
42
        internal_ips = getattr(settings, 'INTERNAL_IPS', [])
43
        set_debug = request.META['REMOTE_ADDR'] in internal_ips
44
        try:
45
            if set_debug:
46
                settings.DEBUG = True
47
            return self.get_response(request)
48
        finally:
49
            settings.DEBUG = old_value
hobo/test_urls.py
3 3
from django.conf.urls import url
4 4
from django.http import HttpResponse
5 5

  
6

  
6 7
def helloworld(request):
7 8
    logging.getLogger(__name__).error('wat!')
9
    if 'raise' in request.GET:
10
        raise Exception('wat!')
8 11
    return HttpResponse('Hello world %s' % request.META['REMOTE_ADDR'])
9 12

  
10 13
urlpatterns = [
tests_multitenant/conftest.py
5 5

  
6 6
import pytest
7 7

  
8
import django_webtest
9

  
8 10

  
9 11
@pytest.fixture(scope='function')
10 12
def tenants(transactional_db, request, settings):
......
92 94
        shutil.rmtree(base)
93 95
    request.addfinalizer(fin)
94 96
    return tenants
97

  
98

  
99
@pytest.fixture
100
def app(request):
101
    wtm = django_webtest.WebTestMixin()
102
    wtm._patch_settings()
103
    yield django_webtest.DjangoTestApp()
104
    wtm._unpatch_settings()
tests_multitenant/test_middleware.py
1
from __future__ import unicode_literals
2
import pytest
3

  
1 4
from hobo.multitenant.middleware import TenantMiddleware
2 5

  
3 6

  
......
13 16
    # and it matches the suffix
14 17
    assert shortened[-20:] == ('x' * 20)
15 18

  
19

  
20
@pytest.mark.urls('hobo.test_urls')
21
def test_internalipmiddleware(app, tenants, settings):
22
    settings.INTERNAL_IPS = []
23
    settings.ALLOWED_HOSTS = ['*']
24
    settings.DEBUG_PROPAGATE_EXCEPTIONS = False
25
    app.get('/?raise', status=404)
26
    response = app.get('/?raise', status=500, extra_environ={'HTTP_HOST': tenants[0].domain_url})
27
    assert response.text == '<h1>Server Error (500)</h1>'
28

  
29
    settings.INTERNAL_IPS = ['127.0.0.1']
30

  
31
    response = app.get('/?raise', status=500, extra_environ={'HTTP_HOST': tenants[0].domain_url})
32
    assert 'You\'re seeing this error because you have' in response.text
33

  
16
-