Skip to content

Commit

Permalink
Island: Remove _credentials_required property from Environment
Browse files Browse the repository at this point in the history
Since #1418, credentials are always required, rendering the
_credentials_required property of the Environment class obsolete.
  • Loading branch information
mssalvatore committed Oct 28, 2021
1 parent 6736699 commit caa62c6
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 54 deletions.
4 changes: 0 additions & 4 deletions monkey/common/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """

Expand Down
23 changes: 6 additions & 17 deletions monkey/monkey_island/cc/environment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from common.utils.exceptions import (
AlreadyRegisteredError,
CredentialsNotRequiredError,
InvalidRegistrationCredentialsError,
)
from monkey_island.cc.environment.environment_config import EnvironmentConfig
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions monkey/monkey_island/cc/environment/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions monkey/monkey_island/cc/environment/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@


class PasswordEnvironment(Environment):
_credentials_required = True

def get_auth_users(self):
if self._is_registered():
return [self._config.get_user()]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 []

Expand All @@ -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"]

Expand All @@ -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())

Expand Down

0 comments on commit caa62c6

Please sign in to comment.