-
Notifications
You must be signed in to change notification settings - Fork 906
Password hardening test fix #6453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ZhaohuiS
merged 3 commits into
sonic-net:master
from
davidpil2002:password-hardening-test-fix
Oct 16, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import logging | ||
| import os | ||
| import difflib | ||
| from tests.common.helpers.assertions import pytest_assert | ||
|
|
||
| CURR_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
| # Sample/Expected files | ||
| PAM_PASSWORD_CONF_DEFAULT_EXPECTED = CURR_DIR + '/sample/passw_hardening_default/common-password' | ||
| PAM_PASSWORD_CONF_EXPECTED = CURR_DIR + '/sample/passw_hardening_enable/common-password' | ||
| PAM_PASSWORD_CONF_HISTORY_ONLY_EXPECTED = CURR_DIR + '/sample/passw_hardening_history/common-password' | ||
| PAM_PASSWORD_CONF_REJECT_USER_PASSW_MATCH_EXPECTED = CURR_DIR + '/sample/passw_hardening_reject_user_passw_match/common-password' | ||
| PAM_PASSWORD_CONF_DIGITS_ONLY_EXPECTED = CURR_DIR + '/sample/passw_hardening_digits/common-password' | ||
| PAM_PASSWORD_CONF_LOWER_LETTER_ONLY_EXPECTED = CURR_DIR + '/sample/passw_hardening_lower_letter/common-password' | ||
| PAM_PASSWORD_CONF_UPPER_LETTER_ONLY_EXPECTED = CURR_DIR + '/sample/passw_hardening_upper_letter/common-password' | ||
| PAM_PASSWORD_CONF_SPECIAL_LETTER_ONLY_EXPECTED = CURR_DIR + '/sample/passw_hardening_special_letter/common-password' | ||
| PAM_PASSWORD_CONF_LEN_MIN_ONLY_EXPECTED = CURR_DIR + '/sample/passw_hardening_min_len/common-password' | ||
| PAM_PASSWORD_CONF_OUTPUT = CURR_DIR + '/output/login.def' | ||
| PAM_PASSWORD_CONF = "/etc/pam.d/common-password" | ||
|
|
||
| # users | ||
| USERNAME_STRONG = 'user_strong_test' | ||
| USERNAME_SIMPLE_0 = 'user_simple_0_test' | ||
| USERNAME_SIMPLE_1 = 'user_simple_1_test' | ||
| USERNAME_ONE_POLICY = 'user_one_policy_test' | ||
| USERNAME_AGE = 'user_test' | ||
| USERNAME_HISTORY = 'user_history_test' | ||
| USERNAME_LEN_MIN = 'user_test' | ||
|
|
||
|
|
||
| class PasswHardening: | ||
| def __init__(self, state='disabled', expiration='100', expiration_warning='15', history='12', | ||
| len_min='8', reject_user_passw_match='true', lower_class='true', | ||
| upper_class='true', digit_class='true', special_class='true'): | ||
| self.policies = { | ||
| "state": state, | ||
| "expiration": expiration, | ||
| "expiration-warning": expiration_warning, | ||
| "history-cnt": history, | ||
| "len-min": len_min, | ||
| "reject-user-passw-match": reject_user_passw_match, | ||
| "lower-class": lower_class, | ||
| "upper-class": upper_class, | ||
| "digits-class": digit_class, | ||
| "special-class": special_class | ||
| } | ||
|
|
||
|
|
||
| def config_user(duthost, username, mode='add'): | ||
| """ Function add or rm users using useradd/userdel tool. """ | ||
|
|
||
| username = username.strip() | ||
| command = "user{} {}".format(mode, username) | ||
| user_cmd = duthost.shell(command, module_ignore_errors=True) | ||
| return user_cmd | ||
|
|
||
|
|
||
| def configure_passw_policies(duthost, passw_hardening_ob): | ||
| for key, value in passw_hardening_ob.policies.items(): | ||
| logging.debug("configuration to be set: key={}, value={}".format(key, value)) | ||
| # cmd_config = 'sudo config passw-hardening policies ' + key + ' ' + value | ||
|
|
||
| cmd_config = 'sudo config passw-hardening policies {} {}'.format(key, value) | ||
|
|
||
| duthost.command(cmd_config) | ||
| return True | ||
|
|
||
|
|
||
| def compare_passw_policies_in_linux(duthost, pam_file_expected=PAM_PASSWORD_CONF_EXPECTED): | ||
| """Compare DUT common-password with the expected one.""" | ||
|
|
||
| command_password_stdout = '' | ||
| read_command_password = 'cat {}'.format(PAM_PASSWORD_CONF) | ||
|
|
||
| logging.debug('DUT command = {}'.format(read_command_password)) | ||
| read_command_password_cmd = duthost.command(read_command_password) | ||
| command_password_stdout = read_command_password_cmd["stdout_lines"] | ||
| command_password_stdout = [line.encode('utf-8') for line in command_password_stdout] | ||
|
|
||
| common_password_expected = [] | ||
| with open(pam_file_expected, 'r') as expected_common_password_file: | ||
| for line in expected_common_password_file: | ||
| line = line.strip() | ||
| line = line.strip('\n') | ||
| line = line.strip('\t') | ||
| common_password_expected.append(line) | ||
|
|
||
| common_password_diff = [li for li in difflib.ndiff(command_password_stdout, common_password_expected) if | ||
| li[0] != ' '] | ||
| pytest_assert(len(common_password_diff) == 0, common_password_diff) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
|
|
||
| def config_and_review_policies(duthost, passw_hardening_ob, pam_file_expected): | ||
| """ | ||
| 1. Config passw hardening policies | ||
| 2. Show passw hardening policies | ||
| 3. Compare passw hardening polices from show cli to the expected (configured) | ||
| 4. Verify polices in PAM files was set according the configured | ||
| """ | ||
| FIRST_LINE = 0 | ||
| configure_passw_policies(duthost, passw_hardening_ob) | ||
|
|
||
| curr_show_policies = duthost.show_and_parse('show passw-hardening policies')[FIRST_LINE] | ||
| exp_show_policies = dict((k.replace('-', ' '), v) for k, v in passw_hardening_ob.policies.items()) | ||
|
|
||
| # ~~ test passw policies in show CLI ~~ | ||
| cli_passw_policies_cmp = cmp(exp_show_policies, curr_show_policies) | ||
| pytest_assert(cli_passw_policies_cmp == 0, "Fail: exp_show_policies='{}',not equal to curr_show_policies='{}'" | ||
| .format(exp_show_policies, curr_show_policies)) | ||
|
|
||
| # ~~ test passw policies in PAM files ~~ | ||
| compare_passw_policies_in_linux(duthost, pam_file_expected) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.