-
Notifications
You must be signed in to change notification settings - Fork 177
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
Add base class implementation for local users' passwords reset #465
Open
azmy98
wants to merge
1
commit into
sonic-net:master
Choose a base branch
from
azmy98:dev-reset-local-users-password
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+122
−0
Open
Changes from all commits
Commits
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 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,59 @@ | ||
''' | ||
local_users_passwords_reset_base.py | ||
|
||
Abstract base class for implementing platform-specific | ||
local users' passwords reset base functionality for SONiC | ||
''' | ||
try: | ||
import json | ||
import subprocess | ||
|
||
from sonic_py_common.logger import Logger | ||
except ImportError as e: | ||
raise ImportError (str(e) + "- required module not found") | ||
|
||
# Global logger class instance | ||
logger = Logger() | ||
|
||
|
||
DEFAULT_USERS_FILEPATH = '/etc/sonic/default_users.json' | ||
|
||
|
||
class LocalUsersConfigurationResetBase(object): | ||
""" | ||
Abstract base class for resetting local users' passwords on the switch | ||
""" | ||
def should_trigger(self): | ||
''' | ||
define the condition to trigger | ||
''' | ||
# the condition to trigger start() method, the default implementation will be by checking if a long reboot press was detected. | ||
raise NotImplementedError | ||
|
||
@staticmethod | ||
def reset_password(user, hashed_password, expire=False): | ||
''' | ||
This method is used to reset the user's password and expire it (optional) using Linux shell commands. | ||
''' | ||
# Use 'chpasswd' shell command to change password | ||
subprocess.call([f"echo '{user}:{hashed_password}' | sudo chpasswd -e"], shell=True) | ||
if expire: | ||
# Use 'passwd' shell command to expire password | ||
subprocess.call(['sudo', 'passwd', '-e', f'{user}']) | ||
|
||
def start(self): | ||
''' | ||
The functionality defined is to restore original password and expire it for default local users. | ||
It is done by reading default users file and resetting passwords using Linux shell commands. | ||
''' | ||
default_users = {} | ||
|
||
# Fetch local users information from default_users | ||
with open(DEFAULT_USERS_FILEPATH) as f: | ||
default_users = json.load(f) | ||
|
||
logger.log_info('Restoring default users\' passwords and expiring them') | ||
for user in default_users.keys(): | ||
hashed_password = default_users.get(user, {}).get('password') | ||
if hashed_password: | ||
self.reset_password(user, hashed_password, expire=True) |
This file contains 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,63 @@ | ||
''' | ||
Test LocalUsersConfigurationResetBase module | ||
''' | ||
import subprocess | ||
import pytest | ||
from mock import patch, mock_open | ||
from sonic_platform_base.local_users_passwords_reset_base import LocalUsersConfigurationResetBase | ||
|
||
|
||
DEFAULT_USERS_JSON_EXAMPLE_OUTPUT = ''' | ||
{ | ||
"admin": { | ||
"expire": "false", | ||
"password": "HASHED_PASSWORD_123" | ||
} | ||
} | ||
''' | ||
|
||
|
||
class TestLocalUsersConfigurationResetBase: | ||
''' | ||
Collection of LocalUsersConfigurationResetBase test methods | ||
''' | ||
@staticmethod | ||
def test_local_users_passwords_reset_base(): | ||
''' | ||
Verify unimplemented methods | ||
''' | ||
base = LocalUsersConfigurationResetBase() | ||
not_implemented_methods = [ | ||
(base.should_trigger,)] | ||
|
||
for method in not_implemented_methods: | ||
expected_exception = False | ||
try: | ||
func = method[0] | ||
args = method[1:] | ||
func(*args) | ||
except Exception as exc: | ||
expected_exception = isinstance(exc, NotImplementedError) | ||
assert expected_exception | ||
|
||
@patch('subprocess.call') | ||
def test_reset_passwords_method(self, mock_subproc_call): | ||
''' | ||
Test the reset passwords static method | ||
''' | ||
LocalUsersConfigurationResetBase.reset_password( | ||
user='admin', | ||
hashed_password='HASHED_PASSWORD_123', | ||
expire=True) | ||
mock_subproc_call.assert_any_call(["echo 'admin:HASHED_PASSWORD_123' | sudo chpasswd -e"], shell=True) | ||
mock_subproc_call.assert_any_call(['sudo', 'passwd', '-e', 'admin']) | ||
|
||
@patch('subprocess.call') | ||
@patch("builtins.open", new_callable=mock_open, read_data=DEFAULT_USERS_JSON_EXAMPLE_OUTPUT) | ||
def test_basic_flow_resetting_users_triggered(self, mock_open, mock_subproc_call): | ||
''' | ||
Test the basic flow of resetting local users when long button press is detected | ||
''' | ||
LocalUsersConfigurationResetBase().start() | ||
mock_subproc_call.assert_any_call(["echo 'admin:HASHED_PASSWORD_123' | sudo chpasswd -e"], shell=True) | ||
mock_subproc_call.assert_any_call(['sudo', 'passwd', '-e', 'admin']) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not concat command line. It is vulnerable to command argument injection. For example, hashed_password may include blank char.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maipbui to help review this part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest not to use shell=True, instead use shell=False with an array of strings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It did not work, the quotations are not working correctly when I split it into list and the pipe does not work