From 252c1d940aaf99252b9cec307bcae41ad32bc050 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 7 Oct 2021 15:49:42 -0400 Subject: [PATCH] Island: Remove "auth user" --- monkey/monkey_island/cc/environment/__init__.py | 5 ++--- monkey/monkey_island/cc/environment/aws.py | 6 ------ .../cc/environment/environment_config.py | 5 ----- monkey/monkey_island/cc/environment/password.py | 8 +++----- monkey/monkey_island/cc/environment/user_creds.py | 5 ----- monkey/monkey_island/cc/resources/auth/auth.py | 4 ++-- monkey/monkey_island/cc/resources/auth/auth_user.py | 8 -------- .../cc/environment/test_environment.py | 13 +++---------- .../cc/environment/test_environment_config.py | 7 +++---- .../monkey_island/cc/environment/test_user_creds.py | 8 -------- 10 files changed, 13 insertions(+), 56 deletions(-) delete mode 100644 monkey/monkey_island/cc/resources/auth/auth_user.py diff --git a/monkey/monkey_island/cc/environment/__init__.py b/monkey/monkey_island/cc/environment/__init__.py index bb9f8e775..70fb775c5 100644 --- a/monkey/monkey_island/cc/environment/__init__.py +++ b/monkey/monkey_island/cc/environment/__init__.py @@ -23,9 +23,8 @@ class Environment(object, metaclass=ABCMeta): self._config = config self._testing = False # Assume env is not for unit testing. - @abstractmethod - def get_auth_users(self): - pass + def get_user(self): + return self._config.user_creds def needs_registration(self) -> bool: try: diff --git a/monkey/monkey_island/cc/environment/aws.py b/monkey/monkey_island/cc/environment/aws.py index 7254b29f2..c367d3a98 100644 --- a/monkey/monkey_island/cc/environment/aws.py +++ b/monkey/monkey_island/cc/environment/aws.py @@ -7,9 +7,3 @@ class AwsEnvironment(Environment): super(AwsEnvironment, self).__init__(config) # Not suppressing error here on purpose. This is critical if we're on AWS env. self.aws_info = AwsInstance() - - def get_auth_users(self): - if self._is_registered(): - return [self._config.get_user()] - else: - return [] diff --git a/monkey/monkey_island/cc/environment/environment_config.py b/monkey/monkey_island/cc/environment/environment_config.py index 7b3c26d4b..42623369e 100644 --- a/monkey/monkey_island/cc/environment/environment_config.py +++ b/monkey/monkey_island/cc/environment/environment_config.py @@ -5,7 +5,6 @@ import os from typing import Dict from monkey_island.cc.environment.user_creds import UserCreds -from monkey_island.cc.resources.auth.auth_user import User class EnvironmentConfig: @@ -58,10 +57,6 @@ class EnvironmentConfig: self.user_creds = credentials self.save_to_file() - def get_user(self) -> User: - auth_user = self.user_creds.to_auth_user() - return auth_user if auth_user else None - def _get_user_credentials_from_config(dict_data: Dict): username = dict_data.get("user", "") diff --git a/monkey/monkey_island/cc/environment/password.py b/monkey/monkey_island/cc/environment/password.py index 68b7d2e16..6dc9eea09 100644 --- a/monkey/monkey_island/cc/environment/password.py +++ b/monkey/monkey_island/cc/environment/password.py @@ -1,9 +1,7 @@ from monkey_island.cc.environment import Environment +# TODO: We can probably remove these Environment subclasses, but the +# AwsEnvironment class still does something unique in its constructor. class PasswordEnvironment(Environment): - def get_auth_users(self): - if self._is_registered(): - return [self._config.get_user()] - else: - return [] + pass diff --git a/monkey/monkey_island/cc/environment/user_creds.py b/monkey/monkey_island/cc/environment/user_creds.py index aba349f2d..a30edae5f 100644 --- a/monkey/monkey_island/cc/environment/user_creds.py +++ b/monkey/monkey_island/cc/environment/user_creds.py @@ -2,8 +2,6 @@ from __future__ import annotations from typing import Dict -from monkey_island.cc.resources.auth.auth_user import User - class UserCreds: def __init__(self, username, password_hash): @@ -20,6 +18,3 @@ class UserCreds: if self.password_hash: cred_dict.update({"password_hash": self.password_hash}) return cred_dict - - def to_auth_user(self) -> User: - return User(1, self.username, self.password_hash) diff --git a/monkey/monkey_island/cc/resources/auth/auth.py b/monkey/monkey_island/cc/resources/auth/auth.py index 3fab5ef66..453a45027 100644 --- a/monkey/monkey_island/cc/resources/auth/auth.py +++ b/monkey/monkey_island/cc/resources/auth/auth.py @@ -50,12 +50,12 @@ class Authenticate(flask_restful.Resource): def _credentials_match_registered_user(username: str, password: str) -> bool: - registered_user = env_singleton.env.get_auth_users() + registered_user = env_singleton.env.get_user() if not registered_user: return False - return (registered_user.username == username) and password_matches_hash(password, registered_user[0].secret) + return (registered_user.username == username) and password_matches_hash(password, registered_user.password_hash) def _create_access_token(username): diff --git a/monkey/monkey_island/cc/resources/auth/auth_user.py b/monkey/monkey_island/cc/resources/auth/auth_user.py deleted file mode 100644 index 547b6e5bc..000000000 --- a/monkey/monkey_island/cc/resources/auth/auth_user.py +++ /dev/null @@ -1,8 +0,0 @@ -class User(object): - def __init__(self, user_id, username, secret): - self.id = user_id - self.username = username - self.secret = secret - - def __str__(self): - return "User(id='%s')" % self.id 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 5fd0d2cda..67f7db115 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 @@ -13,8 +13,7 @@ WITH_CREDENTIALS = None NO_CREDENTIALS = None PARTIAL_CREDENTIALS = None -EMPTY_USER_CREDENTIALS = UserCreds("", "") -FULL_USER_CREDENTIALS = UserCreds(username="test", password_hash="1231234") +USER_CREDENTIALS = UserCreds(username="test", password_hash="1231234") # This fixture is a dirty hack that can be removed once these tests are converted from @@ -49,24 +48,18 @@ class StubEnvironmentConfig(EnvironmentConfig): class TestEnvironment(TestCase): class EnvironmentCredentialsRequired(Environment): def __init__(self): - config = StubEnvironmentConfig("test", "test", EMPTY_USER_CREDENTIALS) + config = StubEnvironmentConfig("test", "test", None) super().__init__(config) - def get_auth_users(self): - return [] - class EnvironmentAlreadyRegistered(Environment): def __init__(self): config = StubEnvironmentConfig("test", "test", UserCreds("test_user", "test_secret")) super().__init__(config) - def get_auth_users(self): - return [1, "Test_username", "Test_secret"] - @patch.object(target=EnvironmentConfig, attribute="save_to_file", new=MagicMock()) def test_try_add_user(self): env = TestEnvironment.EnvironmentCredentialsRequired() - credentials = FULL_USER_CREDENTIALS + credentials = USER_CREDENTIALS env.try_add_user(credentials) credentials = UserCreds(username="test", password_hash="") diff --git a/monkey/tests/unit_tests/monkey_island/cc/environment/test_environment_config.py b/monkey/tests/unit_tests/monkey_island/cc/environment/test_environment_config.py index 95269ab69..13a50c62e 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/environment/test_environment_config.py +++ b/monkey/tests/unit_tests/monkey_island/cc/environment/test_environment_config.py @@ -82,10 +82,9 @@ def test_add_user(config_file, with_credentials): assert from_file["environment"]["password_hash"] == new_password_hash -def test_get_user(with_credentials): +def test_user(with_credentials): environment_config = EnvironmentConfig(with_credentials) - user = environment_config.get_user() + user = environment_config.user_creds - assert user.id == 1 assert user.username == "test" - assert user.secret == "abcdef" + assert user.password_hash == "abcdef" diff --git a/monkey/tests/unit_tests/monkey_island/cc/environment/test_user_creds.py b/monkey/tests/unit_tests/monkey_island/cc/environment/test_user_creds.py index 7d83ba59f..d629687d6 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/environment/test_user_creds.py +++ b/monkey/tests/unit_tests/monkey_island/cc/environment/test_user_creds.py @@ -30,14 +30,6 @@ def test_to_dict_full_creds(): assert user_creds.to_dict() == {"user": TEST_USER, "password_hash": TEST_HASH} -def test_to_auth_user_full_credentials(): - user_creds = UserCreds(TEST_USER, TEST_HASH) - auth_user = user_creds.to_auth_user() - assert auth_user.id == 1 - assert auth_user.username == TEST_USER - assert auth_user.secret == TEST_HASH - - def test_member_values(monkeypatch): creds = UserCreds(TEST_USER, TEST_HASH) assert creds.username == TEST_USER