collapse add forward and add reverse helper functions into update_record.

clean up some handling of cname creation.
This commit is contained in:
Jeffrey Forman 2012-11-25 22:35:11 -05:00
parent e1c0e9d256
commit ba78f39670
2 changed files with 61 additions and 100 deletions

View File

@ -6,56 +6,8 @@ import dns.update
import dns.tsig
import re
def add_forward_record(dns_server, zone_name, record_name, record_type, record_data, ttl, keyring):
""" Create a forward DNS record given passed arguments.
Args:
String dns_server
String zone_name
String record_name (just record name, not FQDN)
String record_type (A, AAAA, etc)
String record_data (IP address)
Int ttl
Dict keyring object
Return:
String representation of DNS update output from record creation.
"""
dns_update = dns.update.Update(zone_name, keyring = keyring)
dns_update.replace(record_name, ttl, record_type, record_data)
output = dns.query.tcp(dns_update, dns_server)
return output
def add_reverse_record(dns_server, zone_name, record_name, record_data, ttl, keyring):
""" Create a reverse DNS record (PTR) given passed arguments.
Args:
String dns_server
String zone_name
String record_name (just record name, not FQDN)
String record_type (A, AAAA, etc)
String record_data (IP address)
Int ttl
Dict keyring object
Return:
String representation of DNS update output from record creation.
"""
reverse_ip_fqdn = str(dns.reversename.from_address(record_data))
reverse_ip = re.search(r"([0-9]+).(.*).$", reverse_ip_fqdn).group(1)
reverse_domain = re.search(r"([0-9]+).(.*).$", reverse_ip_fqdn).group(2)
dns_update = dns.update.Update(reverse_domain, keyring = keyring)
dns_update.replace(reverse_ip, ttl, "PTR", "%s.%s." % (record_name, zone_name))
output = dns.query.tcp(dns_update, dns_server)
return output
def add_record(dns_server, zone_name, record_name, record_type, record_data, ttl, key_name, create_reverse):
""" Create DNS record(s) given passed arguments.
def add_record(dns_server, zone_name, record_name, record_type, record_data, ttl, key_name, create_reverse=False):
""" Parse passed elements and determine which records to create.
Args:
String dns_server
@ -71,58 +23,52 @@ def add_record(dns_server, zone_name, record_name, record_type, record_data, ttl
Dict containing {description, output} from record creation
"""
if key_name is None:
keyring = None
else:
this_key = models.Key.objects.get(name=key_name)
keyring = keyutils.create_keyring(this_key.name, this_key.data)
response = []
response.append({ "description" : "Forward Record Added: %s.%s" % (record_name, zone_name),
"output" : add_forward_record(dns_server,
zone_name,
record_name,
record_type,
record_data,
ttl,
keyring)})
"output" : update_record(dns_server,
zone_name,
record_name,
record_type,
record_data,
ttl,
key_name)})
""" If requested, create a reverse PTR record.
Given the forward record created, resolve its underlying IP. Use that to create the reverse record.
reverse_ip_fqdn ex: 5.0.20.10.in-addr.arpa.
reverse_ip: 5
reverse_domain: 0.20.10.in-addr.arpa.
"""
if create_reverse:
reverse_ip_fqdn = str(dns.reversename.from_address(record_data))
# There must be a cleaner way to figure out the ip/domain
# for this reverse DNS record parsing.
reverse_ip = re.search(r"([0-9]+).(.*)$", reverse_ip_fqdn).group(1)
reverse_domain = re.search(r"([0-9]+).(.*)$", reverse_ip_fqdn).group(2)
response.append({ "description" : "Reverse Record Added: %s" % record_data,
"output" : add_reverse_record(dns_server,
zone_name,
record_name,
record_data,
ttl,
keyring)})
"output" : update_record(dns_server,
reverse_domain,
reverse_ip,
"PTR",
"%s.%s." % (record_name, zone_name),
ttl,
key_name)})
return response
def add_cname_record(dns_server, zone_name, originating_record, cname, ttl, key_name):
def add_cname_record(dns_server, zone_name, cname, originating_record, ttl, key_name):
"""Add a Cname record."""
if key_name is None:
keyring = None
else:
this_key = models.Key.objects.get(name=key_name)
keyring = keyutils.create_keyring(this_key.name, this_key.data)
update = dns.update.Update(zone_name, keyring = keyring)
update.replace(cname, ttl, 'CNAME', originating_record + ".")
try:
response = dns.query.tcp(update, dns_server)
except dns.tsig.PeerBadKey, err:
# There is a mismatch between TSIG key configuration
# for allow-update in the named.conf, and the key
# selected to be used for the update.
# Combos that are tripped:
# * allow-update is a netmask, but key selected on form.
raise exceptions.RecordException("TSIG key mismatch between your BIND configuration and what was selected on the form.")
output = update_record(dns_server,
zone_name,
cname,
"CNAME",
originating_record + ".",
ttl,
key_name)
return [{ "description" : "CNAME %s.%s points to %s" % (cname, zone_name, originating_record),
"output" : response}]
"output" : output}]
def delete_record(dns_server, rr_list, key_name):
"""Delete a list of DNS records passed as strings in rr_items."""
@ -145,3 +91,18 @@ def delete_record(dns_server, rr_list, key_name):
"output" : output })
return delete_response
def update_record(dns_server, zone_name, record_name, record_type, record_data, ttl, key_name):
""" Update/Create DNS record of name and type with passed data and ttl. """
if key_name is None:
keyring = None
else:
this_key = models.Key.objects.get(name=key_name)
keyring = keyutils.create_keyring(this_key.name, this_key.data)
dns_update = dns.update.Update(zone_name, keyring = keyring)
dns_update.replace(record_name, ttl, record_type, record_data)
output = dns.query.tcp(dns_update, dns_server)
return output

View File

@ -69,14 +69,14 @@ def view_add_record_result(request):
if form.is_valid():
form_cleaned = form.cleaned_data
try:
add_record_response = helpers.add_record(form_cleaned["dns_server"],
str(form_cleaned["zone_name"]),
str(form_cleaned["record_name"]),
str(form_cleaned["record_type"]),
str(form_cleaned["record_data"]),
form_cleaned["ttl"],
form_cleaned["key_name"],
form_cleaned["create_reverse"])
response = helpers.add_record(form_cleaned["dns_server"],
str(form_cleaned["zone_name"]),
str(form_cleaned["record_name"]),
str(form_cleaned["record_type"]),
str(form_cleaned["record_data"]),
form_cleaned["ttl"],
form_cleaned["key_name"],
form_cleaned["create_reverse"])
except exceptions.RecordException, err:
# TODO: Start using this exception.
# What would cause this?
@ -84,7 +84,7 @@ def view_add_record_result(request):
return render(request, "bcommon/response_result.htm",
{ "errors" : errors,
"response" : add_record_response })
"response" : response })
return render(request, "bcommon/add_record_form.htm",
{ "dns_server" : request.POST["dns_server"],
@ -114,8 +114,8 @@ def view_add_cname_result(request):
add_cname_response = helpers.add_cname_record(
cd["dns_server"],
cd["zone_name"],
str(cd["originating_record"]),
cd["cname"],
str(cd["originating_record"]),
cd["ttl"],
cd["key_name"])
except exceptions.RecordException, err: