Changeset - 3c652f6dc038
[Not reviewed]
default
0 6 4
Branko Majic (branko) - 11 years ago 2013-02-27 20:58:37
branko@majic.rs
Implemented the basic models. Set-up models to be editable with admin. Added the stripper middleware/template process for stripping excess blank lines. Added prototype template for generating the iptables rules. Added basic project listing to home page. Added basic template for showing project details. Added the administration link to main menu.
10 files changed with 207 insertions and 3 deletions:
0 comments (0 inline, 0 general)
conntrackt/admin.py
Show inline comments
 
new file 100644
 
from conntrackt.models import *
 
from django.contrib import admin
 

	
 
admin.site.register(Project)
 
admin.site.register(Location)
 
admin.site.register(Entity)
 
admin.site.register(Interface)
 
admin.site.register(Communication)
conntrackt/models.py
Show inline comments
 
from django.db import models
 
from django.core.exceptions import ValidationError
 

	
 
# Create your models here.
 
class Project(models.Model):
 
    name = models.CharField(max_length = 100)
 
    description = models.TextField(blank = True)
 

	
 
    def __unicode__(self):
 
        return self.name
 

	
 
class Location(models.Model):
 
    name = models.CharField(max_length = 100)
 
    description = models.TextField(blank = True)
 

	
 
    def __unicode__(self):
 
        return self.name
 

	
 
class Entity(models.Model):
 
    name = models.CharField(max_length = 100)
 
    description = models.TextField(blank = True)
 
    project = models.ForeignKey(Project)
 
    location = models.ForeignKey(Location)
 

	
 
    class Meta:
 
        verbose_name_plural = 'entities'
 

	
 
    def __unicode__(self):
 
        return "%s (%s - %s)" % (self.name, self.project, self.location)
 

	
 
class Interface(models.Model):
 
    name = models.CharField(max_length = 100)
 
    description = models.TextField(blank = True)
 
    entity = models.ForeignKey(Entity)
 
    address = models.IPAddressField()
 
    netmask = models.IPAddressField(default='255.255.255.255')
 

	
 
    def __unicode__(self):
 
        return "%s (%s = %s/%s)" % (self.entity.name, self.name, self.address, self.netmask)
 

	
 
class Communication(models.Model):
 
    source = models.ForeignKey(Interface, related_name = 'source_set')
 
    destination = models.ForeignKey(Interface, related_name = 'destination_set')
 
    protocol = models.CharField(max_length = 10)
 
    port = models.IntegerField(default = 0)
 
    description = models.TextField(blank = True)
 

	
 
    def __unicode__(self):
 
        return "%s (%s/%s) -> %s (%s/%s)" % (self.source.entity.name, self.source.address, self.source.netmask,
 
                                             self.destination.entity.name, self.destination.address, self.destination.netmask)
 

	
 
    def clean(self):
 
        if self.source == self.destination:
 
            raise ValidationError('Source and destination must differ.')
 
        if self.protocol.lower() not in ('udp', 'tcp', 'icmp'):
 
            raise ValidationError('%s is not a supported protocol.' % self.protocol)
 

	
conntrackt/stripper.py
Show inline comments
 
new file 100644
 
# This was taken from https://github.com/ldiqual/django-linestripper/
 

	
 
from django.conf import settings
 
from django.template.loaders import app_directories
 

	
 
import re
 

	
 
# To be set in settings.py
 
STRIPPER_TAG = getattr(settings, 'STRIPPER_TAG', '__STRIPPER_TAG__')
 
STRIPPER_CLEAR_LINE = getattr(settings, 'STRIPPER_CLEAR_LINE', False)
 
STRIPPER_ENABLED =  getattr(settings, 'STRIPPER_ENABLED', True)
 
STRIPPER_ALLOWED_CONTENT_TYPES = getattr(settings, 'STRIPPER_ALLOWED_CONTENT_TYPES', (
 
	'text',
 
	'xml'
 
))
 

	
 
