Merge keyutils into a method inside the Key class. Delete keyutils.py

This commit is contained in:
Jeffrey Forman 2012-11-30 22:40:58 -05:00
parent 063d64d6c6
commit d5b2197571
2 changed files with 29 additions and 47 deletions

View File

@ -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.""" """Delete a list of DNS records passed as strings in rr_items."""
try: try:
keyring = create_keyring(key_name) transfer_key = models.Key.objects.get(name=key_name)
except exceptions.KeyringException, err: keyring = transfer_key.create_keyring()
return([{ "description" : "Error in deletion process", except models.Key.DoesNotExist:
"output" : err }]) keyring = None
delete_response = [] delete_response = []
for current_rr in rr_list: 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. """ """ Update/Create DNS record of name and type with passed data and ttl. """
try: try:
keyring = create_keyring(key_name) transfer_key = models.Key.objects.get(name=key_name)
except exceptions.KeyringException, err: keyring = transfer_key.create_keyring()
return(err) except models.Key.DoesNotExist:
keyring = None
dns_update = dns.update.Update(zone_name, keyring = keyring) dns_update = dns.update.Update(zone_name, keyring = keyring)
dns_update.replace(record_name, ttl, record_type, record_data) 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)) "for correctness." % (dns_server, key_name))
return output 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

View File

@ -14,7 +14,6 @@ import dns.zone
# App Imports # App Imports
from binder import exceptions from binder import exceptions
from django.db import models from django.db import models
import keyutils
TSIG_ALGORITHMS = (('hmac-md5', 'MD5'), TSIG_ALGORITHMS = (('hmac-md5', 'MD5'),
('hmac-sha1', 'SHA1'), ('hmac-sha1', 'SHA1'),
@ -34,6 +33,19 @@ class Key(models.Model):
def __unicode__(self): def __unicode__(self):
return self.name 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): class BindServer(models.Model):
""" Store DNS servers and attributes for referencing their """ 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 } List of Dicts { String rr_name, String rr_ttl, String rr_class, String rr_type, String rr_data }
""" """
if self.default_transfer_key: try:
keyring = keyutils.create_keyring(self.default_transfer_key.name, transfer_key = Key.objects.get(name=self.default_transfer_key)
self.default_transfer_key.data) keyring = transfer_key.create_keyring()
else: except Key.DoesNotExist:
keyring = None keyring = None
try: try:
@ -107,10 +119,10 @@ class BindServer(models.Model):
except socket.error, err: except socket.error, err:
# Thrown when the DNS server does not respond for a zone transfer (XFR). # 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) raise exceptions.TransferException("DNS server did not respond for transfer. Reason: %s" % err)
# except exception.FormError, err: except dns.exception.FormError:
# # TODO: What throws this? # When the DNS message is malformed.
# raise exceptions.TransferException("There was an error attempting to list zone records.") # * 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 = zone.nodes.keys()
names.sort() names.sort()