From 47b9b8b5205f630c8553a184c9c27f29f67571b3 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 20 Dec 2022 19:51:21 +0100 Subject: [PATCH] configpanels: fix inconsistent return format for boolean, sometimes 1/0, sometimes True/False -> force normalization of values when calling get() for a single setting from a config panel --- conf/postfix/main.cf | 4 ++-- helpers/utils | 4 ++++ hooks/conf_regen/03-ssh | 2 +- hooks/conf_regen/15-nginx | 10 +++++----- hooks/conf_regen/19-postfix | 6 +++--- share/config_global.toml | 21 ++++++++++++++++++++ src/tests/test_settings.py | 39 +++++++++++++++++++++---------------- src/utils/config.py | 11 ++++++++++- 8 files changed, 68 insertions(+), 29 deletions(-) diff --git a/conf/postfix/main.cf b/conf/postfix/main.cf index e93d163b3..19b40aefb 100644 --- a/conf/postfix/main.cf +++ b/conf/postfix/main.cf @@ -81,7 +81,7 @@ alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases mydomain = {{ main_domain }} mydestination = localhost -{% if relay_enabled != "1" %} +{% if relay_enabled != "True" %} relayhost = {% else %} relayhost = [{{ relay_host }}]:{{ relay_port }} @@ -198,7 +198,7 @@ smtpd_client_recipient_rate_limit=150 # and after to send spam disable_vrfy_command = yes -{% if relay_enabled == "1" %} +{% if relay_enabled == "True" %} # Relay email through an other smtp account # enable SASL authentication smtp_sasl_auth_enable = yes diff --git a/helpers/utils b/helpers/utils index 2f4a93513..3b1e9c6bb 100644 --- a/helpers/utils +++ b/helpers/utils @@ -957,3 +957,7 @@ _ynh_apply_default_permissions() { chown root:root $target fi } + +int_to_bool() { + sed -e 's/^1$/True/g' -e 's/^0$/False/g' +} diff --git a/hooks/conf_regen/03-ssh b/hooks/conf_regen/03-ssh index 832e07015..d0351b4e5 100755 --- a/hooks/conf_regen/03-ssh +++ b/hooks/conf_regen/03-ssh @@ -17,7 +17,7 @@ do_pre_regen() { # Support different strategy for security configurations export compatibility="$(yunohost settings get 'security.ssh.ssh_compatibility')" export port="$(yunohost settings get 'security.ssh.ssh_port')" - export password_authentication="$(yunohost settings get 'security.ssh.ssh_password_authentication')" + export password_authentication="$(yunohost settings get 'security.ssh.ssh_password_authentication' | int_to_bool)" export ssh_keys export ipv6_enabled ynh_render_template "sshd_config" "${pending_dir}/etc/ssh/sshd_config" diff --git a/hooks/conf_regen/15-nginx b/hooks/conf_regen/15-nginx index aac3ff3e2..28d9e90fb 100755 --- a/hooks/conf_regen/15-nginx +++ b/hooks/conf_regen/15-nginx @@ -56,8 +56,8 @@ do_pre_regen() { # install / update plain conf files cp plain/* "$nginx_conf_dir" # remove the panel overlay if this is specified in settings - panel_overlay=$(yunohost settings get 'misc.portal.ssowat_panel_overlay_enabled') - if [ "$panel_overlay" == "false" ] || [ "$panel_overlay" == "False" ]; then + panel_overlay=$(yunohost settings get 'misc.portal.ssowat_panel_overlay_enabled' | int_to_bool) + if [ "$panel_overlay" == "False" ]; then echo "#" >"${nginx_conf_dir}/yunohost_panel.conf.inc" fi @@ -65,9 +65,9 @@ do_pre_regen() { main_domain=$(cat /etc/yunohost/current_host) # Support different strategy for security configurations - export redirect_to_https="$(yunohost settings get 'security.nginx.nginx_redirect_to_https')" + export redirect_to_https="$(yunohost settings get 'security.nginx.nginx_redirect_to_https' | int_to_bool)" export compatibility="$(yunohost settings get 'security.nginx.nginx_compatibility')" - export experimental="$(yunohost settings get 'security.experimental.security_experimental_enabled')" + export experimental="$(yunohost settings get 'security.experimental.security_experimental_enabled' | int_to_bool)" ynh_render_template "security.conf.inc" "${nginx_conf_dir}/security.conf.inc" cert_status=$(yunohost domain cert status --json) @@ -109,7 +109,7 @@ do_pre_regen() { done - export webadmin_allowlist_enabled=$(yunohost settings get security.webadmin.webadmin_allowlist_enabled) + export webadmin_allowlist_enabled=$(yunohost settings get security.webadmin.webadmin_allowlist_enabled | int_to_bool) if [ "$webadmin_allowlist_enabled" == "True" ]; then export webadmin_allowlist=$(yunohost settings get security.webadmin.webadmin_allowlist) fi diff --git a/hooks/conf_regen/19-postfix b/hooks/conf_regen/19-postfix index 93de29165..3a2aead5d 100755 --- a/hooks/conf_regen/19-postfix +++ b/hooks/conf_regen/19-postfix @@ -29,8 +29,8 @@ do_pre_regen() { export relay_port="" export relay_user="" export relay_host="" - export relay_enabled="$(yunohost settings get 'email.smtp.smtp_relay_enabled')" - if [ "${relay_enabled}" == "1" ]; then + export relay_enabled="$(yunohost settings get 'email.smtp.smtp_relay_enabled' | int_to_bool)" + if [ "${relay_enabled}" == "True" ]; then relay_host="$(yunohost settings get 'email.smtp.smtp_relay_host')" relay_port="$(yunohost settings get 'email.smtp.smtp_relay_port')" relay_user="$(yunohost settings get 'email.smtp.smtp_relay_user')" @@ -56,7 +56,7 @@ do_pre_regen() { >"${default_dir}/postsrsd" # adapt it for IPv4-only hosts - ipv6="$(yunohost settings get 'email.smtp.smtp_allow_ipv6')" + ipv6="$(yunohost settings get 'email.smtp.smtp_allow_ipv6' | int_to_bool)" if [ "$ipv6" == "False" ] || [ ! -f /proc/net/if_inet6 ]; then sed -i \ 's/ \[::ffff:127.0.0.0\]\/104 \[::1\]\/128//g' \ diff --git a/share/config_global.toml b/share/config_global.toml index 1f3cc1b39..dc42baad8 100644 --- a/share/config_global.toml +++ b/share/config_global.toml @@ -160,3 +160,24 @@ name = "Other" [misc.backup.backup_compress_tar_archives] type = "boolean" default = false + +[example] + [example.example] + [example.example.boolean] + type = "boolean" + yes = "True" + no = "False" + default = "True" + + [example.example.number] + type = "number" + default = 42 + + [example.example.string] + type = "string" + default = "yolo swag" + + [example.example.select] + type = "select" + choices = ["a", "b", "c"] + default = "a" diff --git a/src/tests/test_settings.py b/src/tests/test_settings.py index c52691342..2eaebba55 100644 --- a/src/tests/test_settings.py +++ b/src/tests/test_settings.py @@ -1,6 +1,7 @@ import os import pytest import yaml +from mock import patch import moulinette from yunohost.utils.error import YunohostError, YunohostValidationError @@ -152,10 +153,10 @@ def test_settings_get_doesnt_exists(): def test_settings_set(): settings_set("example.example.boolean", False) - assert settings_get("example.example.boolean") is False + assert settings_get("example.example.boolean") == 0 settings_set("example.example.boolean", "on") - assert settings_get("example.example.boolean") is True + assert settings_get("example.example.boolean") == 1 def test_settings_set_int(): @@ -174,35 +175,39 @@ def test_settings_set_doesexit(): def test_settings_set_bad_type_bool(): - with pytest.raises(YunohostError): - settings_set("example.example.boolean", 42) - with pytest.raises(YunohostError): - settings_set("example.example.boolean", "pouet") + + with patch.object(os, "isatty", return_value=False): + with pytest.raises(YunohostError): + settings_set("example.example.boolean", 42) + with pytest.raises(YunohostError): + settings_set("example.example.boolean", "pouet") def test_settings_set_bad_type_int(): # with pytest.raises(YunohostError): # settings_set("example.example.number", True) - with pytest.raises(YunohostError): - settings_set("example.example.number", "pouet") + with patch.object(os, "isatty", return_value=False): + with pytest.raises(YunohostError): + settings_set("example.example.number", "pouet") # def test_settings_set_bad_type_string(): # with pytest.raises(YunohostError): -# settings_set("example.example.string", True) +# settings_set(eexample.example.string", True) # with pytest.raises(YunohostError): # settings_set("example.example.string", 42) def test_settings_set_bad_value_select(): - with pytest.raises(YunohostError): - settings_set("example.example.select", True) - with pytest.raises(YunohostError): - settings_set("example.example.select", "e") - with pytest.raises(YunohostError): - settings_set("example.example.select", 42) - with pytest.raises(YunohostError): - settings_set("example.example.select", "pouet") + with patch.object(os, "isatty", return_value=False): + with pytest.raises(YunohostError): + settings_set("example.example.select", True) + with pytest.raises(YunohostError): + settings_set("example.example.select", "e") + with pytest.raises(YunohostError): + settings_set("example.example.select", 42) + with pytest.raises(YunohostError): + settings_set("example.example.select", "pouet") def test_settings_list_modified(): diff --git a/src/utils/config.py b/src/utils/config.py index 072362f97..27e4b9509 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -264,8 +264,17 @@ class ConfigPanel: # In 'classic' mode, we display the current value if key refer to an option if self.filter_key.count(".") == 2 and mode == "classic": + option = self.filter_key.split(".")[-1] - return self.values.get(option, None) + value = self.values.get(option, None) + + option_type = None + for _, _, option_ in self._iterate(): + if option_["id"] == option: + option_type = ARGUMENTS_TYPE_PARSERS[option_["type"]] + break + + return option_type.normalize(value) if option_type else value # Format result in 'classic' or 'export' mode logger.debug(f"Formating result in '{mode}' mode")