First implementation of configurable dns conf generation

This commit is contained in:
MercierCorentin 2021-03-09 22:57:46 +01:00
parent 9a8cbbd883
commit c111b9c6c2
2 changed files with 83 additions and 70 deletions

View file

@ -467,13 +467,6 @@ domain:
arguments: arguments:
domain: domain:
help: Target domain help: Target domain
-t:
full: --ttl
help: Time To Live (TTL) in second before DNS servers update. Default is 3600 seconds (i.e. 1 hour).
extra:
pattern:
- !!str ^[0-9]+$
- "pattern_positive_number"
### domain_maindomain() ### domain_maindomain()
main-domain: main-domain:

View file

@ -25,6 +25,7 @@
""" """
import os import os
import re import re
import sys
from moulinette import m18n, msettings, msignals from moulinette import m18n, msettings, msignals
from moulinette.core import MoulinetteError from moulinette.core import MoulinetteError
@ -275,22 +276,21 @@ def domain_remove(operation_logger, domain, remove_apps=False, force=False):
logger.success(m18n.n("domain_deleted")) logger.success(m18n.n("domain_deleted"))
def domain_dns_conf(domain, ttl=None): def domain_dns_conf(domain):
""" """
Generate DNS configuration for a domain Generate DNS configuration for a domain
Keyword argument: Keyword argument:
domain -- Domain name domain -- Domain name
ttl -- Time to live
""" """
if domain not in domain_list()["domains"]: if domain not in domain_list()["domains"]:
raise YunohostError("domain_name_unknown", domain=domain) raise YunohostError("domain_name_unknown", domain=domain)
ttl = 3600 if ttl is None else ttl domains_settings = _get_domain_and_subdomains_settings(domain)
dns_conf = _build_dns_conf(domain, ttl) dns_conf = _build_dns_conf(domains_settings)
result = "" result = ""
@ -411,7 +411,7 @@ def _get_maindomain():
return maindomain return maindomain
def _build_dns_conf(domain, ttl=3600, include_empty_AAAA_if_no_ipv6=False): def _build_dns_conf(domains):
""" """
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
@ -451,29 +451,50 @@ def _build_dns_conf(domain, ttl=3600, include_empty_AAAA_if_no_ipv6=False):
} }
""" """
root = min(domains.keys(), key=(lambda k: len(k)))
basic = []
mail = []
xmpp = []
extra = []
ipv4 = get_public_ip() ipv4 = get_public_ip()
ipv6 = get_public_ip(6) ipv6 = get_public_ip(6)
name_prefix = root.partition(".")[0]
for domain_name, domain in domains.items():
print(domain_name)
ttl = domain["ttl"]
owned_dns_zone = "owned_dns_zone" in domains[root] and domains[root]["owned_dns_zone"] == True
if domain_name == root:
name = name_prefix if not owned_dns_zone else "@"
else:
name = domain_name[0:-(1 + len(root))]
if not owned_dns_zone:
name += "." + name_prefix
########################### ###########################
# Basic ipv4/ipv6 records # # Basic ipv4/ipv6 records #
########################### ###########################
basic = []
if ipv4: if ipv4:
basic.append(["@", ttl, "A", ipv4]) basic.append([name, ttl, "A", ipv4])
if ipv6: if ipv6:
basic.append(["@", ttl, "AAAA", ipv6]) basic.append([name, ttl, "AAAA", ipv6])
elif include_empty_AAAA_if_no_ipv6: # TODO
basic.append(["@", ttl, "AAAA", None]) # elif include_empty_AAAA_if_no_ipv6:
# basic.append(["@", ttl, "AAAA", None])
######### #########
# Email # # Email #
######### #########
if domain["mail"] == True:
mail = [ mail += [
["@", ttl, "MX", "10 %s." % domain], [name, ttl, "MX", "10 %s." % domain],
["@", ttl, "TXT", '"v=spf1 a mx -all"'], [name, ttl, "TXT", '"v=spf1 a mx -all"'],
] ]
# DKIM/DMARC record # DKIM/DMARC record
@ -488,21 +509,20 @@ def _build_dns_conf(domain, ttl=3600, include_empty_AAAA_if_no_ipv6=False):
######## ########
# XMPP # # XMPP #
######## ########
if domain["xmpp"] == True:
xmpp = [ xmpp += [
["_xmpp-client._tcp", ttl, "SRV", "0 5 5222 %s." % domain], ["_xmpp-client._tcp", ttl, "SRV", "0 5 5222 %s." % domain_name],
["_xmpp-server._tcp", ttl, "SRV", "0 5 5269 %s." % domain], ["_xmpp-server._tcp", ttl, "SRV", "0 5 5269 %s." % domain_name],
["muc", ttl, "CNAME", "@"], ["muc", ttl, "CNAME", name],
["pubsub", ttl, "CNAME", "@"], ["pubsub", ttl, "CNAME", name],
["vjud", ttl, "CNAME", "@"], ["vjud", ttl, "CNAME", name],
["xmpp-upload", ttl, "CNAME", "@"], ["xmpp-upload", ttl, "CNAME", name],
] ]
######### #########
# Extra # # Extra #
######### #########
extra = []
if ipv4: if ipv4:
extra.append(["*", ttl, "A", ipv4]) extra.append(["*", ttl, "A", ipv4])
@ -512,7 +532,7 @@ def _build_dns_conf(domain, ttl=3600, include_empty_AAAA_if_no_ipv6=False):
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(["@", ttl, "CAA", '128 issue "letsencrypt.org"']) extra.append([name, ttl, "CAA", '128 issue "letsencrypt.org"'])
#################### ####################
# Standard records # # Standard records #
@ -665,17 +685,17 @@ def _get_domain_and_subdomains_settings(domain):
Give data about a domain and its subdomains Give data about a domain and its subdomains
""" """
return { return {
"cmercier.fr" : { "node.cmercier.fr" : {
"main": true, "main": True,
"xmpp": true, "xmpp": True,
"mail": true, "mail": True,
"owned_dns_zone": true, "owned_dns_zone": True,
"ttl": 3600, "ttl": 3600,
}, },
"node.cmercier.fr" : { "sub.node.cmercier.fr" : {
"main": false, "main": False,
"xmpp": false, "xmpp": True,
"mail": false, "mail": False,
"ttl": 3600, "ttl": 3600,
}, },
} }