1 # -*- coding: utf-8 -*- vim:encoding=utf-8:
2 # vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
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
11 from time import sleep
14 from flowspy.utils.randomizer import id_generator as id_gen
16 from flowspec.tasks import *
18 FORMAT = '%(asctime)s %(levelname)s: %(message)s'
19 logging.basicConfig(format=FORMAT)
20 logger = logging.getLogger(__name__)
21 logger.setLevel(logging.DEBUG)
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")
34 ("discard", "Discard"),
35 ("community", "Community"),
36 ("next-term", "Next term"),
37 ("routing-instance", "Routing Instance"),
38 ("rate-limit", "Rate limit"),
62 ("EXPIRED", "EXPIRED"),
63 ("PENDING", "PENDING"),
64 ("OUTOFSYNC", "OUTOFSYNC"),
65 ("INACTIVE", "INACTIVE"),
66 ("ADMININACTIVE", "ADMININACTIVE"),
70 def days_offset(): return datetime.date.today() + datetime.timedelta(days = settings.EXPIRATION_DAYS_OFFSET)
72 class MatchPort(models.Model):
73 port = models.CharField(max_length=24, unique=True)
74 def __unicode__(self):
77 db_table = u'match_port'
79 class MatchDscp(models.Model):
80 dscp = models.CharField(max_length=24)
81 def __unicode__(self):
84 db_table = u'match_dscp'
86 class MatchProtocol(models.Model):
87 protocol = models.CharField(max_length=24, unique=True)
88 def __unicode__(self):
91 db_table = u'match_protocol'
94 class ThenAction(models.Model):
95 action = models.CharField(max_length=60, choices=THEN_CHOICES, verbose_name="Action")
96 action_value = models.CharField(max_length=255, blank=True, null=True, verbose_name="Action Value")
97 def __unicode__(self):
98 ret = "%s:%s" %(self.action, self.action_value)
99 return ret.rstrip(":")
101 db_table = u'then_action'
102 ordering = ['action', 'action_value']
103 unique_together = ("action", "action_value")
105 class Route(models.Model):
106 name = models.SlugField(max_length=128)
107 applier = models.ForeignKey(User, blank=True, null=True)
108 source = models.CharField(max_length=32, help_text=u"Network address. Use address/CIDR notation", verbose_name="Source Address")
109 sourceport = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchSourcePort", verbose_name="Source Port")
110 destination = models.CharField(max_length=32, help_text=u"Network address. Use address/CIDR notation", verbose_name="Destination Address")
111 destinationport = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchDestinationPort", verbose_name="Destination Port")
112 port = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchPort", verbose_name="Port" )
113 dscp = models.ManyToManyField(MatchDscp, blank=True, null=True, verbose_name="DSCP")
114 fragmenttype = models.CharField(max_length=20, choices=FRAGMENT_CODES, blank=True, null=True, verbose_name="Fragment Type")
115 icmpcode = models.CharField(max_length=32, blank=True, null=True, verbose_name="ICMP Code")
116 icmptype = models.CharField(max_length=32, blank=True, null=True, verbose_name="ICMP Type")
117 packetlength = models.IntegerField(blank=True, null=True, verbose_name="Packet Length")
118 protocol = models.ManyToManyField(MatchProtocol, blank=True, null=True, verbose_name="Protocol")
119 tcpflag = models.CharField(max_length=128, blank=True, null=True, verbose_name="TCP flag")
120 then = models.ManyToManyField(ThenAction, verbose_name="Then")
121 filed = models.DateTimeField(auto_now_add=True)
122 last_updated = models.DateTimeField(auto_now=True)
123 status = models.CharField(max_length=20, choices=ROUTE_STATES, blank=True, null=True, verbose_name="Status", default="PENDING")
124 # is_online = models.BooleanField(default=False)
125 # is_active = models.BooleanField(default=False)
126 expires = models.DateField(default=days_offset)
127 response = models.CharField(max_length=512, blank=True, null=True)
128 comments = models.TextField(null=True, blank=True, verbose_name="Comments")
131 def __unicode__(self):
136 verbose_name = "Rule"
137 verbose_name_plural = "Rules"
139 def save(self, *args, **kwargs):
142 self.name = "%s_%s" %(self.name, hash)
143 super(Route, self).save(*args, **kwargs) # Call the "real" save() method.
146 def clean(self, *args, **kwargs):
147 from django.core.exceptions import ValidationError
150 address = IPNetwork(self.destination)
151 self.destination = address.exploded
153 raise ValidationError('Invalid network address format at Destination Field')
156 address = IPNetwork(self.source)
157 self.source = address.exploded
159 raise ValidationError('Invalid network address format at Source Field')
161 def commit_add(self, *args, **kwargs):
162 peer = self.applier.get_profile().peer.domain_name
163 send_message("[%s] Adding rule %s. Please wait..." %(self.applier.username, self.name), peer)
164 response = add.delay(self)
165 logger.info("Got add job id: %s" %response)
167 def commit_edit(self, *args, **kwargs):
168 peer = self.applier.get_profile().peer.domain_name
169 send_message("[%s] Editing rule %s. Please wait..." %(self.applier.username, self.name), peer)
170 response = edit.delay(self)
171 logger.info("Got edit job id: %s" %response)
173 def commit_delete(self, *args, **kwargs):
176 if "reason" in kwargs:
177 reason = kwargs['reason']
178 reason_text = "Reason: %s. " %reason
179 peer = self.applier.get_profile().peer.domain_name
180 send_message("[%s] Suspending rule %s. %sPlease wait..." %(self.applier.username, self.name, reason_text), peer)
181 response = delete.delay(self, reason=reason)
182 logger.info("Got delete job id: %s" %response)
184 def has_expired(self):
185 today = datetime.date.today()
186 if today > self.expires:
190 def check_sync(self):
191 if not self.is_synced():
192 self.status = "OUTOFSYNC"
197 get_device = PR.Retriever()
198 device = get_device.fetch_device()
200 routes = device.routing_options[0].routes
201 except Exception as e:
202 self.status = "EXPIRED"
204 logger.error("No routing options on device. Exception: %s" %e)
207 if route.name == self.name:
209 logger.info('Found a matching rule name')
210 devicematch = route.match
212 assert(self.destination)
213 assert(devicematch['destination'][0])
214 if self.destination == devicematch['destination'][0]:
215 found = found and True
216 logger.info('Found a matching destination')
219 logger.info('Destination fields do not match')
224 assert(devicematch['source'][0])
225 if self.source == devicematch['source'][0]:
226 found = found and True
227 logger.info('Found a matching source')
230 logger.info('Source fields do not match')
234 assert(self.fragmenttype)
235 assert(devicematch['fragment'][0])
236 if self.fragmenttype == devicematch['fragment'][0]:
237 found = found and True
238 logger.info('Found a matching fragment type')
241 logger.info('Fragment type fields do not match')
245 assert(self.icmpcode)
246 assert(devicematch['icmp-code'][0])
247 if self.icmpcode == devicematch['icmp-code'][0]:
248 found = found and True
249 logger.info('Found a matching icmp code')
252 logger.info('Icmp code fields do not match')
256 assert(self.icmptype)
257 assert(devicematch['icmp-type'][0])
258 if self.icmptype == devicematch['icmp-type'][0]:
259 found = found and True
260 logger.info('Found a matching icmp type')
263 logger.info('Icmp type fields do not match')
266 if found and self.status != "ACTIVE":
267 logger.error('Rule is applied on device but appears as offline')
268 self.status = "ACTIVE"
271 if self.status == "ADMININACTIVE" or self.status == "INACTIVE" or self.status == "EXPIRED":
277 then_statements = self.then.all()
278 for statement in then_statements:
279 if statement.action_value:
280 ret = "%s %s:<strong>%s</strong><br/>" %(ret, statement.action, statement.action_value)
282 ret = "%s %s<br>" %(ret, statement.action)
283 return ret.rstrip(',')
285 get_then.short_description = 'Then statement'
286 get_then.allow_tags = True
291 ret = '%s Dst Addr:<strong>%s</strong> <br/>' %(ret, self.destination)
292 if self.fragmenttype:
293 ret = "%s Fragment Type:<strong>%s</strong><br/>" %(ret, self.fragmenttype)
295 ret = "%s ICMP code:<strong>%s</strong><br/>" %(ret, self.icmpcode)
297 ret = "%s ICMP Type:<strong>%s</strong><br/>" %(ret, self.icmptype)
298 if self.packetlength:
299 ret = "%s Packet Length:<strong>%s</strong><br/>" %(ret, self.packetlength)
301 ret = "%s Src Addr:<strong>%s</strong> <br/>" %(ret, self.source)
303 ret = "%s TCP flag:<strong>%s</strong><br/>" %(ret, self.tcpflag)
305 for port in self.port.all():
306 ret = ret + "Port:<strong>%s</strong> <br/>" %(port)
308 for protocol in self.protocol.all():
309 ret = ret + "Protocol:<strong>%s</strong> <br/>" %(protocol)
310 if self.destinationport:
311 for port in self.destinationport.all():
312 ret = ret + "Dst Port:<strong>%s</strong> <br/>" %(port)
314 for port in self.sourceport.all():
315 ret = ret +"Src Port:<strong>%s</strong> <br/>" %(port)
317 for dscp in self.dscp.all():
318 ret = ret + "%s Port:<strong>%s</strong> <br/>" %(ret, dscp)
319 return ret.rstrip('<br/>')
321 get_match.short_description = 'Match statement'
322 get_match.allow_tags = True
325 def applier_peer(self):
327 applier_peer = self.applier.get_profile().peer
333 def days_to_expire(self):
334 if self.status not in ['EXPIRED', 'ADMININACTIVE', 'ERROR', 'INACTIVE']:
335 expiration_days = (self.expires - datetime.date.today()).days
336 if expiration_days < settings.EXPIRATION_NOTIFY_DAYS:
337 return "%s" %expiration_days
343 def send_message(msg, user):
344 # username = user.username
346 b = beanstalkc.Connection()
347 b.use(settings.POLLS_TUBE)
348 tube_message = json.dumps({'message': str(msg), 'username':peer})