From 302a4a2f4dfddffaa0b3dd8d31813fff4e28d044 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Thu, 11 Feb 2021 20:49:23 +0100 Subject: [PATCH] Better error handling for doveadm call. --- modoboa/core/password_hashers/__init__.py | 8 ++++++-- modoboa/core/tests/test_authentication.py | 11 +++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/modoboa/core/password_hashers/__init__.py b/modoboa/core/password_hashers/__init__.py index 0a0b3f778..b94bccae7 100644 --- a/modoboa/core/password_hashers/__init__.py +++ b/modoboa/core/password_hashers/__init__.py @@ -42,12 +42,16 @@ def get_dovecot_schemes(): :return: A list of supported '{SCHEME}' """ schemes = getattr(settings, "DOVECOT_SUPPORTED_SCHEMES", None) + default_schemes = "MD5-CRYPT PLAIN" if not schemes: try: - _, schemes = doveadm_cmd("pw -l") + retcode, schemes = doveadm_cmd("pw -l") except OSError: - schemes = "MD5-CRYPT PLAIN" + schemes = default_schemes + else: + if retcode: + schemes = default_schemes return ["{{{}}}".format(smart_text(scheme)) for scheme in schemes.split()] diff --git a/modoboa/core/tests/test_authentication.py b/modoboa/core/tests/test_authentication.py index f7aa8b3ab..6467cf7ac 100644 --- a/modoboa/core/tests/test_authentication.py +++ b/modoboa/core/tests/test_authentication.py @@ -171,9 +171,16 @@ class AuthenticationTestCase(ModoTestCase): self.assertEqual(parameters.memory_cost, 1000) self.assertEqual(parameters.parallelism, 2) - @override_settings(DOVEADM_LOOKUP_PATH=[DOVEADM_TEST_PATH]) - def test_dovecot_supported_schemes(self): + @mock.patch("modoboa.lib.sysutils.exec_cmd") + def test_dovecot_supported_schemes(self, exec_cmd_mock): """Validate Dovecot supported schemes with fake command output.""" + exec_cmd_mock.return_value = ( + 0, + "MD5 MD5-CRYPT SHA SHA1 SHA256 SHA512 SMD5 SSHA SSHA256 SSHA512 " + "PLAIN CLEAR CLEARTEXT PLAIN-TRUNC CRAM-MD5 SCRAM-SHA-1 HMAC-MD5 " + "DIGEST-MD5 PLAIN-MD4 PLAIN-MD5 LDAP-MD5 LANMAN NTLM OTP SKEY RPA " + "PBKDF2 CRYPT SHA256-CRYPT SHA512-CRYPT" + ) supported_schemes = get_dovecot_schemes() self.assertEqual(supported_schemes, ["{MD5}",