Add a small _assert_domain_exists to avoid always repeating the same code snippet

This commit is contained in:
Alexandre Aubin 2021-09-01 16:53:28 +02:00
parent 01bc6762aa
commit d5b1eecd07
6 changed files with 33 additions and 49 deletions

View file

@ -1340,7 +1340,7 @@ def app_makedefault(operation_logger, app, domain=None):
domain domain
""" """
from yunohost.domain import domain_list from yunohost.domain import _assert_domain_exists
app_settings = _get_app_settings(app) app_settings = _get_app_settings(app)
app_domain = app_settings["domain"] app_domain = app_settings["domain"]
@ -1348,9 +1348,10 @@ def app_makedefault(operation_logger, app, domain=None):
if domain is None: if domain is None:
domain = app_domain domain = app_domain
operation_logger.related_to.append(("domain", domain))
elif domain not in domain_list()["domains"]: _assert_domain_exists(domain)
raise YunohostValidationError("domain_name_unknown", domain=domain)
operation_logger.related_to.append(("domain", domain))
if "/" in app_map(raw=True)[domain]: if "/" in app_map(raw=True)[domain]:
raise YunohostValidationError( raise YunohostValidationError(
@ -3078,13 +3079,12 @@ def _get_conflicting_apps(domain, path, ignore_app=None):
ignore_app -- An optional app id to ignore (c.f. the change_url usecase) ignore_app -- An optional app id to ignore (c.f. the change_url usecase)
""" """
from yunohost.domain import domain_list from yunohost.domain import _assert_domain_exists
domain, path = _normalize_domain_path(domain, path) domain, path = _normalize_domain_path(domain, path)
# Abort if domain is unknown # Abort if domain is unknown
if domain not in domain_list()["domains"]: _assert_domain_exists(domain)
raise YunohostValidationError("domain_name_unknown", domain=domain)
# Fetch apps map # Fetch apps map
apps_map = app_map(raw=True) apps_map = app_map(raw=True)

View file

@ -86,11 +86,8 @@ def certificate_status(domain_list, full=False):
domain_list = yunohost.domain.domain_list()["domains"] domain_list = yunohost.domain.domain_list()["domains"]
# Else, validate that yunohost knows the domains given # Else, validate that yunohost knows the domains given
else: else:
yunohost_domains_list = yunohost.domain.domain_list()["domains"]
for domain in domain_list: for domain in domain_list:
# Is it in Yunohost domain list? yunohost.domain._assert_domain_exists(domain)
if domain not in yunohost_domains_list:
raise YunohostValidationError("domain_name_unknown", domain=domain)
certificates = {} certificates = {}
@ -267,9 +264,7 @@ def _certificate_install_letsencrypt(
# Else, validate that yunohost knows the domains given # Else, validate that yunohost knows the domains given
else: else:
for domain in domain_list: for domain in domain_list:
yunohost_domains_list = yunohost.domain.domain_list()["domains"] yunohost.domain._assert_domain_exists(domain)
if domain not in yunohost_domains_list:
raise YunohostValidationError("domain_name_unknown", domain=domain)
# Is it self-signed? # Is it self-signed?
status = _get_status(domain) status = _get_status(domain)
@ -368,9 +363,8 @@ def certificate_renew(
else: else:
for domain in domain_list: for domain in domain_list:
# Is it in Yunohost dmomain list? # Is it in Yunohost domain list?
if domain not in yunohost.domain.domain_list()["domains"]: yunohost.domain._assert_domain_exists(domain)
raise YunohostValidationError("domain_name_unknown", domain=domain)
status = _get_status(domain) status = _get_status(domain)

View file

@ -30,7 +30,7 @@ from moulinette import m18n, Moulinette
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
from moulinette.utils.filesystem import mkdir, read_yaml, write_to_yaml from moulinette.utils.filesystem import mkdir, read_yaml, write_to_yaml
from yunohost.domain import domain_list, _get_domain_settings from yunohost.domain import domain_list, _get_domain_settings, _assert_domain_exists
from yunohost.app import _parse_args_in_yunohost_format from yunohost.app import _parse_args_in_yunohost_format
from yunohost.utils.error import YunohostValidationError from yunohost.utils.error import YunohostValidationError
from yunohost.utils.network import get_public_ip from yunohost.utils.network import get_public_ip
@ -52,8 +52,7 @@ def domain_dns_conf(domain):
""" """
if domain not in domain_list()["domains"]: _assert_domain_exists(domain)
raise YunohostValidationError("domain_name_unknown", domain=domain)
dns_conf = _build_dns_conf(domain) dns_conf = _build_dns_conf(domain)
@ -94,13 +93,10 @@ def domain_dns_conf(domain):
def _list_subdomains_of(parent_domain): def _list_subdomains_of(parent_domain):
domain_list_ = domain_list()["domains"] _assert_domain_exists(parent_domain)
if parent_domain not in domain_list_:
raise YunohostError("domain_name_unknown", domain=domain)
out = [] out = []
for domain in domain_list_: for domain in domain_list()["domains"]:
if domain.endswith(f".{parent_domain}"): if domain.endswith(f".{parent_domain}"):
out.append(domain) out.append(domain)
@ -462,8 +458,7 @@ def domain_registrar_push(operation_logger, domain):
from lexicon.client import Client as LexiconClient from lexicon.client import Client as LexiconClient
from lexicon.config import ConfigResolver as LexiconConfigResolver from lexicon.config import ConfigResolver as LexiconConfigResolver
if domain not in domain_list()["domains"]: _assert_domain_exists(domain)
raise YunohostValidationError("domain_name_unknown", domain=domain)
dns_zone = _get_domain_settings(domain)["dns_zone"] dns_zone = _get_domain_settings(domain)["dns_zone"]
registrar_settings = _get_registrar_settingss(dns_zone) registrar_settings = _get_registrar_settingss(dns_zone)

View file

@ -96,6 +96,11 @@ def domain_list(exclude_subdomains=False):
return domain_list_cache return domain_list_cache
def _assert_domain_exists(domain):
if domain not in domain_list()["domains"]:
raise YunohostValidationError("domain_name_unknown", domain=domain)
@is_unit_operation() @is_unit_operation()
def domain_add(operation_logger, domain, dyndns=False): def domain_add(operation_logger, domain, dyndns=False):
""" """
@ -216,8 +221,8 @@ def domain_remove(operation_logger, domain, remove_apps=False, force=False):
# the 'force' here is related to the exception happening in domain_add ... # the 'force' here is related to the exception happening in domain_add ...
# we don't want to check the domain exists because the ldap add may have # we don't want to check the domain exists because the ldap add may have
# failed # failed
if not force and domain not in domain_list()["domains"]: if not force:
raise YunohostValidationError("domain_name_unknown", domain=domain) _assert_domain_exists(domain)
# Check domain is not the main domain # Check domain is not the main domain
if domain == _get_maindomain(): if domain == _get_maindomain():
@ -339,8 +344,7 @@ def domain_main_domain(operation_logger, new_main_domain=None):
return {"current_main_domain": _get_maindomain()} return {"current_main_domain": _get_maindomain()}
# Check domain exists # Check domain exists
if new_main_domain not in domain_list()["domains"]: _assert_domain_exists(new_main_domain)
raise YunohostValidationError("domain_name_unknown", domain=new_main_domain)
operation_logger.related_to.append(("domain", new_main_domain)) operation_logger.related_to.append(("domain", new_main_domain))
operation_logger.start() operation_logger.start()
@ -399,12 +403,7 @@ def _get_domain_settings(domain):
Retrieve entries in /etc/yunohost/domains/[domain].yml Retrieve entries in /etc/yunohost/domains/[domain].yml
And set default values if needed And set default values if needed
""" """
# Retrieve actual domain list _assert_domain_exists(domain)
known_domains = domain_list()["domains"]
maindomain = domain_list()["main"]
if domain not in known_domains:
raise YunohostValidationError("domain_name_unknown", domain=domain)
# Retrieve entries in the YAML # Retrieve entries in the YAML
filepath = f"{DOMAIN_SETTINGS_DIR}/{domain}.yml" filepath = f"{DOMAIN_SETTINGS_DIR}/{domain}.yml"
@ -485,8 +484,8 @@ def _set_domain_settings(domain, domain_settings):
settings -- Dict with domain settings settings -- Dict with domain settings
""" """
if domain not in domain_list()["domains"]:
raise YunohostError("domain_name_unknown", domain=domain) _assert_domain_exists(domain)
defaults = _default_domain_settings(domain) defaults = _default_domain_settings(domain)
diff_with_defaults = {k: v for k, v in domain_settings.items() if defaults.get(k) != v} diff_with_defaults = {k: v for k, v in domain_settings.items() if defaults.get(k) != v}

View file

@ -860,11 +860,9 @@ def _validate_and_sanitize_permission_url(url, app_base_path, app):
re:^/api/.*|/scripts/api.js$ re:^/api/.*|/scripts/api.js$
""" """
from yunohost.domain import domain_list from yunohost.domain import _assert_domain_exists
from yunohost.app import _assert_no_conflicting_apps from yunohost.app import _assert_no_conflicting_apps
domains = domain_list()["domains"]
# #
# Regexes # Regexes
# #
@ -896,8 +894,8 @@ def _validate_and_sanitize_permission_url(url, app_base_path, app):
domain, path = url[3:].split("/", 1) domain, path = url[3:].split("/", 1)
path = "/" + path path = "/" + path
if domain.replace("%", "").replace("\\", "") not in domains: domain_with_no_regex = domain.replace("%", "").replace("\\", "")
raise YunohostValidationError("domain_name_unknown", domain=domain) _assert_domain_exists(domain_with_no_regex)
validate_regex(path) validate_regex(path)
@ -931,8 +929,7 @@ def _validate_and_sanitize_permission_url(url, app_base_path, app):
domain, path = split_domain_path(url) domain, path = split_domain_path(url)
sanitized_url = domain + path sanitized_url = domain + path
if domain not in domains: _assert_domain_exists(domain)
raise YunohostValidationError("domain_name_unknown", domain=domain)
_assert_no_conflicting_apps(domain, path, ignore_app=app) _assert_no_conflicting_apps(domain, path, ignore_app=app)

View file

@ -101,7 +101,7 @@ def user_create(
mail=None, mail=None,
): ):
from yunohost.domain import domain_list, _get_maindomain from yunohost.domain import domain_list, _get_maindomain, _assert_domain_exists
from yunohost.hook import hook_callback from yunohost.hook import hook_callback
from yunohost.utils.password import assert_password_is_strong_enough from yunohost.utils.password import assert_password_is_strong_enough
from yunohost.utils.ldap import _get_ldap_interface from yunohost.utils.ldap import _get_ldap_interface
@ -135,8 +135,7 @@ def user_create(
domain = maindomain domain = maindomain
# Check that the domain exists # Check that the domain exists
if domain not in domain_list()["domains"]: _assert_domain_exists(domain)
raise YunohostValidationError("domain_name_unknown", domain=domain)
mail = username + "@" + domain mail = username + "@" + domain
ldap = _get_ldap_interface() ldap = _get_ldap_interface()