End-to-end deletion working for single and multiple records.

This commit is contained in:
Jeffrey Forman 2011-11-25 07:58:43 -05:00
parent 00f8036991
commit 32aadb1ee2
6 changed files with 65 additions and 31 deletions

View File

@ -5,6 +5,8 @@ import dns.query
import dns.reversename
import dns.update
re_IPADDRESS = re.compile(r"\d+.\d+.\d+.\d+")
def list_zone_records(dns_hostname, zone_name):
"""Take a DNS server and a zone name,
and return an array of its records."""
@ -59,15 +61,15 @@ def add_reverse_record(form_data, zone_keyring):
dns_update = dns.update.Update(reverse_domain, keyring = zone_keyring)
dns_update.replace(reverse_ip, int(form_data["ttl"]), "PTR", str(form_data["name"]) + ".")
response = dns.query.tcp(dns_update, form_data["dns_server"])
output = dns.query.tcp(dns_update, form_data["dns_server"])
return response
return output
def add_record(form_data, key_dict):
def add_record(form_data):
"""Add a DNS record with data from a FormAddRecord object.
If a reverse PTR record is requested, this will be added too."""
keyring = create_keyring(key_dict)
keyring = create_keyring(form_data["key_name"])
response = {}
forward_response = add_forward_record(form_data, keyring)
response["forward_response"] = forward_response
@ -77,3 +79,19 @@ def add_record(form_data, key_dict):
response["reverse_response"] = reverse_response
return response
def delete_record(form_data, rr_items):
keyring = create_keyring(form_data["key_name"])
dns_server = form_data["rr_server"]
delete_response = []
for current_rr_item in rr_items:
re_record = re.search(r"(\w+)\.(.*)$", current_rr_item)
record = re_record.group(1)
domain = re_record.group(2)
dns_update = dns.update.Update(domain, keyring = keyring)
dns_update.delete(record)
output = dns.query.tcp(dns_update, dns_server)
delete_response.append({ "rr_item" : current_rr_item, "output" : output })
return delete_response

View File

@ -1,11 +1,13 @@
import dns.tsigkeyring
import re
import sys
from bcommon.models import Key
def create_keyring(key_dict):
def create_keyring(key_name):
"""Accept a TSIG keyfile and a key name to retrieve.
Return a keyring object with the key name and TSIG secret."""
key_dict = Key.objects.get(name=key_name)
keyring = dns.tsigkeyring.from_text({
key_dict.name : key_dict.data
})

View File

@ -3,10 +3,15 @@
from bcommon.models import BindServer, Key
from django.template import Context
from django.shortcuts import render_to_response, redirect
from bcommon.helpers import list_zone_records, add_record #, delete_record
from bcommon.helpers import list_zone_records, add_record, delete_record
from bcommon.forms import FormAddRecord
from django.template import RequestContext
from bcommon.keyutils import create_keyring
import re
RE_UNICODEARRAY = re.compile(r"u'(.*?)'")
def home_index(request):
return render_to_response('index.htm')
@ -73,16 +78,14 @@ def view_add_record_result(request):
context_instance=RequestContext(request))
key_dict = Key.objects.get(name=cd["key_name"])
add_record_response = add_record(cd, key_dict)
add_record_response = add_record(cd)
return render_to_response('bcommon/add_record_result.htm',
{ 'response' : add_record_response },
context_instance=RequestContext(request))
### WORK ON BELOW
def confirm_delete_record(request):
def view_delete_record(request):
if request.method == "GET":
# Return home. You shouldn't trying to directly acces
# the url for deleting records.
@ -92,8 +95,6 @@ def confirm_delete_record(request):
rr_domain = request.POST['rr_domain']
rr_array = request.POST.getlist('rr_array')
## TODO(jforman): We need to handle the case where the POST data
## is somehow bad.
return render_to_response('bcommon/delete_record_initial.htm',
{ 'rr_server' : rr_server,
'rr_domain' : rr_domain,
@ -101,24 +102,20 @@ def confirm_delete_record(request):
'tsig_keys' : Key.objects.all() },
context_instance=RequestContext(request))
# If we hit a case where we don't know what's going on.
# return render_to_response('bcommon/index.htm',
# { 'errors' : "We hit an unhandled exception in deleting your requested records." },
# context_instance=RequestContext(request))
def delete_result(request):
def view_delete_result(request):
if request.method == "GET":
# Return home. You shouldn't trying to directly acces
# Return home. You shouldn't trying to directly access
# the url for deleting records.
return redirect('/')
to_delete_array = {}
to_delete_array['rr_server'] = request.POST['rr_server']
to_delete_array['rr_domain'] = request.POST['rr_domain']
to_delete_array['rr_array'] = eval(request.POST.getlist('rr_array')[0])
for current in to_delete_array['rr_array']:
print "current: %s" % current
# What seems like an ugly hack to get around the fact
# that the array comes back as Unicode values.
rr_unicode_array = request.POST.getlist('rr_array')[0]
rr_items = RE_UNICODEARRAY.findall(rr_unicode_array)
to_delete_array['key_name'] = request.POST['key_name']
to_delete_array['key_data'] = Key.objects.get(name=(to_delete_array['key_name'])).data
delete_result = delete_record(to_delete_array)
delete_result = delete_record(request.POST, rr_items)
return render_to_response('bcommon/delete_record_result.htm',
{ 'delete_result' : delete_result },
context_instance=RequestContext(request))

View File

@ -0,0 +1,17 @@
{% extends "base.htm" %}
{% block pageheader %}
{% endblock pageheader %}
{% block body %}
{% for current_response in delete_result %}
Record: {{ current_response.rr_item }}
<pre>
{{ current_response.output }}
</pre>
{% endfor %}
{% endblock body %}

View File

@ -17,7 +17,7 @@
<input type="hidden" name="rr_domain" value="{{ rr_domain }}">
{% for current_record in record_array %}
<tr>
<td><input type="checkbox" name="rr_array" value="{{ current_record.rr_name }}"></td>
<td><input type="checkbox" name="rr_array" value="{{ current_record.rr_name }}.{{ rr_domain }}"></td>
<td>{{ current_record.rr_name }}</td>
<td>{{ current_record.rr_ttl }}</td>
<td>{{ current_record.rr_class }}</td>

View File

@ -16,8 +16,8 @@ urlpatterns = patterns('',
(r'^add_record/(?P<dns_server>[a-zA-Z0-9.-]+)/(?P<zone>[a-zA-Z0-9.-]+)/$', 'bcommon.views.view_add_record'),
(r'^add_record/result/$', 'bcommon.views.view_add_record_result'),
# (r'^delete_record/$', 'bcommon.views.view_delete_record'),
# (r'^delete_record/result/$', 'bcommon.views.view_delete_result'),
(r'^delete_record/$', 'bcommon.views.view_delete_record'),
(r'^delete_record/result/$', 'bcommon.views.view_delete_result'),
)
if settings.DEBUG: