Simplify add_to_zone

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

130
lib.py
View file

@ -51,19 +51,42 @@ def format_mname(name: str):
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"""
soa = zone["soa"]
soa = re2o_zone["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(
dns.rdataclass.IN,
dns.rdatatype.SOA,
format_mname(soa["name"]),
ns_obj,
format_rname(soa["mail"]),
#soa["serial"],
0,
@ -73,17 +96,14 @@ def soa_handler(zone, records):
soa["ttl"],
)
rd.add(soa_obj)
records[AT].rdatasets.append(rd)
add_to_zone(dns_zone, AT, soa_obj)
def originv4_handler(zone, records):
def originv4_handler(re2o_zone, dns_zone):
"""Handler for the IPv4 origin"""
ipv4_addr = zone["originv4"]["ipv4"]
logging.debug(f"originv4 = {zone['originv4']}")
rd = dns.rdataset.Rdataset(dns.rdataclass.IN, dns.rdatatype.A)
ipv4_addr = re2o_zone["originv4"]["ipv4"]
logging.debug(f"originv4 = {re2o_zone['originv4']}")
originv4_obj = A.A(
dns.rdataclass.IN,
@ -91,32 +111,28 @@ def originv4_handler(zone, records):
ipv4_addr
)
rd.add(originv4_obj)
records[AT].rdatasets.append(rd)
add_to_zone(dns_zone, AT, originv4_obj)
def originv6_handler(zone, records):
def originv6_handler(re2o_zone, dns_zone):
"""Handler for the IPv6 origin"""
ipv6_addr = zone["originv6"] # Yes, re2o is this weird and inconsistent
logging.debug(f"originv6 = {zone['originv6']}")
rd = dns.rdataset.Rdataset(dns.rdataclass.IN, dns.rdatatype.AAAA)
originv6_obj = AAAA.AAAA(
dns.rdataclass.IN,
dns.rdatatype.AAAA,
ipv6_addr
)
rd.add(originv6_obj)
records[AT].rdatasets.append(rd)
add_to_zone(dns_zone, AT, originv6_obj)
def ns_records_handler(zone, records):
def ns_records_handler(re2o_zone, dns_zone):
"""Handler for the NS record"""
for record in zone["ns_records"]:
for record in re2o_zone["ns_records"]:
logging.debug(f"NS target = {record}")
@ -129,15 +145,13 @@ def ns_records_handler(zone, records):
target_obj
)
rd = dns.rdataset.Rdataset(dns.rdataclass.IN, dns.rdatatype.NS)
rd.add(NS_obj)
records[AT].rdatasets.append(rd)
add_to_zone(dns_zone, AT, NS_obj)
def sshfp_record_handler(zone, records):
def sshfp_record_handler(re2o_zone, dns_zone):
"""Handler for the SSHFP record"""
for record in zone["sshfp"]:
for record in re2o_zone["sshfp"]:
# DNS Name object for Hostname
hostname = record["hostname"]
@ -152,44 +166,43 @@ def sshfp_record_handler(zone, records):
fingerprint = fp["hash"][fp_type]
records[key_name].rdatasets.append(
SSHFP.SSHFP(
SSHFP_obj = SSHFP.SSHFP(
dns.rdataclass.IN,
dns.rdatatype.SSHFP,
algorithm,
fp_type,
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"""
# dns name object '@'
key_name = dns.name.Name(())
for record in zone["mx_records"]:
for record in re2o_zone["mx_records"]:
logging.debug(f"MX = {record}")
preference = record["priority"]
exchange = record["target"]
records[key_name].rdatasets.append(
MX.MX(
exchange_obj = dns.name.from_text(exchange)
MX_obj = MX.MX(
dns.rdataclass.IN,
dns.rdatatype.MX,
preference,
exchange
)
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"""
for record in zone["txt_records"]:
for record in re2o_zone["txt_records"]:
logging.debug(f"TXT = {record}")
@ -197,27 +210,26 @@ def txt_records_handler(zone, records):
name = record["field1"]
key_name = dns.name.Name((name,))
records[key_name].rdatasets.append(
TXT.TXT(
TXT_obj = TXT.TXT(
dns.rdataclass.IN,
dns.rdatatype.TXT,
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"""
for record in zone["srv_records"]:
for record in re2o_zone["srv_records"]:
logging.debug(f"SRV = {record}")
# DNS Name obj for SRV
key_name = dns.name.from_text(f"{record['service']}_{record['protocol']}")
records[key_name].rdatasets.append(
SRV.SRV(
SRV_obj = SRV.SRV(
dns.rdataclass.IN,
dns.rdatatype.SRV,
record["priority"],
@ -225,13 +237,13 @@ def srv_records_handler(zone, records):
record["port"],
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"""
for record in zone["a_records"]:
for record in re2o_zone["a_records"]:
logging.debug(f"A = {record}")
@ -241,22 +253,19 @@ def a_records_handler(zone, records):
ipv4_addr = record["ipv4"]
rd = dns.rdataset.Rdataset(dns.rdataclass.IN, dns.rdatatype.A)
A_obj = A.A(
dns.rdataclass.IN,
dns.rdatatype.A,
ipv4_addr
)
rd.add(A_obj)
records[key_name].rdatasets.append(rd)
add_to_zone(dns_zone, key_name, A_obj)
def aaaa_records_handler(zone, records):
def aaaa_records_handler(re2o_zone, dns_zone):
"""Handler for AAAA Record"""
for record in zone["aaaa_records"]:
for record in re2o_zone["aaaa_records"]:
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.")
return
# DNS Name object for Hostname
hostname = record["hostname"]
key_name = dns.name.Name((hostname,))
ipv6_addr = record["ipv6"][0]["ipv6"] # thanks re2o
AAAA_obj = AAAA.AAAA(
@ -272,13 +285,8 @@ def aaaa_records_handler(zone, records):
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)
rd.add(AAAA_obj)
records[key_name].rdatasets.append(rd)
add_to_zone(dns_zone, key_name, AAAA_obj)
def pass_handler(zone, records):