collapse add forward and add reverse helper functions into update_record.
clean up some handling of cname creation.
This commit is contained in:
parent
e1c0e9d256
commit
ba78f39670
|
@ -6,56 +6,8 @@ import dns.update
|
||||||
import dns.tsig
|
import dns.tsig
|
||||||
import re
|
import re
|
||||||
|
|
||||||
def add_forward_record(dns_server, zone_name, record_name, record_type, record_data, ttl, keyring):
|
def add_record(dns_server, zone_name, record_name, record_type, record_data, ttl, key_name, create_reverse=False):
|
||||||
""" Create a forward DNS record given passed arguments.
|
""" Parse passed elements and determine which records to create.
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
String dns_server
|
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
|
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 = []
|
||||||
response.append({ "description" : "Forward Record Added: %s.%s" % (record_name, zone_name),
|
response.append({ "description" : "Forward Record Added: %s.%s" % (record_name, zone_name),
|
||||||
"output" : add_forward_record(dns_server,
|
"output" : update_record(dns_server,
|
||||||
zone_name,
|
zone_name,
|
||||||
record_name,
|
record_name,
|
||||||
record_type,
|
record_type,
|
||||||
record_data,
|
record_data,
|
||||||
ttl,
|
ttl,
|
||||||
keyring)})
|
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:
|
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,
|
response.append({ "description" : "Reverse Record Added: %s" % record_data,
|
||||||
"output" : add_reverse_record(dns_server,
|
"output" : update_record(dns_server,
|
||||||
zone_name,
|
reverse_domain,
|
||||||
record_name,
|
reverse_ip,
|
||||||
record_data,
|
"PTR",
|
||||||
ttl,
|
"%s.%s." % (record_name, zone_name),
|
||||||
keyring)})
|
ttl,
|
||||||
|
key_name)})
|
||||||
|
|
||||||
return response
|
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."""
|
"""Add a Cname record."""
|
||||||
|
|
||||||
if key_name is None:
|
output = update_record(dns_server,
|
||||||
keyring = None
|
zone_name,
|
||||||
else:
|
cname,
|
||||||
this_key = models.Key.objects.get(name=key_name)
|
"CNAME",
|
||||||
keyring = keyutils.create_keyring(this_key.name, this_key.data)
|
originating_record + ".",
|
||||||
|
ttl,
|
||||||
update = dns.update.Update(zone_name, keyring = keyring)
|
key_name)
|
||||||
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.")
|
|
||||||
|
|
||||||
return [{ "description" : "CNAME %s.%s points to %s" % (cname, zone_name, originating_record),
|
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):
|
def delete_record(dns_server, rr_list, key_name):
|
||||||
"""Delete a list of DNS records passed as strings in rr_items."""
|
"""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 })
|
"output" : output })
|
||||||
|
|
||||||
return delete_response
|
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
|
||||||
|
|
|
@ -69,14 +69,14 @@ def view_add_record_result(request):
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form_cleaned = form.cleaned_data
|
form_cleaned = form.cleaned_data
|
||||||
try:
|
try:
|
||||||
add_record_response = helpers.add_record(form_cleaned["dns_server"],
|
response = helpers.add_record(form_cleaned["dns_server"],
|
||||||
str(form_cleaned["zone_name"]),
|
str(form_cleaned["zone_name"]),
|
||||||
str(form_cleaned["record_name"]),
|
str(form_cleaned["record_name"]),
|
||||||
str(form_cleaned["record_type"]),
|
str(form_cleaned["record_type"]),
|
||||||
str(form_cleaned["record_data"]),
|
str(form_cleaned["record_data"]),
|
||||||
form_cleaned["ttl"],
|
form_cleaned["ttl"],
|
||||||
form_cleaned["key_name"],
|
form_cleaned["key_name"],
|
||||||
form_cleaned["create_reverse"])
|
form_cleaned["create_reverse"])
|
||||||
except exceptions.RecordException, err:
|
except exceptions.RecordException, err:
|
||||||
# TODO: Start using this exception.
|
# TODO: Start using this exception.
|
||||||
# What would cause this?
|
# What would cause this?
|
||||||
|
@ -84,7 +84,7 @@ def view_add_record_result(request):
|
||||||
|
|
||||||
return render(request, "bcommon/response_result.htm",
|
return render(request, "bcommon/response_result.htm",
|
||||||
{ "errors" : errors,
|
{ "errors" : errors,
|
||||||
"response" : add_record_response })
|
"response" : response })
|
||||||
|
|
||||||
return render(request, "bcommon/add_record_form.htm",
|
return render(request, "bcommon/add_record_form.htm",
|
||||||
{ "dns_server" : request.POST["dns_server"],
|
{ "dns_server" : request.POST["dns_server"],
|
||||||
|
@ -114,8 +114,8 @@ def view_add_cname_result(request):
|
||||||
add_cname_response = helpers.add_cname_record(
|
add_cname_response = helpers.add_cname_record(
|
||||||
cd["dns_server"],
|
cd["dns_server"],
|
||||||
cd["zone_name"],
|
cd["zone_name"],
|
||||||
str(cd["originating_record"]),
|
|
||||||
cd["cname"],
|
cd["cname"],
|
||||||
|
str(cd["originating_record"]),
|
||||||
cd["ttl"],
|
cd["ttl"],
|
||||||
cd["key_name"])
|
cd["key_name"])
|
||||||
except exceptions.RecordException, err:
|
except exceptions.RecordException, err:
|
||||||
|
|
Loading…
Reference in New Issue