Projet

Général

Profil

0001-environment-add-network-checks-on-service-url-35341.patch

Valentin Deniaud, 06 janvier 2020 17:15

Télécharger (5,32 ko)

Voir les différences:

Subject: [PATCH] environment: add network checks on service url (#35341)

 hobo/environment/forms.py                    |  6 +++++
 hobo/environment/management/commands/cook.py | 13 +++++------
 hobo/environment/validators.py               | 20 +++++++++++++++++
 tests/test_environment.py                    | 23 +++++++++++++++++++-
 4 files changed, 54 insertions(+), 8 deletions(-)
 create mode 100644 hobo/environment/validators.py
hobo/environment/forms.py
25 25
from .models import (Authentic, Wcs, Passerelle, Variable, Combo, Fargo, Welco,
26 26
                     MandayeJS, Chrono, Corbo, BiJoe, Hobo)
27 27
from .utils import get_variable
28
from .validators import validate_service_url
28 29

  
29 30
EXCLUDED_FIELDS = ('last_operational_check_timestamp',
30 31
                   'last_operational_success_timestamp', 'secret_key',
......
60 61
        service_id = self.Meta.model.Extra.service_id
61 62
        return settings.SERVICE_TEMPLATES.get(service_id, [])
62 63

  
64
    def clean_base_url(self):
65
        url = self.cleaned_data['base_url']
66
        validate_service_url(url)
67
        return url
68

  
63 69
    def save(self, commit=True):
64 70
        if not self.instance.slug:
65 71
            base_slug = slugify(self.instance.title)
hobo/environment/management/commands/cook.py
37 37
from hobo.multitenant.middleware import TenantMiddleware
38 38
from hobo.environment.models import (AVAILABLE_SERVICES, Authentic, Wcs, Hobo,
39 39
        Passerelle, Combo, Fargo, Welco, MandayeJS, Chrono, Corbo, BiJoe,
40
        Variable, ServiceBase)
40
        Variable)
41
from hobo.environment.validators import validate_service_url
41 42
from hobo.deploy.signals import notify_agents
42 43
from hobo.theme.utils import set_theme
43 44
from hobo.profile.models import AttributeDefinition
......
298 299
        if not hasattr(self, action.replace('-', '_')):
299 300
            raise CommandError('Error: Unknown action %s' % action)
300 301
        if 'url' in action_args.keys():
301
            url = action_args['url']
302
            service = ServiceBase(title='dummy', base_url=url)
303
            if not service.is_resolvable():
304
                raise CommandError('Error: %s is not resolvable' % url)
305
            if not service.has_valid_certificate():
306
                raise CommandError('Error: %s has no valid certificate' % url)
302
            try:
303
                validate_service_url(action_args['url'])
304
            except ValidationError as e:
305
                raise CommandError(e)
hobo/environment/validators.py
1
from django.core.exceptions import ValidationError
2
from django.utils.translation import gettext_lazy as _
3

  
4
from hobo.environment.models import ServiceBase
5

  
6

  
7
def validate_service_url(url):
8
    service = ServiceBase(title='dummy', base_url=url)
9
    if not service.is_resolvable():
10
        raise ValidationError(
11
            _('Error: %(url)s is not resolvable'),
12
            code='not-resolvable',
13
            params={'url': url}
14
        )
15
    if not service.has_valid_certificate():
16
        raise ValidationError(
17
            _('Error: %(url)s has no valid certificate'),
18
            code='invalid-certificate',
19
            params={'url': url}
20
        )
tests/test_environment.py
5 5
from django.core.exceptions import ValidationError
6 6
from django.utils import timezone
7 7

  
8
from hobo.environment.models import AVAILABLE_SERVICES, Combo, Passerelle
8
from hobo.environment.models import AVAILABLE_SERVICES, Combo, Passerelle, ServiceBase
9 9

  
10 10
pytestmark = pytest.mark.django_db
11 11

  
......
84 84
    app = login(app)
85 85
    response = app.get('/sites/new-combo')
86 86
    assert 'value="http://portal-test.example.net"' in response.text
87

  
88

  
89
def test_service_creation_url_validation(app, admin_user, monkeypatch):
90
    app = login(app)
91
    response = app.get('/sites/new-combo')
92
    form = response.form
93
    form['title'] = "test"
94
    form['base_url'] = "http://portal-test.example.net"
95
    response = form.submit()
96
    assert 'not resolvable' in response
97

  
98
    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
99
    form = response.form
100
    response = form.submit()
101
    assert 'no valid certificate' in response
102

  
103
    assert not Combo.objects.exists()
104
    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
105
    form = response.form
106
    response = form.submit()
107
    assert Combo.objects.exists()
87
-