From eef77c91829ff8e0a8d74cbb5975b71b8df47f33 Mon Sep 17 00:00:00 2001 From: houk-ms Date: Tue, 25 Aug 2020 14:20:33 +0800 Subject: [PATCH 1/5] improve vm password validation message --- src/azure-cli/azure/cli/command_modules/vm/_validators.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/_validators.py b/src/azure-cli/azure/cli/command_modules/vm/_validators.py index 24a278fb557..e222b9e2576 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_validators.py @@ -1034,9 +1034,11 @@ def _validate_admin_password(password, os_type): is_linux = (os_type.lower() == 'linux') max_length = 72 if is_linux else 123 min_length = 12 + error_msg = ("Rule 1: The password length must be betwween {} and {}\n" + "Rule 2: Password must have the 3 of the following: 1 lower case character, " + "1 upper case character, 1 number and 1 special character").format(min_length, max_length) if len(password) not in range(min_length, max_length + 1): - raise CLIError('The password length must be between {} and {}'.format(min_length, - max_length)) + raise CLIError("Your password is invalid for it violates Rule 1\n{}".format(error_msg)) contains_lower = re.findall('[a-z]+', password) contains_upper = re.findall('[A-Z]+', password) contains_digit = re.findall('[0-9]+', password) @@ -1045,7 +1047,7 @@ def _validate_admin_password(password, os_type): contains_digit, contains_special_char] if x]) # pylint: disable=line-too-long if count < 3: - raise CLIError('Password must have the 3 of the following: 1 lower case character, 1 upper case character, 1 number and 1 special character') + raise CLIError("Your password is invalid for it violates Rule 2\n{}".format(error_msg)) def validate_ssh_key(namespace): From 4da2bcb1225ac06c0285c0fb910f280e83f27aaa Mon Sep 17 00:00:00 2001 From: houk-ms Date: Tue, 25 Aug 2020 15:36:34 +0800 Subject: [PATCH 2/5] fix typo --- src/azure-cli/azure/cli/command_modules/vm/_validators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/_validators.py b/src/azure-cli/azure/cli/command_modules/vm/_validators.py index e222b9e2576..51d4c224aaa 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_validators.py @@ -1034,7 +1034,7 @@ def _validate_admin_password(password, os_type): is_linux = (os_type.lower() == 'linux') max_length = 72 if is_linux else 123 min_length = 12 - error_msg = ("Rule 1: The password length must be betwween {} and {}\n" + error_msg = ("Rule 1: The password length must be between {} and {}\n" "Rule 2: Password must have the 3 of the following: 1 lower case character, " "1 upper case character, 1 number and 1 special character").format(min_length, max_length) if len(password) not in range(min_length, max_length + 1): From b7816b8727970ddf59c5c207a6a922f77f95f3c1 Mon Sep 17 00:00:00 2001 From: houk-ms Date: Tue, 8 Sep 2020 10:04:08 +0800 Subject: [PATCH 3/5] adjust error messages --- .../azure/cli/command_modules/vm/_validators.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/_validators.py b/src/azure-cli/azure/cli/command_modules/vm/_validators.py index 51d4c224aaa..168e857c0fd 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_validators.py @@ -1034,20 +1034,23 @@ def _validate_admin_password(password, os_type): is_linux = (os_type.lower() == 'linux') max_length = 72 if is_linux else 123 min_length = 12 - error_msg = ("Rule 1: The password length must be between {} and {}\n" - "Rule 2: Password must have the 3 of the following: 1 lower case character, " - "1 upper case character, 1 number and 1 special character").format(min_length, max_length) - if len(password) not in range(min_length, max_length + 1): - raise CLIError("Your password is invalid for it violates Rule 1\n{}".format(error_msg)) + contains_lower = re.findall('[a-z]+', password) contains_upper = re.findall('[A-Z]+', password) contains_digit = re.findall('[0-9]+', password) contains_special_char = re.findall(r'[ `~!@#$%^&*()=+_\[\]{}\|;:.\/\'\",<>?]+', password) count = len([x for x in [contains_lower, contains_upper, contains_digit, contains_special_char] if x]) + # pylint: disable=line-too-long + length_error = "The password length must be between {} and {}.".format(min_length, max_length) + complexity_error = "Password must have the 3 of the following: 1 lower case character, 1 upper case character, 1 number and 1 special character." + if len(password) not in range(min_length, max_length + 1) and count < 3: + raise CLIError("{} {}".format(length_error, complexity_error)) + if len(password) not in range(min_length, max_length + 1): + raise CLIError(length_error) if count < 3: - raise CLIError("Your password is invalid for it violates Rule 2\n{}".format(error_msg)) + raise CLIError(complexity_error) def validate_ssh_key(namespace): From 31b884749c50e48166195744476ecf41247d4368 Mon Sep 17 00:00:00 2001 From: houk-ms Date: Tue, 8 Sep 2020 12:37:46 +0800 Subject: [PATCH 4/5] adjsut error message --- .../azure/cli/command_modules/vm/_validators.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/_validators.py b/src/azure-cli/azure/cli/command_modules/vm/_validators.py index 168e857c0fd..7326451d7f7 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_validators.py @@ -1043,14 +1043,10 @@ def _validate_admin_password(password, os_type): contains_digit, contains_special_char] if x]) # pylint: disable=line-too-long - length_error = "The password length must be between {} and {}.".format(min_length, max_length) - complexity_error = "Password must have the 3 of the following: 1 lower case character, 1 upper case character, 1 number and 1 special character." - if len(password) not in range(min_length, max_length + 1) and count < 3: - raise CLIError("{} {}".format(length_error, complexity_error)) - if len(password) not in range(min_length, max_length + 1): - raise CLIError(length_error) - if count < 3: - raise CLIError(complexity_error) + error_msg = ("The password length must be between {} and {}. And the password must have the 3 of the following: " + "1 lower case character, 1 upper case character, 1 number and 1 special character.").format(min_length, max_length) + if len(password) not in range(min_length, max_length + 1) or count < 3: + raise CLIError(error_msg) def validate_ssh_key(namespace): From 3d108cba09eaf3f68fda5ad9e4ff7774d0b26cee Mon Sep 17 00:00:00 2001 From: houk-ms Date: Tue, 8 Sep 2020 12:46:43 +0800 Subject: [PATCH 5/5] fix testing issues --- src/azure-cli/azure/cli/command_modules/vm/_validators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/azure-cli/azure/cli/command_modules/vm/_validators.py b/src/azure-cli/azure/cli/command_modules/vm/_validators.py index 7326451d7f7..f4b57fd7f2d 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_validators.py @@ -1043,7 +1043,7 @@ def _validate_admin_password(password, os_type): contains_digit, contains_special_char] if x]) # pylint: disable=line-too-long - error_msg = ("The password length must be between {} and {}. And the password must have the 3 of the following: " + error_msg = ("The password length must be between {} and {}. Password must have the 3 of the following: " "1 lower case character, 1 upper case character, 1 number and 1 special character.").format(min_length, max_length) if len(password) not in range(min_length, max_length + 1) or count < 3: raise CLIError(error_msg)