From caa62c627206fe068c119812cf0e9068acf26609 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 7 Oct 2021 15:24:23 -0400 Subject: [PATCH] Island: Remove _credentials_required property from Environment Since #1418, credentials are always required, rendering the _credentials_required property of the Environment class obsolete. --- monkey/common/utils/exceptions.py | 4 --- .../monkey_island/cc/environment/__init__.py | 23 ++++---------- monkey/monkey_island/cc/environment/aws.py | 2 -- .../monkey_island/cc/environment/password.py | 2 -- .../cc/environment/test_environment.py | 30 +------------------ 5 files changed, 7 insertions(+), 54 deletions(-) diff --git a/monkey/common/utils/exceptions.py b/monkey/common/utils/exceptions.py index df40f3007..9b41edc68 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 281b08a3a..3e70602c7 100644 --- a/monkey/monkey_island/cc/environment/__init__.py +++ b/monkey/monkey_island/cc/environment/__init__.py @@ -4,7 +4,6 @@ from datetime import timedelta from common.utils.exceptions import ( AlreadyRegisteredError, - CredentialsNotRequiredError, InvalidRegistrationCredentialsError, ) from monkey_island.cc.environment.environment_config import EnvironmentConfig @@ -24,11 +23,6 @@ class Environment(object, metaclass=ABCMeta): 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 @@ class Environment(object, metaclass=ABCMeta): 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 @@ class Environment(object, metaclass=ABCMeta): 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 d050ce605..7254b29f2 100644 --- a/monkey/monkey_island/cc/environment/aws.py +++ b/monkey/monkey_island/cc/environment/aws.py @@ -3,8 +3,6 @@ from monkey_island.cc.environment import Environment 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 2c9a7bd6e..68b7d2e16 100644 --- a/monkey/monkey_island/cc/environment/password.py +++ b/monkey/monkey_island/cc/environment/password.py @@ -2,8 +2,6 @@ from monkey_island.cc.environment import Environment 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 10adea8b7..746954880 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 @@ from unittest.mock import MagicMock, patch 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 @@ class StubEnvironmentConfig(EnvironmentConfig): 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 @@ class TestEnvironment(TestCase): 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 @@ class TestEnvironment(TestCase): 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())