Answer to the review

This commit is contained in:
Adrien Ferrand 2020-11-11 10:42:11 +01:00
parent dbebbfacb0
commit 7a059d2ea0
2 changed files with 13 additions and 13 deletions

View file

@ -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

View file

@ -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):