Detecting duplicate CNAMES

23 views
Skip to first unread message

Michael Nielsen

unread,
Jun 26, 2023, 10:03:00 PM6/26/23
to dnspython-users
Hi,

I would like to do some pre-processing on BIND-format files during our CI/CD for DNS-- in particular, it would be very helpful to detect duplicate CNAMES, which cause the version of BIND we are using to refuse the zone file.

Creating a zone from a file with dnspython replaces duplicate CNAMES with just one, and so the zone files are happily parsed by dnspython, with no available indication that a duplicate CNAME has been processed. 

I'm not overly excited about writing a BIND file parser myself, so was hoping to leverage dnspython for the task of detecting duplicate CNAMES.  Is there a way to do this?

Any suggestions would be gratefully received!

Thanks

Mike

Bob Halley

unread,
Jun 26, 2023, 10:56:57 PM6/26/23
to dnspython-users
While dnspython doesn't have a clean API way to do what you want, it's still easy to do if you're willing to alter the CNAME processing for a whole python program (as opposed to selectively).

import sys

import dns.rdatatype
import dns.zone

# Disable singleton processing for CNAMEs in all of dnspython
# Just add() the CNAME type back to undo this change.
dns.rdatatype._singletons.remove(dns.rdatatype.RdataType.CNAME)

zone = dns.zone.from_file("example.db", "example")

exit_code = 0
for (name, rdataset) in zone.iterate_rdatasets("CNAME"):
    if len(rdataset) > 1:
        print(f"{name} has more than one CNAME RR")
        exit_code = 1

sys.exit(exit_code)

Michael Nielsen

unread,
Jun 27, 2023, 9:28:21 AM6/27/23
to dnspython-users
Wow, many thanks!  Exactly what I was looking for!

Thanks again!

Reply all
Reply to author
Forward
0 new messages