Merge branch 'master' of github.com:asmaps/nsupdate.info

Conflicts:
	nsupdate/main/views.py
This commit is contained in:
Arne Schauf 2013-09-28 22:22:11 +02:00
commit cddda9b6c0
8 changed files with 91 additions and 50 deletions

View File

@ -4,16 +4,16 @@ Tests for dnstools module.
import pytest
from ..dnstools import (update, query_ns, parse_name, update_ns, SameIpError,
BASEDOMAIN, NONEXISTING_HOST,
WWW_HOST, WWW_IPV4_HOST, WWW_IPV4_IP, WWW_IPV6_HOST, WWW_IPV6_IP, )
from ..dnstools import update, query_ns, parse_name, update_ns, SameIpError
from django.conf import settings
BASEDOMAIN = settings.BASEDOMAIN
from dns.resolver import NXDOMAIN
class TestIntelligentUpdater(object):
def test_double_update(self):
host, ip = 'test0.' + BASEDOMAIN, '1.2.3.4'
host, ip = 'test0.' + settings.BASEDOMAIN, '1.2.3.4'
# make sure the host is not there
try:
update_ns(host, 'A', action='del')
@ -29,16 +29,16 @@ class TestIntelligentUpdater(object):
class TestQuery(object):
def test_queries_ok(self):
assert query_ns(WWW_IPV4_HOST, 'A') == WWW_IPV4_IP # v4 ONLY
assert query_ns(WWW_IPV6_HOST, 'AAAA') == WWW_IPV6_IP # v6 ONLY
assert query_ns(WWW_HOST, 'A') == WWW_IPV4_IP # v4 and v6, query v4
assert query_ns(WWW_HOST, 'AAAA') == WWW_IPV6_IP # v4 and v6, query v6
assert query_ns(settings.WWW_IPV4_HOST, 'A') == settings.WWW_IPV4_IP # v4 ONLY
assert query_ns(settings.WWW_IPV6_HOST, 'AAAA') == settings.WWW_IPV6_IP # v6 ONLY
assert query_ns(settings.WWW_HOST, 'A') == settings.WWW_IPV4_IP # v4 and v6, query v4
assert query_ns(settings.WWW_HOST, 'AAAA') == settings.WWW_IPV6_IP # v4 and v6, query v6
def test_queries_failing(self):
with pytest.raises(NXDOMAIN):
query_ns(NONEXISTING_HOST, 'A')
query_ns(settings.NONEXISTING_HOST, 'A')
with pytest.raises(NXDOMAIN):
query_ns(NONEXISTING_HOST, 'AAAA')
query_ns(settings.NONEXISTING_HOST, 'AAAA')
class TestUpdate(object):

View File

