Merge keyutils into a method inside the Key class. Delete keyutils.py
This commit is contained in:
parent
063d64d6c6
commit
d5b2197571
|
@ -84,10 +84,10 @@ def delete_record(dns_server, rr_list, key_name):
|
|||
"""Delete a list of DNS records passed as strings in rr_items."""
|
||||
|
||||
try:
|
||||
keyring = create_keyring(key_name)
|
||||
except exceptions.KeyringException, err:
|
||||
return([{ "description" : "Error in deletion process",
|
||||
"output" : err }])
|
||||
transfer_key = models.Key.objects.get(name=key_name)
|
||||
keyring = transfer_key.create_keyring()
|
||||
except models.Key.DoesNotExist:
|
||||
keyring = None
|
||||
|
||||
delete_response = []
|
||||
for current_rr in rr_list:
|
||||
|
@ -107,9 +107,10 @@ def create_update(dns_server, zone_name, record_name, record_type, record_data,
|
|||
""" Update/Create DNS record of name and type with passed data and ttl. """
|
||||
|
||||
try:
|
||||
keyring = create_keyring(key_name)
|
||||
except exceptions.KeyringException, err:
|
||||
return(err)
|
||||
transfer_key = models.Key.objects.get(name=key_name)
|
||||
keyring = transfer_key.create_keyring()
|
||||
except models.Key.DoesNotExist:
|
||||
keyring = None
|
||||
|
||||
dns_update = dns.update.Update(zone_name, keyring = keyring)
|
||||
dns_update.replace(record_name, ttl, record_type, record_data)
|
||||
|
@ -159,34 +160,3 @@ def send_dns_update(dns_message, dns_server, key_name):
|
|||
"for correctness." % (dns_server, key_name))
|
||||
|
||||
return output
|
||||
|
||||
def create_keyring(key_name):
|
||||
|
||||
"""Return a tsigkeyring object from key name and key data.
|
||||
|
||||
Args:
|
||||
key_name: String representation of Key name object
|
||||
|
||||
Return:
|
||||
None if key_name is none.
|
||||
keyring object with the key name and TSIG secret.
|
||||
|
||||
Raises:
|
||||
KeyringException: For incorrect key data.
|
||||
"""
|
||||
|
||||
if key_name is None:
|
||||
return None
|
||||
|
||||
# TODO: Unittest here for key_name that does not exist
|
||||
# Stick this in a try/except and catch models.Key.DoesNotExist
|
||||
this_key = models.Key.objects.get(name=key_name)
|
||||
|
||||
try:
|
||||
keyring = dns.tsigkeyring.from_text({
|
||||
this_key.name : this_key.data
|
||||
})
|
||||
except binascii.Error, err:
|
||||
raise exceptions.KeyringException("Incorrect key data. Verify key: %s. Reason: %s" % (key_name, err))
|
||||
|
||||
return keyring
|
||||
|
|
|
@ -14,7 +14,6 @@ import dns.zone
|
|||
# App Imports
|
||||
from binder import exceptions
|
||||
from django.db import models
|
||||
import keyutils
|
||||
|
||||
TSIG_ALGORITHMS = (('hmac-md5', 'MD5'),
|
||||
('hmac-sha1', 'SHA1'),
|
||||
|
@ -34,6 +33,19 @@ class Key(models.Model):
|
|||
def __unicode__(self):
|
||||
return self.name
|
||||
|
||||
def create_keyring(self):
|
||||
if self.name is None:
|
||||
return None
|
||||
|
||||
try:
|
||||
keyring = dns.tsigkeyring.from_text({
|
||||
self.name : self.data
|
||||
})
|
||||
except binascii.Error, err:
|
||||
raise exceptions.KeyringException("Incorrect key data. Verify key: %s. Reason: %s" % (key_name, err))
|
||||
|
||||
return keyring
|
||||
|
||||
|
||||
class BindServer(models.Model):
|
||||
""" Store DNS servers and attributes for referencing their
|
||||
|
@ -93,10 +105,10 @@ class BindServer(models.Model):
|
|||
List of Dicts { String rr_name, String rr_ttl, String rr_class, String rr_type, String rr_data }
|
||||
"""
|
||||
|
||||
if self.default_transfer_key:
|
||||
keyring = keyutils.create_keyring(self.default_transfer_key.name,
|
||||
self.default_transfer_key.data)
|
||||
else:
|
||||
try:
|
||||
transfer_key = Key.objects.get(name=self.default_transfer_key)
|
||||
keyring = transfer_key.create_keyring()
|
||||
except Key.DoesNotExist:
|
||||
keyring = None
|
||||
|
||||
try:
|
||||
|
@ -107,10 +119,10 @@ class BindServer(models.Model):
|
|||
except socket.error, err:
|
||||
# Thrown when the DNS server does not respond for a zone transfer (XFR).
|
||||
raise exceptions.TransferException("DNS server did not respond for transfer. Reason: %s" % err)
|
||||
# except exception.FormError, err:
|
||||
# # TODO: What throws this?
|
||||
# raise exceptions.TransferException("There was an error attempting to list zone records.")
|
||||
|
||||
except dns.exception.FormError:
|
||||
# When the DNS message is malformed.
|
||||
# * Can happen if a TSIG key is required but a default_transfer_key is not specified.
|
||||
raise exceptions.TransferException("There was an error attempting to list zone records. Did you forget to specify a default transfer key?")
|
||||
|
||||
names = zone.nodes.keys()
|
||||
names.sort()
|
||||
|
|
Loading…
Reference in New Issue