# Finders
 
FIND_BLANK_LINE = r'\n(\s*)\n'
 
FIND_START_BLANK_LINE = r'^(\s*)\n'
 
FIND_TAG = STRIPPER_TAG
 

	
 
# Replacers
 
REPLACE_WITH_TAG = '\n'+ STRIPPER_TAG +'\n' if STRIPPER_CLEAR_LINE else r'\n\1'+ STRIPPER_TAG +'\n'
 
REPLACE_START_WITH_TAG = STRIPPER_TAG +'\n' if STRIPPER_CLEAR_LINE else r'\n\1'+ STRIPPER_TAG +'\n'
 

	
 
# Deleters
 
DELETE_BLANK_LINE = '\n'
 
DELETE_START_BLANK_LINE = ''
 
DELETE_TAG = ''
 

	
 
'''
 
This is called AFTER the template generation.
 
It suppresses blank lines and deletes STRIPPER_TAG
 
'''
 
class StripperMiddleware(object):
 
	def process_response(self, request, response):
 
		if not STRIPPER_ENABLED:
 
			return response
 

	
 
		# Checks if the content type is allowed
 
		allowed = False
 
		for type in STRIPPER_ALLOWED_CONTENT_TYPES:
 
			if type in response['Content-Type']:
 
				allowed = True
 
				break
 

	
 
		# If the content type is not allowed, untag and return
 
		if not allowed:
 
			response.content = response.content.replace(STRIPPER_TAG, DELETE_TAG)
 
			return response
 

	
 
		# Suppress a blank line at the beginning of the document
 
		response.content = re.sub(FIND_START_BLANK_LINE, DELETE_START_BLANK_LINE, response.content)
 
		# Suppress blank lines
 
		response.content = re.sub(FIND_BLANK_LINE, DELETE_BLANK_LINE, response.content)
 
		# Delete STRIPPER_TAG
 
		# response.content = re.sub(FIND_TAG, DELETE_TAG, response.content)
 
		response.content = response.content.replace(STRIPPER_TAG, DELETE_TAG)
 
		return response
 

	
 
'''
 
This is called BEFORE the template generation.
 
It adds STRIPPER_TAG to blank lines so they aren't removed in the middleware
 
'''
 
class Loader(app_directories.Loader):
 
    is_usable = True
 

	
 
    def process_content(self, content):
 
    	if not STRIPPER_ENABLED:
 
			return content
 

	
 
        content = re.sub(FIND_BLANK_LINE, REPLACE_WITH_TAG, content)
 
        content = re.sub(FIND_START_BLANK_LINE, REPLACE_WITH_TAG, content)
 
        return content
 

	
 
    def load_template(self, template_name, template_dirs=None):
 
        source, origin = self.load_template_source(template_name, template_dirs)
 
        if not STRIPPER_ENABLED:
 
			return source, origin
 
        return self.process_content(source), origin
conntrackt/templates/conntrackt/entity_iptables.html
Show inline comments
 
new file 100644
 
{% for interface in entity.interface_set.all %}{% for communication in interface.destination_set.all %}
 
{% ifchanged communication.description %}
 

	
 
{% if communication.description %}
 
# {{communication.description}}
 
{% endif %}
 
{% endifchanged %}
 
iptables -A INPUT -s {{communication.source.address}}/{{communication.source.netmask}} -p {{communication.protocol|lower}} -m {{communication.protocol|lower}} --dport {{communication.port}} -j ACCEPT
 
{% endfor %}
 
{% endfor %}
conntrackt/templates/conntrackt/index.html
Show inline comments
 
{% extends "conntrackt/template.html" %}
 

	
 
{% load conntrackt %}
 

	
 
{% block content %}
 
<h1>Welcome to Conntrackt</h1>
 
Select your project:
 
{% if projects %}
 
  {% for project in projects %}
 
