Simplify add_to_zone

This commit is contained in:
otthorn 2021-04-09 05:02:56 +02:00
parent 2ef3962bdf
commit f18d1496b8

182
lib.py
View file

@ -51,39 +51,59 @@ def format_mname(name: str):
return mname return mname
#def add_to_zone(zone, name, rdata):
# """Add a rdata object to a zone object."""
#
# zone.nodes.setdefault(name, dns.node.Node())
# rdataset = zone.nodes[name].find_rdataset(
# rdata.rdclass,
# rdata.rdtype,
# create=True
# )
# rdataset.add(rdata)
def soa_handler(zone, records):
def add_to_zone(zone, name, rdata):
"""Add a rdata object to a zone object."""
rdataset = zone.find_rdataset(
name,
rdata.rdtype,
create=True
)
rdataset.add(rdata)
def soa_handler(re2o_zone, dns_zone):
"""Handler for SOA record""" """Handler for SOA record"""
soa = zone["soa"] soa = re2o_zone["soa"]
logging.debug(f"SOA = {soa}") logging.debug(f"SOA = {soa}")
rd = dns.rdataset.Rdataset(dns.rdataclass.IN, dns.rdatatype.SOA) ns = re2o_zone["ns_records"][0]["target"]
ns_obj = dns.name.from_text(ns)
soa_obj = SOA.SOA( soa_obj = SOA.SOA(
dns.rdataclass.IN, dns.rdataclass.IN,
dns.rdatatype.SOA, dns.rdatatype.SOA,
format_mname(soa["name"]), ns_obj,
format_rname(soa["mail"]), format_rname(soa["mail"]),
#soa["serial"], #soa["serial"],
0, 0,
soa["refresh"], soa["refresh"],
soa["retry"], soa["retry"],
soa["expire"], soa["expire"],
soa["ttl"], soa["ttl"],
) )
rd.add(soa_obj) add_to_zone(dns_zone, AT, soa_obj)
records[AT].rdatasets.append(rd)
def originv4_handler(zone, records): def originv4_handler(re2o_zone, dns_zone):
"""Handler for the IPv4 origin""" """Handler for the IPv4 origin"""
ipv4_addr = zone["originv4"]["ipv4"] ipv4_addr = re2o_zone["originv4"]["ipv4"]
logging.debug(f"originv4 = {zone['originv4']}") logging.debug(f"originv4 = {re2o_zone['originv4']}")
rd = dns.rdataset.Rdataset(dns.rdataclass.IN, dns.rdatatype.A)
originv4_obj = A.A( originv4_obj = A.A(
dns.rdataclass.IN, dns.rdataclass.IN,
@ -91,32 +111,28 @@ def originv4_handler(zone, records):
ipv4_addr ipv4_addr
) )
rd.add(originv4_obj) add_to_zone(dns_zone, AT, originv4_obj)
records[AT].rdatasets.append(rd)
def originv6_handler(zone, records): def originv6_handler(re2o_zone, dns_zone):
"""Handler for the IPv6 origin""" """Handler for the IPv6 origin"""
ipv6_addr = zone["originv6"] # Yes, re2o is this weird and inconsistent ipv6_addr = zone["originv6"] # Yes, re2o is this weird and inconsistent
logging.debug(f"originv6 = {zone['originv6']}") logging.debug(f"originv6 = {zone['originv6']}")
rd = dns.rdataset.Rdataset(dns.rdataclass.IN, dns.rdatatype.AAAA)
originv6_obj = AAAA.AAAA( originv6_obj = AAAA.AAAA(
dns.rdataclass.IN, dns.rdataclass.IN,
dns.rdatatype.AAAA, dns.rdatatype.AAAA,
ipv6_addr ipv6_addr
) )
rd.add(originv6_obj) add_to_zone(dns_zone, AT, originv6_obj)
records[AT].rdatasets.append(rd)
def ns_records_handler(zone, records): def ns_records_handler(re2o_zone, dns_zone):
"""Handler for the NS record""" """Handler for the NS record"""
for record in zone["ns_records"]: for record in re2o_zone["ns_records"]:
logging.debug(f"NS target = {record}") logging.debug(f"NS target = {record}")
@ -129,15 +145,13 @@ def ns_records_handler(zone, records):
target_obj target_obj
) )
rd = dns.rdataset.Rdataset(dns.rdataclass.IN, dns.rdatatype.NS) add_to_zone(dns_zone, AT, NS_obj)
rd.add(NS_obj)
records[AT].rdatasets.append(rd)
def sshfp_record_handler(zone, records): def sshfp_record_handler(re2o_zone, dns_zone):
"""Handler for the SSHFP record""" """Handler for the SSHFP record"""
for record in zone["sshfp"]: for record in re2o_zone["sshfp"]:
# DNS Name object for Hostname # DNS Name object for Hostname
hostname = record["hostname"] hostname = record["hostname"]
@ -152,44 +166,43 @@ def sshfp_record_handler(zone, records):
fingerprint = fp["hash"][fp_type] fingerprint = fp["hash"][fp_type]
records[key_name].rdatasets.append( SSHFP_obj = SSHFP.SSHFP(
SSHFP.SSHFP( dns.rdataclass.IN,
dns.rdataclass.IN, dns.rdatatype.SSHFP,
dns.rdatatype.SSHFP, algorithm,
algorithm, fp_type,
fp_type, fingerprint,
fingerprint,
)
) )
add_to_zone(dns_zone, key_name, SSHFP_obj)
def mx_records_handler(zone, records):
def mx_records_handler(re2o_zone, dns_zone):
"""Handler for the MX record""" """Handler for the MX record"""
# dns name object '@' for record in re2o_zone["mx_records"]:
key_name = dns.name.Name(())
for record in zone["mx_records"]:
logging.debug(f"MX = {record}") logging.debug(f"MX = {record}")
preference = record["priority"] preference = record["priority"]
exchange = record["target"] exchange = record["target"]
records[key_name].rdatasets.append( exchange_obj = dns.name.from_text(exchange)
MX.MX(
dns.rdataclass.IN, MX_obj = MX.MX(
dns.rdatatype.MX, dns.rdataclass.IN,
preference, dns.rdatatype.MX,
exchange preference,
) exchange_obj
) )
add_to_zone(dns_zone, AT, MX_obj)
def txt_records_handler(zone, records):
def txt_records_handler(re2o_zone, dns_zone):
"""Handler for TXT record""" """Handler for TXT record"""
for record in zone["txt_records"]: for record in re2o_zone["txt_records"]:
logging.debug(f"TXT = {record}") logging.debug(f"TXT = {record}")
@ -197,41 +210,40 @@ def txt_records_handler(zone, records):
name = record["field1"] name = record["field1"]
key_name = dns.name.Name((name,)) key_name = dns.name.Name((name,))
records[key_name].rdatasets.append( TXT_obj = TXT.TXT(
TXT.TXT( dns.rdataclass.IN,
dns.rdataclass.IN, dns.rdatatype.TXT,
dns.rdatatype.TXT, record["field2"]
record["field2"]
)
) )
add_to_zone(dns_zone, key_name, TXT_obj)
def srv_records_handler(zone, records):
def srv_records_handler(re2o_zone, dns_zone):
"""Handler for SRV record""" """Handler for SRV record"""
for record in zone["srv_records"]: for record in re2o_zone["srv_records"]:
logging.debug(f"SRV = {record}") logging.debug(f"SRV = {record}")
# DNS Name obj for SRV # DNS Name obj for SRV
key_name = dns.name.from_text(f"{record['service']}_{record['protocol']}") key_name = dns.name.from_text(f"{record['service']}_{record['protocol']}")
records[key_name].rdatasets.append( SRV_obj = SRV.SRV(
SRV.SRV( dns.rdataclass.IN,
dns.rdataclass.IN, dns.rdatatype.SRV,
dns.rdatatype.SRV, record["priority"],
record["priority"], record["weight"],
record["weight"], record["port"],
record["port"], record["target"]
record["target"]
)
) )
add_to_zone(dns_zone, key_name, SRV_obj)
def a_records_handler(zone, records): def a_records_handler(re2o_zone, dns_zone):
"""Handler for A Record""" """Handler for A Record"""
for record in zone["a_records"]: for record in re2o_zone["a_records"]:
logging.debug(f"A = {record}") logging.debug(f"A = {record}")
@ -241,22 +253,19 @@ def a_records_handler(zone, records):
ipv4_addr = record["ipv4"] ipv4_addr = record["ipv4"]
rd = dns.rdataset.Rdataset(dns.rdataclass.IN, dns.rdatatype.A)
A_obj = A.A( A_obj = A.A(
dns.rdataclass.IN, dns.rdataclass.IN,
dns.rdatatype.A, dns.rdatatype.A,
ipv4_addr ipv4_addr
) )
rd.add(A_obj) add_to_zone(dns_zone, key_name, A_obj)
records[key_name].rdatasets.append(rd)
def aaaa_records_handler(zone, records): def aaaa_records_handler(re2o_zone, dns_zone):
"""Handler for AAAA Record""" """Handler for AAAA Record"""
for record in zone["aaaa_records"]: for record in re2o_zone["aaaa_records"]:
logging.debug(f"AAAA = {record}") logging.debug(f"AAAA = {record}")
@ -264,6 +273,10 @@ def aaaa_records_handler(zone, records):
logging.debug("AAAA record does not have an IPv6. Skipping.") logging.debug("AAAA record does not have an IPv6. Skipping.")
return return
# DNS Name object for Hostname
hostname = record["hostname"]
key_name = dns.name.Name((hostname,))
ipv6_addr = record["ipv6"][0]["ipv6"] # thanks re2o ipv6_addr = record["ipv6"][0]["ipv6"] # thanks re2o
AAAA_obj = AAAA.AAAA( AAAA_obj = AAAA.AAAA(
@ -272,13 +285,8 @@ def aaaa_records_handler(zone, records):
ipv6_addr ipv6_addr
) )
# DNS Name object for Hostname
hostname = record["hostname"]
key_name = dns.name.Name((hostname,))
rd = dns.rdataset.Rdataset(dns.rdataclass.IN, dns.rdatatype.AAAA) add_to_zone(dns_zone, key_name, AAAA_obj)
rd.add(AAAA_obj)
records[key_name].rdatasets.append(rd)
def pass_handler(zone, records): def pass_handler(zone, records):