Reformat everything.

This commit is contained in:
Felix Fontein
2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View File

@@ -252,8 +252,30 @@ try:
import dns.resolver
import dns.reversename
import dns.rdataclass
from dns.rdatatype import (A, AAAA, CAA, CNAME, DNAME, DNSKEY, DS, HINFO, LOC,
MX, NAPTR, NS, NSEC3PARAM, PTR, RP, SOA, SPF, SRV, SSHFP, TLSA, TXT)
from dns.rdatatype import (
A,
AAAA,
CAA,
CNAME,
DNAME,
DNSKEY,
DS,
HINFO,
LOC,
MX,
NAPTR,
NS,
NSEC3PARAM,
PTR,
RP,
SOA,
SPF,
SRV,
SSHFP,
TLSA,
TXT,
)
HAVE_DNS = True
except ImportError:
HAVE_DNS = False
@@ -263,35 +285,35 @@ display = Display()
def make_rdata_dict(rdata):
''' While the 'dig' lookup plugin supports anything which dnspython supports
out of the box, the following supported_types list describes which
DNS query types we can convert to a dict.
"""While the 'dig' lookup plugin supports anything which dnspython supports
out of the box, the following supported_types list describes which
DNS query types we can convert to a dict.
Note: adding support for RRSIG is hard work. :)
'''
Note: adding support for RRSIG is hard work. :)
"""
supported_types = {
A: ['address'],
AAAA: ['address'],
CAA: ['flags', 'tag', 'value'],
CNAME: ['target'],
DNAME: ['target'],
DNSKEY: ['flags', 'algorithm', 'protocol', 'key'],
DS: ['algorithm', 'digest_type', 'key_tag', 'digest'],
HINFO: ['cpu', 'os'],
LOC: ['latitude', 'longitude', 'altitude', 'size', 'horizontal_precision', 'vertical_precision'],
MX: ['preference', 'exchange'],
NAPTR: ['order', 'preference', 'flags', 'service', 'regexp', 'replacement'],
NS: ['target'],
NSEC3PARAM: ['algorithm', 'flags', 'iterations', 'salt'],
PTR: ['target'],
RP: ['mbox', 'txt'],
A: ["address"],
AAAA: ["address"],
CAA: ["flags", "tag", "value"],
CNAME: ["target"],
DNAME: ["target"],
DNSKEY: ["flags", "algorithm", "protocol", "key"],
DS: ["algorithm", "digest_type", "key_tag", "digest"],
HINFO: ["cpu", "os"],
LOC: ["latitude", "longitude", "altitude", "size", "horizontal_precision", "vertical_precision"],
MX: ["preference", "exchange"],
NAPTR: ["order", "preference", "flags", "service", "regexp", "replacement"],
NS: ["target"],
NSEC3PARAM: ["algorithm", "flags", "iterations", "salt"],
PTR: ["target"],
RP: ["mbox", "txt"],
# RRSIG: ['type_covered', 'algorithm', 'labels', 'original_ttl', 'expiration', 'inception', 'key_tag', 'signer', 'signature'],
SOA: ['mname', 'rname', 'serial', 'refresh', 'retry', 'expire', 'minimum'],
SPF: ['strings'],
SRV: ['priority', 'weight', 'port', 'target'],
SSHFP: ['algorithm', 'fp_type', 'fingerprint'],
TLSA: ['usage', 'selector', 'mtype', 'cert'],
TXT: ['strings'],
SOA: ["mname", "rname", "serial", "refresh", "retry", "expire", "minimum"],
SPF: ["strings"],
SRV: ["priority", "weight", "port", "target"],
SSHFP: ["algorithm", "fp_type", "fingerprint"],
TLSA: ["usage", "selector", "mtype", "cert"],
TXT: ["strings"],
}
rd = {}
@@ -304,18 +326,18 @@ def make_rdata_dict(rdata):
if isinstance(val, dns.name.Name):
val = dns.name.Name.to_text(val)
if rdata.rdtype == DS and f == 'digest':
val = dns.rdata._hexify(rdata.digest).replace(' ', '')
if rdata.rdtype == DNSKEY and f == 'algorithm':
if rdata.rdtype == DS and f == "digest":
val = dns.rdata._hexify(rdata.digest).replace(" ", "")
if rdata.rdtype == DNSKEY and f == "algorithm":
val = int(val)
if rdata.rdtype == DNSKEY and f == 'key':
val = dns.rdata._base64ify(rdata.key).replace(' ', '')
if rdata.rdtype == NSEC3PARAM and f == 'salt':
val = dns.rdata._hexify(rdata.salt).replace(' ', '')
if rdata.rdtype == SSHFP and f == 'fingerprint':
val = dns.rdata._hexify(rdata.fingerprint).replace(' ', '')
if rdata.rdtype == TLSA and f == 'cert':
val = dns.rdata._hexify(rdata.cert).replace(' ', '')
if rdata.rdtype == DNSKEY and f == "key":
val = dns.rdata._base64ify(rdata.key).replace(" ", "")
if rdata.rdtype == NSEC3PARAM and f == "salt":
val = dns.rdata._hexify(rdata.salt).replace(" ", "")
if rdata.rdtype == SSHFP and f == "fingerprint":
val = dns.rdata._hexify(rdata.fingerprint).replace(" ", "")
if rdata.rdtype == TLSA and f == "cert":
val = dns.rdata._hexify(rdata.cert).replace(" ", "")
rd[f] = val
@@ -327,11 +349,10 @@ def make_rdata_dict(rdata):
#
# --------------------------------------------------------------
class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
'''
"""
terms contains a string with things to `dig' for. We support the
following formats:
example.com # A record
@@ -344,7 +365,7 @@ class LookupModule(LookupBase):
^^^ can be comma-sep list of names/addresses
... flat=0 # returns a dict; default is 1 == string
'''
"""
if HAVE_DNS is False:
raise AnsibleError("The dig lookup requires the python 'dnspython' library and it is not installed")
@@ -357,21 +378,21 @@ class LookupModule(LookupBase):
domains = []
nameservers = []
qtype = self.get_option('qtype')
flat = self.get_option('flat')
fail_on_error = self.get_option('fail_on_error')
real_empty = self.get_option('real_empty')
tcp = self.get_option('tcp')
port = self.get_option('port')
qtype = self.get_option("qtype")
flat = self.get_option("flat")
fail_on_error = self.get_option("fail_on_error")
real_empty = self.get_option("real_empty")
tcp = self.get_option("tcp")
port = self.get_option("port")
try:
rdclass = dns.rdataclass.from_text(self.get_option('class'))
rdclass = dns.rdataclass.from_text(self.get_option("class"))
except Exception as e:
raise AnsibleError(f"dns lookup illegal CLASS: {e}")
myres.retry_servfail = self.get_option('retry_servfail')
myres.retry_servfail = self.get_option("retry_servfail")
for t in terms:
if t.startswith('@'): # e.g. "@10.0.1.2,192.0.2.1" is ok.
nsset = t[1:].split(',')
if t.startswith("@"): # e.g. "@10.0.1.2,192.0.2.1" is ok.
nsset = t[1:].split(",")
for ns in nsset:
# Check if we have a valid IP address. If so, use that, otherwise
# try to resolve name to address using system's resolver. If that
@@ -386,35 +407,35 @@ class LookupModule(LookupBase):
except Exception as e:
raise AnsibleError(f"dns lookup NS: {e}")
continue
if '=' in t:
if "=" in t:
try:
opt, arg = t.split('=', 1)
opt, arg = t.split("=", 1)
except Exception:
pass
if opt == 'qtype':
if opt == "qtype":
qtype = arg.upper()
elif opt == 'flat':
elif opt == "flat":
flat = int(arg)
elif opt == 'class':
elif opt == "class":
try:
rdclass = dns.rdataclass.from_text(arg)
except Exception as e:
raise AnsibleError(f"dns lookup illegal CLASS: {e}")
elif opt == 'retry_servfail':
elif opt == "retry_servfail":
myres.retry_servfail = boolean(arg)
elif opt == 'fail_on_error':
elif opt == "fail_on_error":
fail_on_error = boolean(arg)
elif opt == 'real_empty':
elif opt == "real_empty":
real_empty = boolean(arg)
elif opt == 'tcp':
elif opt == "tcp":
tcp = boolean(arg)
continue
if '/' in t:
if "/" in t:
try:
domain, qtype = t.split('/')
domain, qtype = t.split("/")
domains.append(domain)
except Exception:
domains.append(t)
@@ -428,7 +449,7 @@ class LookupModule(LookupBase):
if len(nameservers) > 0:
myres.nameservers = nameservers
if qtype.upper() == 'PTR':
if qtype.upper() == "PTR":
reversed_domains = []
for domain in domains:
try:
@@ -450,7 +471,7 @@ class LookupModule(LookupBase):
answers = myres.query(domain, qtype, rdclass=rdclass, tcp=tcp)
for rdata in answers:
s = rdata.to_text()
if qtype.upper() == 'TXT':
if qtype.upper() == "TXT":
s = s[1:-1] # Strip outside quotes on TXT rdata
if flat:
@@ -458,10 +479,10 @@ class LookupModule(LookupBase):
else:
try:
rd = make_rdata_dict(rdata)
rd['owner'] = answers.canonical_name.to_text()
rd['type'] = dns.rdatatype.to_text(rdata.rdtype)
rd['ttl'] = answers.rrset.ttl
rd['class'] = dns.rdataclass.to_text(rdata.rdclass)
rd["owner"] = answers.canonical_name.to_text()
rd["type"] = dns.rdatatype.to_text(rdata.rdtype)
rd["ttl"] = answers.rrset.ttl
rd["class"] = dns.rdataclass.to_text(rdata.rdclass)
ret.append(rd)
except Exception as err:
@@ -473,7 +494,7 @@ class LookupModule(LookupBase):
if fail_on_error:
raise AnsibleError(f"Lookup failed: {err}")
if not real_empty:
ret.append('NXDOMAIN')
ret.append("NXDOMAIN")
except (dns.resolver.NoAnswer, dns.resolver.Timeout, dns.resolver.NoNameservers) as err:
if fail_on_error:
raise AnsibleError(f"Lookup failed: {err}")