forked from p15670423/monkey
Island: Remove _credentials_required property from Environment
Since #1418, credentials are always required, rendering the _credentials_required property of the Environment class obsolete.
This commit is contained in:
parent
6736699cf4
commit
caa62c6272
|
@ -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 """
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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()]
|
||||
|
|
|
@ -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())
|
||||
|
||||
|
|
Loading…
Reference in New Issue