From 7a059d2ea0310e960cacf19094f6758defa86596 Mon Sep 17 00:00:00 2001 From: Adrien Ferrand Date: Wed, 11 Nov 2020 10:42:11 +0100 Subject: [PATCH] Answer to the review --- certbot/certbot/_internal/main.py | 16 ++++++---------- certbot/tests/main_test.py | 10 +++++++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/certbot/certbot/_internal/main.py b/certbot/certbot/_internal/main.py index a7d8d850d..8d62210e0 100644 --- a/certbot/certbot/_internal/main.py +++ b/certbot/certbot/_internal/main.py @@ -139,21 +139,17 @@ def _handle_unexpected_key_type_migration(config, cert): :param config: Current configuration provided by the client :param cert: Matching certificate that could be renewed """ - if (config.verb in ["certonly", "run"] - and (not cli.set_by_cli("key_type") or not cli.set_by_cli("certname"))): + if not cli.set_by_cli("key_type") or not cli.set_by_cli("certname"): new_key_type = config.key_type.upper() cur_key_type = cert.private_key_type.upper() if new_key_type != cur_key_type: - logger.error("A certificate already exists for that name, the list of provided " - "domains or a subset of this list. This certificate uses a key of %s " - "type, and you have not provided the --key-type flag. By default in this " - "case Certbot would generate a new certificate with a key of %s type. " - "Please confirm that you really want to change the type of the key by " - "setting both --cert-name and --key-type CLI flags.", - cur_key_type, new_key_type) - raise errors.Error("Command canceled.") + msg = ('Are you trying to change the key type of the certificate named {0} ' + 'from {1} to {2}? Please provide both --cert-name and --key-type on ' + 'the command line confirm the change you are trying to make.') + msg = msg.format(cert.lineagename, cur_key_type, new_key_type) + raise errors.Error(msg) def _handle_subset_cert_request(config, # type: configuration.NamespaceConfig diff --git a/certbot/tests/main_test.py b/certbot/tests/main_test.py index e0ce6f1c6..26154ae16 100644 --- a/certbot/tests/main_test.py +++ b/certbot/tests/main_test.py @@ -73,7 +73,6 @@ class TestHandleCerts(unittest.TestCase): @mock.patch("certbot._internal.main.cli.set_by_cli") def test_handle_unexpected_key_type_migration(self, mock_set): config = mock.Mock() - config.verb = "certonly" config.key_type = "rsa" cert = mock.Mock() cert.private_key_type = "ecdsa" @@ -84,12 +83,17 @@ class TestHandleCerts(unittest.TestCase): mock_set.return_value = False with self.assertRaises(errors.Error) as raised: main._handle_unexpected_key_type_migration(config, cert) - self.assertTrue("Command canceled." in str(raised.exception)) + self.assertTrue("Please provide both --cert-name and --key-type" in str(raised.exception)) + + mock_set.side_effect = lambda var: var != "certname" + with self.assertRaises(errors.Error) as raised: + main._handle_unexpected_key_type_migration(config, cert) + self.assertTrue("Please provide both --cert-name and --key-type" in str(raised.exception)) mock_set.side_effect = lambda var: var != "key_type" with self.assertRaises(errors.Error) as raised: main._handle_unexpected_key_type_migration(config, cert) - self.assertTrue("Command canceled." in str(raised.exception)) + self.assertTrue("Please provide both --cert-name and --key-type" in str(raised.exception)) class RunTest(test_util.ConfigTestCase):