<ul class="unstyled">
 
  <li>{% html_link 'project' project.name project.id "btn btn-link" %}</li>
 
</ul>
 
  {% endfor %}
 
{% endif %}
 
{% endblock %}
 

	
conntrackt/templates/conntrackt/project_detail.html
Show inline comments
 
new file 100644
 
{% extends "conntrackt/template.html" %}
 

	
 
{% load conntrackt %}
 

	
 
{% block content %}
 
<h1>{{project.name}}</h1>
 

	
 
{% if project.entity_set %}
 
<ul class="unstyled">
 
{% for entity in project.entity_set.all %}
 
  <li>{% html_link 'entity_iptables' entity.name entity.id "btn btn-link" %}</li>
 
{% endfor %}
 
</ul>
 
{% endif %}
 

	
 
{% endblock %}
 

	
conntrackt/templates/conntrackt/template.html
Show inline comments
 
@@ -31,6 +31,7 @@
 
              {% block header %}
 
            <ul class="nav">
 
              <li class="{% active_link 'index' %}"><a href="{% url index %}"><i class="icon-home icon-white"></i> Main Page</a></li>
 
              <li class="{% active_link 'admin' %}"><a href="{% url admin:index %}"><i class="icon-wrench icon-white"></i> Administration</a></li>
 
            </ul>
 
              {% endblock %}
 
          </div>
conntrackt/urls.py
Show inline comments
 
from django.conf.urls import patterns, include, url
 

	
 
from conntrackt.views import IndexView
 
from conntrackt.views import IndexView, IptablesView
 

	
 
from conntrackt.models import Project
 

	
 
from django.views.generic import DetailView
 

	
 
urlpatterns = patterns(
 
    'threadedcomments_zinnia.views',
 
    'conntrackt.views',
 
    url(r'^$', IndexView.as_view(), name="index"),
 
    url(r'^project/(?P<pk>\d+)/$', DetailView.as_view(model = Project),
 
        name = 'project'),
 
    url(r'^entity/(?P<pk>\d+)/iptables/$', IptablesView.as_view(), name="entity_iptables"),
 
)
 

	
conntrackt/views.py
Show inline comments
 
from django.views.generic import TemplateView
 
from django.views.generic import TemplateView, DetailView
 

	
 
from conntrackt.models import Project, Entity
 

	
 
class IndexView(TemplateView):
 
    """
 
@@ -7,3 +9,16 @@ class IndexView(TemplateView):
 

	
 
    template_name = 'conntrackt/index.html'
 

	
 
    def get_context_data(self, **kwargs):
 
        context = super(IndexView, self).get_context_data(**kwargs)
 

	
 
        context['projects'] = Project.objects.all().order_by('name')
 

	
 
        return context
 

	
 
class IptablesView(DetailView):
 
    model = Entity
 
    template_name = 'conntrackt/entity_iptables.html'
 
    def render_to_response(self, context, **kwargs):
 
        return super(IptablesView, self).render_to_response(context,
 
                                                            content_type='text/plain', **kwargs)
projtest/projtest/settings.py
Show inline comments
 
@@ -81,6 +81,7 @@ SECRET_KEY = '%s-x^wskhxu#5%o)0ck71g7o@7
 

	
 
# List of callables that know how to import templates from various sources.
 
TEMPLATE_LOADERS = (
 
    'conntrackt.stripper.Loader',
 
    'django.template.loaders.filesystem.Loader',
 
    'django.template.loaders.app_directories.Loader',
 
#     'django.template.loaders.eggs.Loader',
 
@@ -94,6 +95,7 @@ MIDDLEWARE_CLASSES = (
 
    'django.contrib.messages.middleware.MessageMiddleware',
 
    # Uncomment the next line for simple clickjacking protection:
 
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 
    'conntrackt.stripper.StripperMiddleware'
 
)
 

	
 
ROOT_URLCONF = 'projtest.urls'
0 comments (0 inline, 0 general)