Update Makefile to be more generic and more version number friendly
[flowspy] / flowspec / models.py
1 # -*- coding: utf-8 -*- vim:encoding=utf-8:
2 # vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
3
4 from django.db import models
5 from django.conf import settings
6 from django.contrib.auth.models import User
7 from utils import proxy as PR
8 from ipaddr import *
9 import datetime
10 import logging
11 from flowspec.tasks import *
12 from time import sleep
13
14 import beanstalkc
15 from flowspy.utils.randomizer import id_generator as id_gen
16
17
18 FORMAT = '%(asctime)s %(levelname)s: %(message)s'
19 logging.basicConfig(format=FORMAT)
20 logger = logging.getLogger(__name__)
21 logger.setLevel(logging.DEBUG)
22
23
24 FRAGMENT_CODES = (
25     ("dont-fragment", "Don't fragment"),
26     ("first-fragment", "First fragment"),
27     ("is-fragment", "Is fragment"),
28     ("last-fragment", "Last fragment"),
29     ("not-a-fragment", "Not a fragment")
30 )
31
32 THEN_CHOICES = (
33     ("accept", "Accept"),
34     ("discard", "Discard"),
35     ("community", "Community"),
36     ("next-term", "Next term"),
37     ("routing-instance", "Routing Instance"),
38     ("rate-limit", "Rate limit"),
39     ("sample", "Sample")                
40 )
41
42 ROUTE_STATES = (
43     ("ACTIVE", "ACTIVE"),
44     ("ERROR", "ERROR"),
45     ("EXPIRED", "EXPIRED"),
46     ("PENDING", "PENDING"),
47     ("OUTOFSYNC", "OUTOFSYNC"),
48     ("INACTIVE", "INACTIVE"),
49     ("ADMININACTIVE", "ADMININACTIVE"),           
50 )
51
52
53 def days_offset(): return datetime.date.today() + datetime.timedelta(days = settings.EXPIRATION_DAYS_OFFSET)
54     
55 class MatchPort(models.Model):
56     port = models.CharField(max_length=24, unique=True)
57     def __unicode__(self):
58         return self.port
59     class Meta:
60         db_table = u'match_port'    
61
62 class MatchDscp(models.Model):
63     dscp = models.CharField(max_length=24)
64     def __unicode__(self):
65         return self.dscp
66     class Meta:
67         db_table = u'match_dscp'
68
69    
70 class ThenAction(models.Model):
71     action = models.CharField(max_length=60, choices=THEN_CHOICES, verbose_name="Action")
72     action_value = models.CharField(max_length=255, blank=True, null=True, verbose_name="Action Value")
73     def __unicode__(self):
74         ret = "%s:%s" %(self.action, self.action_value)
75         return ret.rstrip(":")
76     class Meta:
77         db_table = u'then_action'
78         ordering = ['action', 'action_value']
79         unique_together = ("action", "action_value")
80
81 class Route(models.Model):
82     name = models.SlugField(max_length=128)
83     applier = models.ForeignKey(User, blank=True, null=True)
84     source = models.CharField(max_length=32, help_text=u"Network address. Use address/CIDR notation", verbose_name="Source Address")
85     sourceport = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchSourcePort", verbose_name="Source Port")
86     destination = models.CharField(max_length=32, help_text=u"Network address. Use address/CIDR notation", verbose_name="Destination Address")
87     destinationport = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchDestinationPort", verbose_name="Destination Port")
88     port = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchPort", verbose_name="Port" )
89     dscp = models.ManyToManyField(MatchDscp, blank=True, null=True, verbose_name="DSCP")
90     fragmenttype = models.CharField(max_length=20, choices=FRAGMENT_CODES, blank=True, null=True, verbose_name="Fragment Type")
91     icmpcode = models.CharField(max_length=32, blank=True, null=True, verbose_name="ICMP Code")
92     icmptype = models.CharField(max_length=32, blank=True, null=True, verbose_name="ICMP Type")
93     packetlength = models.IntegerField(blank=True, null=True, verbose_name="Packet Length")
94     protocol = models.CharField(max_length=32, blank=True, null=True, verbose_name="Protocol")
95     tcpflag = models.CharField(max_length=128, blank=True, null=True, verbose_name="TCP flag")
96     then = models.ManyToManyField(ThenAction, verbose_name="Then")
97     filed = models.DateTimeField(auto_now_add=True)
98     last_updated = models.DateTimeField(auto_now=True)
99     status = models.CharField(max_length=20, choices=ROUTE_STATES, blank=True, null=True, verbose_name="Status", default="PENDING")
100 #    is_online = models.BooleanField(default=False)
101 #    is_active = models.BooleanField(default=False)
102     expires = models.DateField(default=days_offset)
103     response = models.CharField(max_length=512, blank=True, null=True)
104     comments = models.TextField(null=True, blank=True, verbose_name="Comments")
105
106     
107     def __unicode__(self):
108         return self.name
109     
110     class Meta:
111         db_table = u'route'
112         verbose_name = "Rule"
113         verbose_name_plural = "Rules"
114     
115     def save(self, *args, **kwargs):
116         if not self.pk:
117             hash = id_gen()
118             self.name = "%s_%s" %(self.name, hash)
119         super(Route, self).save(*args, **kwargs) # Call the "real" save() method.
120
121         
122     def clean(self, *args, **kwargs):
123         from django.core.exceptions import ValidationError
124         if self.destination:
125             try:
126                 address = IPNetwork(self.destination)
127                 self.destination = address.exploded
128             except Exception:
129                 raise ValidationError('Invalid network address format at Destination Field')
130         if self.source:
131             try:
132                 address = IPNetwork(self.source)
133                 self.source = address.exploded
134             except Exception:
135                 raise ValidationError('Invalid network address format at Source Field')
136    
137     def commit_add(self, *args, **kwargs):
138         peer = self.applier.get_profile().peer.domain_name
139         send_message("[%s] Adding rule %s. Please wait..." %(self.applier.username, self.name), peer)
140         response = add.delay(self)
141         logger.info("Got add job id: %s" %response)
142         
143     def commit_edit(self, *args, **kwargs):
144         peer = self.applier.get_profile().peer.domain_name
145         send_message("[%s] Editing rule %s. Please wait..." %(self.applier.username, self.name), peer)
146         response = edit.delay(self)
147         logger.info("Got edit job id: %s" %response)
148
149     def commit_delete(self, *args, **kwargs):
150         reason_text = ''
151         reason = ''
152         if "reason" in kwargs:
153             reason = kwargs['reason']
154             reason_text = "Reason: %s. " %reason
155         peer = self.applier.get_profile().peer.domain_name
156         send_message("[%s] Suspending rule %s. %sPlease wait..." %(self.applier.username, self.name, reason_text), peer)
157         response = delete.delay(self, reason=reason)
158         logger.info("Got delete job id: %s" %response)
159
160     def has_expired(self):
161         today = datetime.date.today()
162         if today > self.expires:
163             return True
164         return False
165     
166     def check_sync(self):
167         if not self.is_synced():
168             self.status = "OUTOFSYNC"
169             self.save()
170     
171     def is_synced(self):
172         found = False
173         get_device = PR.Retriever()
174         device = get_device.fetch_device()
175         try:
176             routes = device.routing_options[0].routes
177         except Exception as e:
178             self.status = "EXPIRED"
179             self.save()
180             logger.error("No routing options on device. Exception: %s" %e)
181             return True
182         for route in routes:
183             if route.name == self.name:
184                 found = True
185                 logger.info('Found a matching rule name')
186                 devicematch = route.match
187                 try:
188                     assert(self.destination)
189                     assert(devicematch['destination'][0])
190                     if self.destination == devicematch['destination'][0]:
191                         found = found and True
192                         logger.info('Found a matching destination')
193                     else:
194                         found = False
195                         logger.info('Destination fields do not match')
196                 except:
197                     pass
198                 try:
199                     assert(self.source)
200                     assert(devicematch['source'][0])
201                     if self.source == devicematch['source'][0]:
202                         found = found and True
203                         logger.info('Found a matching source')
204                     else:
205                         found = False
206                         logger.info('Source fields do not match')
207                 except:
208                     pass
209                 try:
210                     assert(self.fragmenttype)
211                     assert(devicematch['fragment'][0])
212                     if self.fragmenttype == devicematch['fragment'][0]:
213                         found = found and True
214                         logger.info('Found a matching fragment type')
215                     else:
216                         found = False
217                         logger.info('Fragment type fields do not match')
218                 except:
219                     pass
220                 try:
221                     assert(self.icmpcode)
222                     assert(devicematch['icmp-code'][0])
223                     if self.icmpcode == devicematch['icmp-code'][0]:
224                         found = found and True
225                         logger.info('Found a matching icmp code')
226                     else:
227                         found = False
228                         logger.info('Icmp code fields do not match')
229                 except:
230                     pass
231                 try:
232                     assert(self.icmptype)
233                     assert(devicematch['icmp-type'][0])
234                     if self.icmptype == devicematch['icmp-type'][0]:
235                         found = found and True
236                         logger.info('Found a matching icmp type')
237                     else:
238                         found = False
239                         logger.info('Icmp type fields do not match')
240                 except:
241                     pass
242                 try:
243                     assert(self.protocol)
244                     assert(devicematch['protocol'][0])
245                     if self.protocol == devicematch['protocol'][0]:
246                         found = found and True
247                         logger.info('Found a matching protocol')
248                     else:
249                         found = False
250                         logger.info('Protocol fields do not match')
251                 except:
252                     pass
253                 if found and self.status != "ACTIVE":
254                     logger.error('Rule is applied on device but appears as offline')
255                     self.status = "ACTIVE"
256                     self.save()
257                     found = True
258             if self.status == "ADMININACTIVE" or self.status == "INACTIVE" or self.status == "EXPIRED":
259                 found = True
260         return found
261
262     def get_then(self):
263         ret = ''
264         then_statements = self.then.all()
265         for statement in then_statements:
266             if statement.action_value:
267                 ret = "%s %s:<strong>%s</strong><br/>" %(ret, statement.action, statement.action_value)
268             else: 
269                 ret = "%s %s<br>" %(ret, statement.action)
270         return ret.rstrip(',')
271     
272     get_then.short_description = 'Then statement'
273     get_then.allow_tags = True
274 #
275     def get_match(self):
276         ret = ''
277         if self.destination:
278             ret = '%s Dst Addr:<strong>%s</strong> <br/>' %(ret, self.destination)
279         if self.fragmenttype:
280             ret = "%s Fragment Type:<strong>%s</strong><br/>" %(ret, self.fragmenttype)
281         if self.icmpcode:
282             ret = "%s ICMP code:<strong>%s</strong><br/>" %(ret, self.icmpcode)
283         if self.icmptype:
284             ret = "%s ICMP Type:<strong>%s</strong><br/>" %(ret, self.icmptype)
285         if self.packetlength:
286             ret = "%s Packet Length:<strong>%s</strong><br/>" %(ret, self.packetlength)
287         if self.protocol:
288             ret = "%s Protocol:<strong>%s</strong><br/>" %(ret, self.protocol)
289         if self.source:
290             ret = "%s Src Addr:<strong>%s</strong> <br/>" %(ret, self.source)
291         if self.tcpflag:
292             ret = "%s TCP flag:<strong>%s</strong><br/>" %(ret, self.tcpflag)
293         if self.port:
294             for port in self.port.all():
295                     ret = ret + "Port:<strong>%s</strong> <br/>" %(port)
296         if self.destinationport:
297             for port in self.destinationport.all():
298                     ret = ret + "Dst Port:<strong>%s</strong> <br/>" %(port)
299         if self.sourceport:
300             for port in self.sourceport.all():
301                     ret = ret +"Src Port:<strong>%s</strong> <br/>" %(port)
302         if self.dscp:
303             for dscp in self.dscp.all():
304                     ret = ret + "%s Port:<strong>%s</strong> <br/>" %(ret, dscp)
305         return ret.rstrip('<br/>')
306         
307     get_match.short_description = 'Match statement'
308     get_match.allow_tags = True
309     
310     @property
311     def applier_peer(self):
312         try:
313             applier_peer = self.applier.get_profile().peer
314         except:
315             applier_peer = None
316         return applier_peer
317     
318     @property
319     def days_to_expire(self):
320         if self.status not in ['EXPIRED', 'ADMININACTIVE', 'ERROR', 'INACTIVE']:
321             expiration_days = (self.expires - datetime.date.today()).days
322             if expiration_days < settings.EXPIRATION_NOTIFY_DAYS:
323                 return "%s" %expiration_days
324             else:
325                 return False
326         else:
327             return False
328
329 def send_message(msg, user):
330 #    username = user.username
331     peer = user
332     b = beanstalkc.Connection()
333     b.use(settings.POLLS_TUBE)
334     tube_message = json.dumps({'message': str(msg), 'username':peer})
335     b.put(tube_message)
336     b.close()