Revision 6d153302 flowspec/forms.py
b/flowspec/forms.py | ||
---|---|---|
3 | 3 |
from django.utils.translation import ugettext as _ |
4 | 4 |
from django.utils.translation import ugettext_lazy |
5 | 5 |
from django.template.defaultfilters import filesizeformat |
6 |
from flowspy.flowspec.models import *
|
|
6 |
from flowspy.flowspec.models import * |
|
7 | 7 |
from ipaddr import * |
8 |
from django.core.urlresolvers import reverse |
|
8 | 9 |
from django.contrib.auth.models import User |
10 |
from django.db.models import Avg, Max, Min, Count |
|
11 |
|
|
9 | 12 |
|
10 | 13 |
class RouteForm(forms.ModelForm): |
11 | 14 |
# name = forms.CharField(help_text=ugettext_lazy("A unique route name," |
... | ... | |
39 | 42 |
raise forms.ValidationError('Invalid network address format') |
40 | 43 |
|
41 | 44 |
def clean(self): |
45 |
name = self.cleaned_data.get('name', None) |
|
42 | 46 |
source = self.cleaned_data.get('source', None) |
43 | 47 |
sourceports = self.cleaned_data.get('sourceport', None) |
44 | 48 |
ports = self.cleaned_data.get('port', None) |
49 |
then = self.cleaned_data.get('then', None) |
|
45 | 50 |
destination = self.cleaned_data.get('destination', None) |
46 | 51 |
destinationports = self.cleaned_data.get('destinationport', None) |
47 | 52 |
user = self.cleaned_data.get('applier', None) |
48 |
networks = user.get_profile().peer.networks.all() |
|
53 |
peer = user.get_profile().peer |
|
54 |
networks = peer.networks.all() |
|
49 | 55 |
mynetwork = False |
56 |
route_pk_list = [] |
|
57 |
|
|
50 | 58 |
if destination: |
51 | 59 |
for network in networks: |
52 | 60 |
net = IPNetwork(network.network) |
... | ... | |
64 | 72 |
raise forms.ValidationError('Once destination port is matched, destination has to be filled as well. Either deselect destination port or fill destination address') |
65 | 73 |
if not (source or sourceports or ports or destination or destinationports): |
66 | 74 |
raise forms.ValidationError('Fill at least a Route Match Condition') |
75 |
existing_routes = Route.objects.exclude(status='EXPIRED').exclude(status='PENDING').exclude(status='ERROR').exclude(status='ADMININACTIVE') |
|
76 |
existing_routes = existing_routes.filter(applier__userprofile__peer=peer) |
|
77 |
if source: |
|
78 |
source = IPNetwork(source).compressed |
|
79 |
existing_routes = existing_routes.filter(source=source) |
|
80 |
else: |
|
81 |
existing_routes = existing_routes.filter(source=None) |
|
82 |
if sourceports: |
|
83 |
route_pk_list=get_matchingport_route_pks(sourceports, existing_routes) |
|
84 |
if route_pk_list: |
|
85 |
existing_routes = existing_routes.filter(pk__in=route_pk_list) |
|
86 |
else: |
|
87 |
existing_routes = existing_routes.filter(sourceport=None) |
|
88 |
if destinationports: |
|
89 |
route_pk_list=get_matchingport_route_pks(destinationports, existing_routes) |
|
90 |
if route_pk_list: |
|
91 |
existing_routes = existing_routes.filter(pk__in=route_pk_list) |
|
92 |
else: |
|
93 |
existing_routes = existing_routes.filter(destinationport=None) |
|
94 |
if ports: |
|
95 |
route_pk_list=get_matchingport_route_pks(ports, existing_routes) |
|
96 |
if route_pk_list: |
|
97 |
existing_routes = existing_routes.filter(pk__in=route_pk_list) |
|
98 |
else: |
|
99 |
existing_routes = existing_routes.filter(port=None) |
|
100 |
|
|
101 |
for route in existing_routes: |
|
102 |
if name != route.name: |
|
103 |
existing_url = reverse('edit-route', args=[route.name]) |
|
104 |
if IPNetwork(destination) in IPNetwork(route.destination): |
|
105 |
raise forms.ValidationError('There is an exact %s rule, %s whose destination (%s) is supernet of (or the same as) network (%s).<br>To avoid overlapping try editing rule <a href=\'%s\'>%s</a>' %(route.status, route.name, route.destination, destination, existing_url, route.name)) |
|
106 |
if IPNetwork(route.destination) in IPNetwork(destination): |
|
107 |
raise forms.ValidationError('There is an exact %s rule, %s whose destination network (%s) belongs to the destination network %s.<br>To avoid overlapping try editing rule <a href=\'%s\'>%s</a>' %(route.status, route.name, route.destination, destination, existing_url, route.name)) |
|
108 |
|
|
109 |
|
|
67 | 110 |
return self.cleaned_data |
68 | 111 |
|
69 | 112 |
class ThenPlainForm(forms.ModelForm): |
... | ... | |
106 | 149 |
raise forms.ValidationError('Port should be an integer') |
107 | 150 |
else: |
108 | 151 |
raise forms.ValidationError('Cannot be empty') |
152 |
|
|
153 |
def value_list_to_list(valuelist): |
|
154 |
vl = [] |
|
155 |
for val in valuelist: |
|
156 |
vl.append(val[0]) |
|
157 |
return vl |
|
158 |
|
|
159 |
def get_matchingport_route_pks(portlist, routes): |
|
160 |
route_pk_list = [] |
|
161 |
ports_value_list = value_list_to_list(portlist.values_list('port').order_by('port')) |
|
162 |
for route in routes: |
|
163 |
rsp = value_list_to_list(route.destinationport.all().values_list('port').order_by('port')) |
|
164 |
if rsp and rsp == ports_value_list: |
|
165 |
route_pk_list.append(route.pk) |
|
166 |
return route_pk_list |
Also available in: Unified diff