domain editing, shared secret generation, show bind9 configuration

This commit is contained in:
Thomas Waldmann 2013-11-02 11:29:06 +01:00
parent 029dc20be0
commit 35c953347c
5 changed files with 85 additions and 9 deletions

View File

@ -26,3 +26,9 @@ class CreateDomainForm(forms.ModelForm):
widgets = {
'domain': forms.widgets.TextInput(attrs=dict(autofocus=None)),
}
class EditDomainForm(forms.ModelForm):
class Meta(object):
model = Domain
fields = ['nameserver_ip', 'public', 'available', 'nameserver_update_algorithm', 'nameserver_update_key']

View File

@ -1,5 +1,5 @@
import re
from django.utils.timezone import now
import base64
import dns.resolver
@ -10,6 +10,7 @@ from django.core.validators import RegexValidator
from django.conf import settings
from django.db.models.signals import pre_delete
from django.contrib.auth.hashers import make_password
from django.utils.timezone import now
from . import dnstools
@ -74,6 +75,23 @@ class Domain(models.Model):
def __unicode__(self):
return u"%s" % (self.domain, )
def generate_ns_secret(self):
secret = User.objects.make_random_password(length=64) # 512 bits
self.nameserver_update_key = key = base64.b64encode(secret)
self.nameserver_update_algorithm = 'HMAC_SHA512'
self.save()
return key
def get_bind9_algorithm(self):
mapping = {
'HMAC_SHA512': 'hmac-sha512',
'HMAC_SHA384': 'hmac-sha384',
'HMAC_SHA256': 'hmac-sha256',
'HMAC_SHA224': 'hmac-sha224',
'HMAC_SHA1': 'hmac-sha1',
'HMAC_MD5': 'hmac-md5',
}
return mapping.get(self.nameserver_update_algorithm)
class Host(models.Model):
subdomain = models.CharField(max_length=256, validators=[

View File

@ -13,19 +13,15 @@
<th>Available?</th>
<th>Nameserver IP</th>
<th>Algorithm</th>
<th>Action</th>
</tr>
</thead>
{% for domain in domains %}
<tr>
<td rowspan="2" style="vertical-align: middle"><b>{{ domain.domain }}</b></td>
<td rowspan="2" style="vertical-align: middle"><a href="{% url 'domain_view' domain.pk %}">{{ domain.domain }}</a></td>
<td>{{ domain.public|yesno }}</td>
<td>{{ domain.available|yesno }}</td>
<td>{{ domain.nameserver_ip }}</td>
<td>{{ domain.get_nameserver_update_algorithm_display }}</td>
<td rowspan="2" style="vertical-align: middle">
<a href="{% url 'delete_domain' domain.pk %}"><i class="icon icon-remove"></i> delete</a>
</td>
</tr>
<tr>
<td colspan="4">Key: {{ domain.nameserver_update_key }}</td>

View File

@ -2,8 +2,8 @@ from django.conf.urls import patterns, url
from django.views.generic import TemplateView
from .views import (
HomeView, OverviewView, HostView, DeleteHostView, AboutView, HelpView, GenerateSecretView,
RobotsTxtView, DomainOverwievView, DeleteDomainView, ScreenshotsView)
HomeView, OverviewView, HostView, DeleteHostView, AboutView, HelpView, GenerateSecretView, GenerateNSSecretView,
RobotsTxtView, DomainOverwievView, DomainView, DeleteDomainView, ScreenshotsView)
from ..api.views import (
MyIpView, DetectIpView, AjaxGetIps, NicUpdateView, AuthorizedNicUpdateView)
@ -18,7 +18,9 @@ urlpatterns = patterns(
url(r'^screenshots/$', ScreenshotsView.as_view(), name="screenshots"),
url(r'^overview/$', OverviewView.as_view(), name='overview'),
url(r'^host/(?P<pk>\d+)/$', HostView.as_view(), name='host_view'),
url(r'^domain/(?P<pk>\d+)/$', DomainView.as_view(), name='domain_view'),
url(r'^generate_secret/(?P<pk>\d+)/$', GenerateSecretView.as_view(), name='generate_secret_view'),
url(r'^generate_ns_secret/(?P<pk>\d+)/$', GenerateNSSecretView.as_view(), name='generate_ns_secret_view'),
url(r'^host/(?P<pk>\d+)/delete/$',
DeleteHostView.as_view(), name='delete_host'),
url(r'^domain_overview/$',

View File

@ -12,7 +12,7 @@ from django.core.exceptions import PermissionDenied
import dnstools
from .forms import CreateHostForm, EditHostForm, CreateDomainForm
from .forms import CreateHostForm, EditHostForm, CreateDomainForm, EditDomainForm
from .models import Host, Domain
@ -40,6 +40,29 @@ class GenerateSecretView(UpdateView):
return context
class GenerateNSSecretView(UpdateView):
model = Domain
template_name = "main/generate_ns_secret.html"
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(GenerateNSSecretView, self).dispatch(*args, **kwargs)
def get_object(self, *args, **kwargs):
obj = super(GenerateNSSecretView, self).get_object(*args, **kwargs)
if obj.created_by != self.request.user:
raise PermissionDenied() # or Http404
return obj
def get_context_data(self, *args, **kwargs):
context = super(GenerateNSSecretView, self).get_context_data(*args, **kwargs)
context['nav_overview'] = True
context['shared_secret'] = self.object.generate_ns_secret()
context['domains'] = Domain.objects.filter(created_by=self.request.user)
messages.add_message(self.request, messages.SUCCESS, 'Nameserver shared secret created.')
return context
class AboutView(TemplateView):
template_name = "main/about.html"
@ -210,6 +233,37 @@ class DomainOverwievView(CreateView):
return context
class DomainView(UpdateView):
model = Domain
template_name = "main/domain.html"
form_class = EditDomainForm
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(DomainView, self).dispatch(*args, **kwargs)
def get_success_url(self):
return reverse('domain_overview')
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.save()
messages.add_message(self.request, messages.SUCCESS, 'Domain updated.')
return HttpResponseRedirect(self.get_success_url())
def get_object(self, *args, **kwargs):
obj = super(DomainView, self).get_object(*args, **kwargs)
if obj.created_by != self.request.user:
raise PermissionDenied() # or Http404
return obj
def get_context_data(self, *args, **kwargs):
context = super(DomainView, self).get_context_data(*args, **kwargs)
context['nav_overview'] = True
context['domains'] = Domain.objects.filter(created_by=self.request.user)
return context
class DeleteDomainView(DeleteView):
model = Domain
template_name = "main/delete_object.html"