diff --git a/letsencrypt/client/account.py b/letsencrypt/client/account.py index 6b1e99fc3..ef8831fa5 100644 --- a/letsencrypt/client/account.py +++ b/letsencrypt/client/account.py @@ -35,7 +35,7 @@ class Account(object): # Just make sure we don't get pwned # Make sure that it also doesn't start with a period or have two consecutive # periods <- this needs to be done in addition to the regex - EMAIL_REGEX = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+$" + EMAIL_REGEX = re.compile("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+$") def __init__(self, config, key, email=None, phone=None, regr=None): le_util.make_or_verify_dir( @@ -192,13 +192,14 @@ class Account(object): "Enter email address (optional, press Enter to skip)") if code == display_util.OK: - if email == "" or cls.safe_email(email): - email = email if email != "" else None + if not email or cls.safe_email(email): + email = email if email else None le_util.make_or_verify_dir( config.account_keys_dir, 0o700, os.geteuid()) key = crypto_util.init_save_key( - config.rsa_key_size, config.account_keys_dir, email) + config.rsa_key_size, config.account_keys_dir, + cls._get_config_filename(email)) return cls(config, key, email) else: return None @@ -206,7 +207,8 @@ class Account(object): @classmethod def safe_email(cls, email): """Scrub email address before using it.""" - if re.match(cls.EMAIL_REGEX, email): - return bool(not email.startswith(".") and ".." not in email) - logging.warn("Invalid email address.") - return False + if cls.EMAIL_REGEX.match(email): + return not email.startswith(".") and ".." not in email + else: + logging.warn("Invalid email address.") + return False diff --git a/letsencrypt/client/tests/account_test.py b/letsencrypt/client/tests/account_test.py index 0cb3346c8..269328f27 100644 --- a/letsencrypt/client/tests/account_test.py +++ b/letsencrypt/client/tests/account_test.py @@ -64,14 +64,26 @@ class AccountTest(unittest.TestCase): zope.component.provideUtility(displayer) mock_util().input.return_value = (display_util.OK, self.email) - mock_key.return_value = self.key - acc = account.Account.from_prompts(self.config) + acc = account.Account.from_prompts(self.config) self.assertEqual(acc.email, self.email) self.assertEqual(acc.key, self.key) self.assertEqual(acc.config, self.config) + @mock.patch("letsencrypt.client.account.zope.component.getUtility") + @mock.patch("letsencrypt.client.account.crypto_util.init_save_key") + def test_prompts_empty_email(self, mock_key, mock_util): + displayer = display_util.FileDisplay(sys.stdout) + zope.component.provideUtility(displayer) + + mock_util().input.return_value = (display_util.OK, "") + acc = account.Account.from_prompts(self.config) + self.assertTrue(acc.email is None) + # _get_config_filename | pylint: disable=protected-access + mock_key.assert_called_once_with( + mock.ANY, mock.ANY, acc._get_config_filename(None)) + @mock.patch("letsencrypt.client.account.zope.component.getUtility") def test_prompts_cancel(self, mock_util): # displayer = display_util.FileDisplay(sys.stdout)