domains.py: Attempt to clarify build_dns_zone?

This commit is contained in:
Alexandre Aubin 2021-08-28 19:08:28 +02:00
parent 9f4ca2e819
commit 756e6041cb

View file

@ -453,7 +453,7 @@ def _get_maindomain():
return maindomain return maindomain
def _build_dns_conf(domain): def _build_dns_conf(base_domain):
""" """
Internal function that will returns a data structure containing the needed Internal function that will returns a data structure containing the needed
information to generate/adapt the dns configuration information to generate/adapt the dns configuration
@ -496,43 +496,40 @@ def _build_dns_conf(domain):
} }
""" """
domains = _get_domain_settings(domain, include_subdomains=True)
basic = [] basic = []
mail = [] mail = []
xmpp = [] xmpp = []
extra = [] extra = []
ipv4 = get_public_ip() ipv4 = get_public_ip()
ipv6 = get_public_ip(6) ipv6 = get_public_ip(6)
owned_dns_zone = (
# TODO test this
"dns_zone" in domains[domain] and domains[domain]["dns_zone"] == domain
)
root_prefix = domain.partition(".")[0] domains_settings = _get_domain_settings(base_domain, include_subdomains=True)
child_domain_suffix = "" base_dns_zone = domain_settings[base_domain].get("dns_zone")
for domain_name, domain in domains.items(): for domain, settings in domain_settings.items():
ttl = domain["ttl"]
if domain_name == domain: # Domain # Base DNS zone # Basename # Suffix #
name = "@" if owned_dns_zone else root_prefix # ------------------ # ----------------- # --------- # -------- #
else: # domain.tld # domain.tld # @ # #
name = domain_name # sub.domain.tld # domain.tld # sub # .sub #
if not owned_dns_zone: # foo.sub.domain.tld # domain.tld # foo.sub # .foo.sub #
name += "." + root_prefix # sub.domain.tld # sub.domain.tld # @ # #
# foo.sub.domain.tld # sub.domain.tld # foo # .foo #
if name != "@": # FIXME: shouldn't the basename just be based on the dns_zone setting of this domain ?
child_domain_suffix = "." + name basename = domain.replace(f"{base_dns_zone}", "").rstrip(".") or "@"
suffix = f".{basename}" if base_name != "@" else ""
ttl = settings["ttl"]
########################### ###########################
# Basic ipv4/ipv6 records # # Basic ipv4/ipv6 records #
########################### ###########################
if ipv4: if ipv4:
basic.append([name, ttl, "A", ipv4]) basic.append([basename, ttl, "A", ipv4])
if ipv6: if ipv6:
basic.append([name, ttl, "AAAA", ipv6]) basic.append([basename, ttl, "AAAA", ipv6])
# TODO # TODO
# elif include_empty_AAAA_if_no_ipv6: # elif include_empty_AAAA_if_no_ipv6:
# basic.append(["@", ttl, "AAAA", None]) # basic.append(["@", ttl, "AAAA", None])
@ -540,46 +537,42 @@ def _build_dns_conf(domain):
######### #########
# Email # # Email #
######### #########
if domain["mail_in"]: if settings["mail_in"]:
mail += [ mail.append([basename, ttl, "MX", f"10 {domain}."])
[name, ttl, "MX", "10 %s." % domain_name]
]
if domain["mail_out"]: if settings["mail_out"]:
mail += [ mail.append([basename, ttl, "TXT", '"v=spf1 a mx -all"'])
[name, ttl, "TXT", '"v=spf1 a mx -all"']
]
# DKIM/DMARC record # DKIM/DMARC record
dkim_host, dkim_publickey = _get_DKIM(domain_name) dkim_host, dkim_publickey = _get_DKIM(domain)
if dkim_host: if dkim_host:
mail += [ mail += [
[dkim_host, ttl, "TXT", dkim_publickey], [f"{dkim_host}{suffix}", ttl, "TXT", dkim_publickey],
[f"_dmarc{child_domain_suffix}", ttl, "TXT", '"v=DMARC1; p=none"'], [f"_dmarc{suffix}", ttl, "TXT", '"v=DMARC1; p=none"'],
] ]
######## ########
# XMPP # # XMPP #
######## ########
if domain["xmpp"]: if settings["xmpp"]:
xmpp += [ xmpp += [
[ [
f"_xmpp-client._tcp{child_domain_suffix}", f"_xmpp-client._tcp{suffix}",
ttl, ttl,
"SRV", "SRV",
f"0 5 5222 {domain_name}.", f"0 5 5222 {domain}.",
], ],
[ [
f"_xmpp-server._tcp{child_domain_suffix}", f"_xmpp-server._tcp{suffix}",
ttl, ttl,
"SRV", "SRV",
f"0 5 5269 {domain_name}.", f"0 5 5269 {domain}.",
], ],
["muc" + child_domain_suffix, ttl, "CNAME", name], [f"muc{suffix}", ttl, "CNAME", basename],
["pubsub" + child_domain_suffix, ttl, "CNAME", name], [f"pubsub{suffix}", ttl, "CNAME", basename],
["vjud" + child_domain_suffix, ttl, "CNAME", name], [f"vjud{suffix}", ttl, "CNAME", basename],
["xmpp-upload" + child_domain_suffix, ttl, "CNAME", name], [f"xmpp-upload{suffix}", ttl, "CNAME", basename],
] ]
######### #########
@ -587,15 +580,15 @@ def _build_dns_conf(domain):
######### #########
if ipv4: if ipv4:
extra.append([f"*{child_domain_suffix}", ttl, "A", ipv4]) extra.append([f"*{suffix}", ttl, "A", ipv4])
if ipv6: if ipv6:
extra.append([f"*{child_domain_suffix}", ttl, "AAAA", ipv6]) extra.append([f"*{suffix}", ttl, "AAAA", ipv6])
# TODO # TODO
# elif include_empty_AAAA_if_no_ipv6: # elif include_empty_AAAA_if_no_ipv6:
# extra.append(["*", ttl, "AAAA", None]) # extra.append(["*", ttl, "AAAA", None])
extra.append([name, ttl, "CAA", '128 issue "letsencrypt.org"']) extra.append([basename, ttl, "CAA", '128 issue "letsencrypt.org"'])
#################### ####################
# Standard records # # Standard records #
@ -626,7 +619,7 @@ def _build_dns_conf(domain):
# Defined by custom hooks ships in apps for example ... # Defined by custom hooks ships in apps for example ...
hook_results = hook_callback("custom_dns_rules", args=[domain]) hook_results = hook_callback("custom_dns_rules", args=[base_domain])
for hook_name, results in hook_results.items(): for hook_name, results in hook_results.items():
# #
# There can be multiple results per hook name, so results look like # There can be multiple results per hook name, so results look like