fix how keyrings are created, now use two parameters.

This commit is contained in:
Jeffrey Forman 2012-11-17 23:01:37 -05:00
parent b1907b140a
commit 3451cae823
1 changed files with 10 additions and 40 deletions

View File

@ -1,44 +1,14 @@
import keyutils
from binder import keyutils
import re
import dns.query
import dns.reversename
import dns.update
import keyutils
from binder import exceptions, models
re_IPADDRESS = re.compile(r"\d+.\d+.\d+.\d+")
class BinderException(Exception):
pass
def list_zone_records(dns_server, zone_name):
"""Take a DNS server and a zone name,
and return an array of its records."""
# Need to move most of this logic into a helper method.
try:
zone = dns.zone.from_xfr(dns.query.xfr(dns_server, zone_name))
except dns.exception.FormError:
# There was an error querying the server for the specific zone.
# Example: a zone that does not exist on the server.
return { 'errors' : 'Encountered a FormError when querying %s on %s' % (zone_name, dns_server) }
except socket.gaierror, err:
# TODO: Need to better handle errors here.
return { 'errors' : "Problems querying DNS server %s: %s" % (dns_server, err) }
names = zone.nodes.keys()
names.sort()
record_array = []
for current_name in names:
current_record = zone[current_name].to_text(current_name)
for split_record in current_record.split("\n"): # Split the records on the newline
record_array.append({'rr_name' : split_record.split(" ")[0],
'rr_ttl' : split_record.split(" ")[1],
'rr_class' : split_record.split(" ")[2],
'rr_type' : split_record.split(" ")[3],
'rr_data' : split_record.split(" ")[4]})
return record_array
def add_forward_record(dns_server, zone_name, record_name, record_type, record_data, ttl, keyring):
"""Take in data from FormAddRecord and a keyring object,
return a response from the DNS server about adding the record."""
@ -46,10 +16,7 @@ def add_forward_record(dns_server, zone_name, record_name, record_type, record_d
dns_update = dns.update.Update(zone_name, keyring = keyring)
dns_update.replace(record_name, ttl, record_type, record_data)
try:
response = dns.query.tcp(dns_update, dns_server)
except dns.tsig.BadPeerKey:
raise BinderException("There was a problem adding your forward record due to a TSIG key issue.")
return response
@ -70,7 +37,8 @@ def add_record(form_data):
If a reverse PTR record is requested, this will be added too."""
if form_data["key_name"]:
keyring = keyutils.create_keyring(form_data["key_name"])
this_key = models.Key.objects.get(name=form_data["key_name"])
keyring = keyutils.create_keyring(this_key.name, this_key.data)
else:
keyring = None
@ -99,9 +67,10 @@ def add_cname_record(dns_server, zone_name, originating_record, cname, ttl, key_
"""Add a Cname record."""
if key_name is None:
keyring = keyutils.create_keyring(key_name)
else:
keyring = None
else:
this_key = models.Key.objects.get(name=str(key_name))
keyring = keyutils.create_keyring(this_key.name, this_key.data)
update = dns.update.Update(zone_name, keyring = keyring)
update.replace(cname, int(ttl), 'CNAME', originating_record + ".")
@ -115,7 +84,8 @@ def delete_record(form_data, rr_items):
"""Delete a list of DNS records passed as strings in rr_items."""
if form_data["key_name"]:
keyring = keyutils.create_keyring(form_data["key_name"])
this_key = models.Key.objects.get(name=form_data["key_name"])
keyring = keyutils.create_keyring(this_key.name, this_key.data)
else:
keyring = None