forked from p15670423/monkey
Merge pull request #1845 from guardicore/1650-timeouts-for-creating-new-users
Add timeout to new user creation
This commit is contained in:
commit
4bb0e506c9
|
@ -3,6 +3,7 @@ import logging
|
|||
import shlex
|
||||
import subprocess
|
||||
|
||||
from common.common_consts.timeouts import SHORT_REQUEST_TIMEOUT
|
||||
from infection_monkey.utils.auto_new_user import AutoNewUser
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
@ -43,7 +44,12 @@ class AutoNewLinuxUser(AutoNewUser):
|
|||
logger.debug(
|
||||
"Trying to add {} with commands {}".format(self.username, str(commands_to_add_user))
|
||||
)
|
||||
_ = subprocess.check_output(commands_to_add_user, stderr=subprocess.STDOUT)
|
||||
try:
|
||||
_ = subprocess.check_output(
|
||||
commands_to_add_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT
|
||||
)
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as err:
|
||||
logger.error(f"An exception occurred when creating a new linux user: {str(err)}")
|
||||
|
||||
def __enter__(self):
|
||||
return self # No initialization/logging on needed in Linux
|
||||
|
@ -52,7 +58,12 @@ class AutoNewLinuxUser(AutoNewUser):
|
|||
command_as_new_user = shlex.split(
|
||||
"sudo -u {username} {command}".format(username=self.username, command=command)
|
||||
)
|
||||
return subprocess.call(command_as_new_user)
|
||||
try:
|
||||
return subprocess.call(command_as_new_user, timeout=SHORT_REQUEST_TIMEOUT)
|
||||
except subprocess.TimeoutExpired as err:
|
||||
logger.error(
|
||||
f"An exception occurred when running a command as a new linux user: {str(err)}"
|
||||
)
|
||||
|
||||
def __exit__(self, _exc_type, value, traceback):
|
||||
# delete the user.
|
||||
|
@ -62,4 +73,9 @@ class AutoNewLinuxUser(AutoNewUser):
|
|||
self.username, str(commands_to_delete_user)
|
||||
)
|
||||
)
|
||||
_ = subprocess.check_output(commands_to_delete_user, stderr=subprocess.STDOUT)
|
||||
try:
|
||||
_ = subprocess.check_output(
|
||||
commands_to_delete_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT
|
||||
)
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as err:
|
||||
logger.error(f"An exception occurred when deleting the new linux user: {str(err)}")
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import logging
|
||||
import subprocess
|
||||
|
||||
from common.common_consts.timeouts import SHORT_REQUEST_TIMEOUT
|
||||
from infection_monkey.utils.auto_new_user import AutoNewUser
|
||||
from infection_monkey.utils.environment import is_windows_os
|
||||
from infection_monkey.utils.new_user_error import NewUserError
|
||||
|
@ -49,7 +50,12 @@ class AutoNewWindowsUser(AutoNewUser):
|
|||
|
||||
windows_cmds = get_windows_commands_to_add_user(self.username, self.password, True)
|
||||
logger.debug("Trying to add {} with commands {}".format(self.username, str(windows_cmds)))
|
||||
_ = subprocess.check_output(windows_cmds, stderr=subprocess.STDOUT)
|
||||
try:
|
||||
_ = subprocess.check_output(
|
||||
windows_cmds, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT
|
||||
)
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as err:
|
||||
logger.error(f"An exception occurred when creating a new windows user: {str(err)}")
|
||||
|
||||
def __enter__(self):
|
||||
try:
|
||||
|
@ -124,7 +130,9 @@ class AutoNewWindowsUser(AutoNewUser):
|
|||
self.username, str(commands_to_deactivate_user)
|
||||
)
|
||||
)
|
||||
_ = subprocess.check_output(commands_to_deactivate_user, stderr=subprocess.STDOUT)
|
||||
_ = subprocess.check_output(
|
||||
commands_to_deactivate_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT
|
||||
)
|
||||
except Exception as err:
|
||||
raise NewUserError("Can't deactivate user {}. Info: {}".format(self.username, err))
|
||||
|
||||
|
@ -136,6 +144,8 @@ class AutoNewWindowsUser(AutoNewUser):
|
|||
self.username, str(commands_to_delete_user)
|
||||
)
|
||||
)
|
||||
_ = subprocess.check_output(commands_to_delete_user, stderr=subprocess.STDOUT)
|
||||
_ = subprocess.check_output(
|
||||
commands_to_delete_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT
|
||||
)
|
||||
except Exception as err:
|
||||
raise NewUserError("Can't delete user {}. Info: {}".format(self.username, err))
|
||||
|
|
|
@ -9,7 +9,7 @@ TEST_USER = "test_user"
|
|||
|
||||
@pytest.fixture
|
||||
def subprocess_check_output_spy(monkeypatch):
|
||||
def mock_check_output(command, stderr):
|
||||
def mock_check_output(command, stderr, timeout):
|
||||
mock_check_output.command = command
|
||||
|
||||
mock_check_output.command = ""
|
||||
|
|
|
@ -10,7 +10,7 @@ TEST_USER = "test_user"
|
|||
|
||||
@pytest.fixture
|
||||
def subprocess_check_output_spy(monkeypatch):
|
||||
def mock_check_output(command, stderr):
|
||||
def mock_check_output(command, stderr, timeout):
|
||||
mock_check_output.command = command
|
||||
|
||||
mock_check_output.command = ""
|
||||
|
|
Loading…
Reference in New Issue