@ -13,20 +13,6 @@ import dns.tsig
import dns.tsigkeyring
SERVER = settings.SERVER
BASEDOMAIN = settings.BASEDOMAIN
NONEXISTING_HOST = settings.NONEXISTING_HOST
WWW_HOST = settings.WWW_HOST
WWW_IPV4_HOST = settings.WWW_IPV4_HOST
WWW_IPV6_HOST = settings.WWW_IPV6_HOST
WWW_IPV4_IP = settings.WWW_IPV4_IP
WWW_IPV6_IP = settings.WWW_IPV6_IP
UPDATE_ALGO = settings.UPDATE_ALGO
UPDATE_KEY = settings.UPDATE_KEY
class SameIpError(ValueError):
"""
raised if an IP address is already present in DNS and and update was
@ -66,8 +52,8 @@ def query_ns(qname, rdtype):
resolver = dns.resolver.Resolver(configure=False)
# we do not configure it from resolv.conf, but patch in the values we
# want into the documented attributes:
resolver.nameservers = [SERVER, ]
resolver.search = [dns.name.from_text(BASEDOMAIN), ]
resolver.nameservers = [settings.SERVER, ]
resolver.search = [dns.name.from_text(settings.BASEDOMAIN), ]
answer = resolver.query(qname, rdtype)
return str(list(answer)[0])
@ -107,8 +93,8 @@ def update_ns(fqdn, rdtype='A', ipaddr=None, origin=None, action='upd', ttl=60):
assert action in ['add', 'del', 'upd', ]
origin, name = parse_name(fqdn, origin)
upd = dns.update.Update(origin,
keyring=dns.tsigkeyring.from_text({BASEDOMAIN+'.': UPDATE_KEY}),
keyalgorithm=UPDATE_ALGO)
keyring=dns.tsigkeyring.from_text({settings.BASEDOMAIN+'.': settings.UPDATE_KEY}),
keyalgorithm=settings.UPDATE_ALGO)
if action == 'add':
assert ipaddr is not None
upd.add(name, ttl, rdtype, ipaddr)
@ -117,5 +103,5 @@ def update_ns(fqdn, rdtype='A', ipaddr=None, origin=None, action='upd', ttl=60):
elif action == 'upd':
assert ipaddr is not None
upd.replace(name, ttl, rdtype, ipaddr)
response = dns.query.tcp(upd, SERVER)
response = dns.query.tcp(upd, settings.SERVER)
return response

View File

@ -13,6 +13,13 @@ class HostForm(forms.ModelForm):
super(HostForm, self).__init__(*args, **kwargs)
self.created_by = user
def save(self, user, commit=True):
instance = super(HostForm, self).save(commit=False)
instance.created_by = user
if commit:
instance.save()
return instance
def create_host(self, user):
self.clean()
host = Host(fqdn=self.cleaned_data['fqdn'],

View File

@ -4,6 +4,7 @@ from django.forms import ModelForm
class Host(models.Model):
"""TODO: hash update_secret"""
fqdn = models.CharField(max_length=256)
update_secret = models.CharField(max_length=256)
comment = models.CharField(max_length=256,default='')
@ -16,6 +17,7 @@ class Host(models.Model):
return u"%s (%s)" % (self.fqdn, self.created_by)
class HostForm(ModelForm):
class Meta:
model = Host

View File

@ -0,0 +1,17 @@
{% extends "base.html" %}
{% load bootstrap %}
{% block content %}
<div class="row">
<h3>Edit Host</h3>
<div class="col-lg-4">
<form method="post" action="">
{% csrf_token %}
{{ HostForm|bootstrap }}
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
{% endblock %}

View File

@ -5,33 +5,38 @@
<div class="row">
<div class="col-lg-8">
<h1>Host List</h1>
<h3>Host List</h3>
<table class="table">
<tr><th>domain</th><th>last update</th><th>comment</th></tr>
<tr><th>domain</th><th>last update</th><th>comment</th><th>action</th></tr>
{% for host in Hosts %}
<tr><td><b>{{ host.fqdn }}</b>.nsupdate.info</td> <td>{{ host.last_update|date }}</td> <td>{{ host.comment }}</td></tr>
<tr>
<td><b>{{ host.fqdn }}</b></td>
<td>{{ host.last_update|date }}</td>
<td>{{ host.comment }}</td>
<td><a href="{% url 'host-view' host.pk %}"><span class="glyphicon glyphicon-edit"></span></a>
</tr>
{% empty %}
<tr>No hosts yet.</tr>
{% endfor %}
</table>
<div style="display: none">
<img src="//{{ WWW_IPV4_HOST }}/updateip" />
<img src="//{{ WWW_IPV6_HOST }}/updateip" />
</div>
</div>
<h1>New host</h1>
<h3>New host</h3>
<div class="col-lg-4">
<form method="post" action="">
{% csrf_token %}
{{ HostForm|bootstrap }}
<label>IPv4</label>
<input type='text' name='ipv4addr' class="form-control ipv4adr" placeholder="can't find IP" value="{{session.ipv4}}"> </br >
<label>IPv6</label>
<input type='text' name='ipv6addr' class="form-control ipv6adr" placeholder="can't find IP" value="{{session.ipv6}}"> </br>
<button type="submit" class="btn btn-primary">Create</button>
</form>
</div>
</div>
<div class="row">
<h3>Information</h3>
<b>your IPv4:</b> <span class="ipv4adr">{{session.ipv4}}</span> </br>
<b>your IPv6:</b> <span class="ipv6adr">{{session.ipv6}}</span>
</div>
<div style="display: none">
<img src="//{{ WWW_IPV4_HOST }}/updateip" />
<img src="//{{ WWW_IPV6_HOST }}/updateip" />
</div>
{% endblock %}

View File

@ -1,6 +1,6 @@
from django.conf.urls import patterns, include, url
from main.views import (
HomeView, OverviewView,
HomeView, OverviewView, HostView,
)
from api.views import (
MyIpView, UpdateIpView, NicUpdateView
@ -9,6 +9,7 @@ from api.views import (
urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name="home"),
url(r'^overview/$', OverviewView, name='overview'),
url(r'^host/(?P<pk>\w+)$', HostView, name='host-view'),
url(r'^myip$', MyIpView),
url(r'^updateip$', UpdateIpView),
url(r'^nic/update$', NicUpdateView),

View File

@ -3,11 +3,19 @@ from django.views.generic import TemplateView
from django.views.generic.list import ListView
from django.http import HttpResponse
from django.conf import settings
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from main.forms import HostForm
from main.models import Host
def create_context(request):
context = {}
context['WWW_IPV4_HOST'] = settings.WWW_IPV4_HOST
context['WWW_IPV6_HOST'] = settings.WWW_IPV6_HOST
context['session'] = request.session
return context
class HomeView(TemplateView):
template_name = "base.html"
@ -18,21 +26,36 @@ class HomeView(TemplateView):
@login_required
def OverviewView(request):
context = {}
context = create_context(request)
context['nav_overview'] = True
context['WWW_IPV4_HOST'] = settings.WWW_IPV4_HOST
context['WWW_IPV6_HOST'] = settings.WWW_IPV6_HOST
context['session'] = request.session
context['HostForm'] = HostForm(request.user)
context['Hosts'] = Host.objects.filter(created_by=request.user)
if request.method == "POST":
print "POST"
form = HostForm(request.user,request.POST)
print form
if form.is_valid():
print "valid"
host = form.create_host(request.user)
host.save()
context['HostForm'] = form
return render(request, "main/overview.html", context)
<<<<<<< HEAD
=======
@login_required
def HostView(request,pk=None):
context = create_context(request)
context['nav_overview'] = True
context['HostForm'] = HostForm(request.user,instance=get_object_or_404(Host, pk=pk, created_by=request.user))
if request.method == "POST":
print "POST"
form = HostForm(request.user, request.POST,)
print form
if form.is_valid():
print "valid"
host = form.save(request.user)
context['HostForm'] = form
return render(request, "main/host.html", context)
>>>>>>> 3b2fa9008fe6aa3d20e7b3ae9504fadedb4165c4