From 418f350b11cec0d184d26cb28b1211c2ffe47317 Mon Sep 17 00:00:00 2001 From: Spitfireap Date: Mon, 15 Jul 2024 18:43:52 +0200 Subject: [PATCH 1/4] Removed serializer check for dkim dir, added alarms on gen --- modoboa/admin/api/v2/serializers.py | 4 ---- modoboa/admin/constants.py | 2 ++ .../commands/subcommands/_manage_dkim_keys.py | 18 +++++++++++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/modoboa/admin/api/v2/serializers.py b/modoboa/admin/api/v2/serializers.py index 081af930f..c51e68086 100644 --- a/modoboa/admin/api/v2/serializers.py +++ b/modoboa/admin/api/v2/serializers.py @@ -228,10 +228,6 @@ class AdminGlobalParametersSerializer(serializers.Serializer): def validate_dkim_keys_storage_dir(self, value): """Check that directory exists.""" if value: - if not os.path.isdir(value): - raise serializers.ValidationError(_("Directory not found.")) - if not os.access(value, os.W_OK): - raise serializers.ValidationError(_("Directory non-writable")) code, output = exec_cmd("which openssl") if code: raise serializers.ValidationError( diff --git a/modoboa/admin/constants.py b/modoboa/admin/constants.py index aaa2d2bc6..20f3fd79c 100644 --- a/modoboa/admin/constants.py +++ b/modoboa/admin/constants.py @@ -29,6 +29,8 @@ DKIM_KEY_LENGTHS = [ ] DKIM_WRITE_ERROR = "DKIM path error" +DKIM_DIR_NOT_FOUND = "DKIM directory not found" +DKIM_ERROR = "general DKIM generation error" ALARM_OPENED = 1 ALARM_CLOSED = 2 diff --git a/modoboa/admin/management/commands/subcommands/_manage_dkim_keys.py b/modoboa/admin/management/commands/subcommands/_manage_dkim_keys.py index 7f5a61275..e2148d6df 100644 --- a/modoboa/admin/management/commands/subcommands/_manage_dkim_keys.py +++ b/modoboa/admin/management/commands/subcommands/_manage_dkim_keys.py @@ -10,8 +10,7 @@ from modoboa.lib import sysutils from modoboa.parameters import tools as param_tools from .... import models -from .... import signals -from ....constants import DKIM_WRITE_ERROR, ALARM_OPENED +from ....constants import DKIM_WRITE_ERROR, ALARM_OPENED, DKIM_DIR_NOT_FOUND, DKIM_ERROR class ManageDKIMKeys(BaseCommand): @@ -21,11 +20,16 @@ class ManageDKIMKeys(BaseCommand): """Create a new DKIM key.""" storage_dir = param_tools.get_global_parameter("dkim_keys_storage_dir") pkey_path = os.path.join(storage_dir, "{}.pem".format(domain.name)) + alarm_qset = domain.alarms.filter(internal_name=DKIM_WRITE_ERROR) if not os.access(storage_dir, os.W_OK): if not alarm_qset.exists(): domain.alarms.create( - title=_("DKIM path non-writable"), internal_name=DKIM_WRITE_ERROR + title=_( + "DKIM path non-writable " + "(either a permission issue or the directory does not exist)" + ), + internal_name=DKIM_WRITE_ERROR, ) else: alarm = alarm_qset.first() @@ -48,6 +52,10 @@ class ManageDKIMKeys(BaseCommand): domain.name, smart_str(output) ) ) + domain.alarms.create( + title=_("Failed to generate DKIM private key"), internal_name=DKIM_ERROR + ) + return domain.dkim_private_key_path = pkey_path code, output = sysutils.exec_cmd("openssl rsa -in {} -pubout".format(pkey_path)) if code: @@ -56,6 +64,10 @@ class ManageDKIMKeys(BaseCommand): domain.name, smart_str(output) ) ) + domain.alarms.create( + title=_("Failed to generate DKIM public key"), internal_name=DKIM_ERROR + ) + return public_key = "" for cpt, line in enumerate(smart_str(output).splitlines()): if cpt == 0 or line.startswith("-----"): From 930299f42ef95ab43d696e9ec076c0184b4d5863 Mon Sep 17 00:00:00 2001 From: Spitfireap Date: Mon, 15 Jul 2024 18:52:20 +0200 Subject: [PATCH 2/4] Updated tests --- modoboa/parameters/api/v2/tests.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/modoboa/parameters/api/v2/tests.py b/modoboa/parameters/api/v2/tests.py index 24494fa72..6cf783698 100644 --- a/modoboa/parameters/api/v2/tests.py +++ b/modoboa/parameters/api/v2/tests.py @@ -6,7 +6,6 @@ from modoboa.lib.tests import ModoAPITestCase class ParametersAPITestCase(ModoAPITestCase): - def test_applications(self): url = reverse("v2:parameter-applications") resp = self.client.get(url) @@ -69,15 +68,6 @@ class ParametersAPITestCase(ModoAPITestCase): self.assertEqual( errors["default_mailbox_quota"], ["Must be a positive integer"] ) - self.assertEqual(errors["dkim_keys_storage_dir"], ["Directory not found."]) - - # Try to set dkim storage dir to a non-writable directory. - data = {"dkim_keys_storage_dir": "/root"} - url = reverse("v2:parameter-detail", args=["admin"]) - resp = self.client.put(url, data, format="json") - self.assertEqual(resp.status_code, 400) - errors = resp.json() - self.assertEqual(errors["dkim_keys_storage_dir"], ["Directory non-writable"]) data = { "enable_mx_checks": True, From 5cb7a94ee1d999dc427fb8af877f086b27e5face Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Wed, 17 Jul 2024 11:42:22 +0200 Subject: [PATCH 3/4] Removed useless constant --- modoboa/admin/constants.py | 1 - 1 file changed, 1 deletion(-) diff --git a/modoboa/admin/constants.py b/modoboa/admin/constants.py index 20f3fd79c..39a95023a 100644 --- a/modoboa/admin/constants.py +++ b/modoboa/admin/constants.py @@ -29,7 +29,6 @@ DKIM_KEY_LENGTHS = [ ] DKIM_WRITE_ERROR = "DKIM path error" -DKIM_DIR_NOT_FOUND = "DKIM directory not found" DKIM_ERROR = "general DKIM generation error" ALARM_OPENED = 1 From c86a917417436421aabdf0e8ba623623b9f51d70 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Wed, 17 Jul 2024 11:50:56 +0200 Subject: [PATCH 4/4] Removed old ref to DKIM_DIR_NOT_FOUND, --- README.rst | 6 +++--- .../management/commands/subcommands/_manage_dkim_keys.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index e4f717745..9555fbe14 100644 --- a/README.rst +++ b/README.rst @@ -32,9 +32,9 @@ Main features * `Webmail `_ * `Calendar `_ * `Address book `_ -* `Per-user Sieve filters `_ -* `Autoreply messages for Postfix `_ -* `Graphical statistics about email traffic `_ +* Per-user Sieve filters +* Autoreply messages for Postfix +* Graphical statistics about email traffic ************ Installation diff --git a/modoboa/admin/management/commands/subcommands/_manage_dkim_keys.py b/modoboa/admin/management/commands/subcommands/_manage_dkim_keys.py index e2148d6df..9cac1a2b8 100644 --- a/modoboa/admin/management/commands/subcommands/_manage_dkim_keys.py +++ b/modoboa/admin/management/commands/subcommands/_manage_dkim_keys.py @@ -10,7 +10,7 @@ from modoboa.lib import sysutils from modoboa.parameters import tools as param_tools from .... import models -from ....constants import DKIM_WRITE_ERROR, ALARM_OPENED, DKIM_DIR_NOT_FOUND, DKIM_ERROR +from ....constants import DKIM_WRITE_ERROR, ALARM_OPENED, DKIM_ERROR class ManageDKIMKeys(BaseCommand):