2019-03-20 21:42:47 +00:00
|
|
|
"""passbook Application Security Gateway Forms"""
|
2019-03-22 09:55:26 +00:00
|
|
|
from urllib.parse import urlparse
|
2019-03-20 21:42:47 +00:00
|
|
|
|
|
|
|
from django import forms
|
|
|
|
from django.contrib.admin.widgets import FilteredSelectMultiple
|
2019-03-21 15:36:38 +00:00
|
|
|
from django.forms import ValidationError
|
2019-03-20 21:42:47 +00:00
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
|
|
|
from passbook.lib.fields import DynamicArrayField
|
2019-10-07 14:33:48 +00:00
|
|
|
from passbook.providers.app_gw.models import (ApplicationGatewayProvider,
|
|
|
|
RewriteRule)
|
2019-03-20 21:42:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ApplicationGatewayProviderForm(forms.ModelForm):
|
|
|
|
"""Security Gateway Provider form"""
|
|
|
|
|
2019-03-21 15:36:38 +00:00
|
|
|
def clean_server_name(self):
|
|
|
|
"""Check if server_name is in DB already, since
|
|
|
|
Postgres ArrayField doesn't suppport keys."""
|
|
|
|
current = self.cleaned_data.get('server_name')
|
|
|
|
if ApplicationGatewayProvider.objects \
|
|
|
|
.filter(server_name__overlap=current) \
|
|
|
|
.exclude(pk=self.instance.pk).exists():
|
2019-03-22 09:55:26 +00:00
|
|
|
raise ValidationError(_("Server Name already in use."))
|
2019-03-21 15:36:38 +00:00
|
|
|
return current
|
|
|
|
|
2019-03-22 09:55:26 +00:00
|
|
|
def clean_upstream(self):
|
|
|
|
"""Check that upstream begins with http(s)"""
|
|
|
|
for upstream in self.cleaned_data.get('upstream'):
|
|
|
|
_parsed_url = urlparse(upstream)
|
|
|
|
|
|
|
|
if _parsed_url.scheme not in ('http', 'https'):
|
|
|
|
raise ValidationError(_("URL Scheme must be either http or https"))
|
|
|
|
return self.cleaned_data.get('upstream')
|
|
|
|
|
2019-03-20 21:42:47 +00:00
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = ApplicationGatewayProvider
|
|
|
|
fields = ['server_name', 'upstream', 'enabled', 'authentication_header',
|
2019-03-21 15:21:51 +00:00
|
|
|
'default_content_type', 'upstream_ssl_verification', 'property_mappings']
|
2019-03-20 21:42:47 +00:00
|
|
|
widgets = {
|
|
|
|
'authentication_header': forms.TextInput(),
|
|
|
|
'default_content_type': forms.TextInput(),
|
|
|
|
'property_mappings': FilteredSelectMultiple(_('Property Mappings'), False)
|
|
|
|
}
|
|
|
|
field_classes = {
|
|
|
|
'server_name': DynamicArrayField,
|
|
|
|
'upstream': DynamicArrayField
|
|
|
|
}
|
|
|
|
labels = {
|
2019-03-21 15:21:51 +00:00
|
|
|
'upstream_ssl_verification': _('Verify upstream SSL Certificates?'),
|
|
|
|
'property_mappings': _('Rewrite Rules')
|
|
|
|
}
|
|
|
|
|
|
|
|
class RewriteRuleForm(forms.ModelForm):
|
|
|
|
"""Rewrite Rule Form"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = RewriteRule
|
|
|
|
fields = ['name', 'match', 'halt', 'replacement', 'redirect', 'conditions']
|
|
|
|
widgets = {
|
|
|
|
'name': forms.TextInput(),
|
|
|
|
'match': forms.TextInput(attrs={'data-is-monospace': True}),
|
|
|
|
'replacement': forms.TextInput(attrs={'data-is-monospace': True}),
|
|
|
|
'conditions': FilteredSelectMultiple(_('Conditions'), False)
|
2019-03-20 21:42:47 +00:00
|
|
|
}
|