diff --git a/monkey/common/utils/exceptions.py b/monkey/common/utils/exceptions.py index df40f30070d..9b41edc6866 100644 --- a/monkey/common/utils/exceptions.py +++ b/monkey/common/utils/exceptions.py @@ -14,10 +14,6 @@ class RegistrationNotNeededError(Exception): """ Raise to indicate the reason why registration is not required """ -class CredentialsNotRequiredError(RegistrationNotNeededError): - """ Raise to indicate the reason why registration is not required """ - - class AlreadyRegisteredError(RegistrationNotNeededError): """ Raise to indicate the reason why registration is not required """ diff --git a/monkey/monkey_island/cc/environment/__init__.py b/monkey/monkey_island/cc/environment/__init__.py index 281b08a3a3f..3e70602c7a4 100644 --- a/monkey/monkey_island/cc/environment/__init__.py +++ b/monkey/monkey_island/cc/environment/__init__.py @@ -4,7 +4,6 @@ from common.utils.exceptions import ( AlreadyRegisteredError, - CredentialsNotRequiredError, InvalidRegistrationCredentialsError, ) from monkey_island.cc.environment.environment_config import EnvironmentConfig @@ -24,11 +23,6 @@ def __init__(self, config: EnvironmentConfig): self._config = config self._testing = False # Assume env is not for unit testing. - @property - @abstractmethod - def _credentials_required(self) -> bool: - pass - @abstractmethod def get_auth_users(self): pass @@ -37,7 +31,7 @@ def needs_registration(self) -> bool: try: needs_registration = self._try_needs_registration() return needs_registration - except (CredentialsNotRequiredError, AlreadyRegisteredError) as e: + except (AlreadyRegisteredError) as e: logger.info(e) return False @@ -49,19 +43,14 @@ def try_add_user(self, credentials: UserCreds): logger.info(f"New user {credentials.username} registered!") def _try_needs_registration(self) -> bool: - if not self._credentials_required: - raise CredentialsNotRequiredError( - "Credentials are not required " "for current environment." + if self._is_registered(): + raise AlreadyRegisteredError( + "User has already been registered. " "Reset credentials or login." ) - else: - if self._is_registered(): - raise AlreadyRegisteredError( - "User has already been registered. " "Reset credentials or login." - ) - return True + return True def _is_registered(self) -> bool: - return self._credentials_required and self._is_credentials_set_up() + return self._is_credentials_set_up() def _is_credentials_set_up(self) -> bool: if self._config and self._config.user_creds: diff --git a/monkey/monkey_island/cc/environment/aws.py b/monkey/monkey_island/cc/environment/aws.py index d050ce605e0..7254b29f2b2 100644 --- a/monkey/monkey_island/cc/environment/aws.py +++ b/monkey/monkey_island/cc/environment/aws.py @@ -3,8 +3,6 @@ class AwsEnvironment(Environment): - _credentials_required = True - def __init__(self, config): super(AwsEnvironment, self).__init__(config) # Not suppressing error here on purpose. This is critical if we're on AWS env. diff --git a/monkey/monkey_island/cc/environment/password.py b/monkey/monkey_island/cc/environment/password.py index 2c9a7bd6ea5..68b7d2e1684 100644 --- a/monkey/monkey_island/cc/environment/password.py +++ b/monkey/monkey_island/cc/environment/password.py @@ -2,8 +2,6 @@ class PasswordEnvironment(Environment): - _credentials_required = True - def get_auth_users(self): if self._is_registered(): return [self._config.get_user()] diff --git a/monkey/tests/unit_tests/monkey_island/cc/environment/test_environment.py b/monkey/tests/unit_tests/monkey_island/cc/environment/test_environment.py index 10adea8b79b..746954880e5 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/environment/test_environment.py +++ b/monkey/tests/unit_tests/monkey_island/cc/environment/test_environment.py @@ -6,12 +6,7 @@ import pytest -from common.utils.exceptions import ( - AlreadyRegisteredError, - CredentialsNotRequiredError, - InvalidRegistrationCredentialsError, - RegistrationNotNeededError, -) +from common.utils.exceptions import AlreadyRegisteredError, InvalidRegistrationCredentialsError from monkey_island.cc.environment import Environment, EnvironmentConfig, UserCreds WITH_CREDENTIALS = None @@ -52,23 +47,11 @@ def __del__(self): class TestEnvironment(TestCase): - class EnvironmentCredentialsNotRequired(Environment): - def __init__(self): - config = StubEnvironmentConfig("test", "test", EMPTY_USER_CREDENTIALS) - super().__init__(config) - - _credentials_required = False - - def get_auth_users(self): - return [] - class EnvironmentCredentialsRequired(Environment): def __init__(self): config = StubEnvironmentConfig("test", "test", EMPTY_USER_CREDENTIALS) super().__init__(config) - _credentials_required = True - def get_auth_users(self): return [] @@ -77,8 +60,6 @@ def __init__(self): config = StubEnvironmentConfig("test", "test", UserCreds("test_user", "test_secret")) super().__init__(config) - _credentials_required = True - def get_auth_users(self): return [1, "Test_username", "Test_secret"] @@ -92,20 +73,11 @@ def test_try_add_user(self): with self.assertRaises(InvalidRegistrationCredentialsError): env.try_add_user(credentials) - env = TestEnvironment.EnvironmentCredentialsNotRequired() - credentials = FULL_USER_CREDENTIALS - with self.assertRaises(RegistrationNotNeededError): - env.try_add_user(credentials) - def test_try_needs_registration(self): env = TestEnvironment.EnvironmentAlreadyRegistered() with self.assertRaises(AlreadyRegisteredError): env._try_needs_registration() - env = TestEnvironment.EnvironmentCredentialsNotRequired() - with self.assertRaises(CredentialsNotRequiredError): - env._try_needs_registration() - env = TestEnvironment.EnvironmentCredentialsRequired() self.assertTrue(env._try_needs_registration())