From 96a796856531cc7a3032d18f962570c4890d777c Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 12 Jul 2022 08:04:01 -0400 Subject: [PATCH 1/8] Island: Make AuthenticationService explicitly stateful --- .../monkey_island/cc/resources/auth/auth.py | 5 ++- .../cc/resources/auth/registration.py | 7 +++- .../authentication/authentication_service.py | 42 +++++++------------ .../monkey_island/cc/services/initialize.py | 4 +- .../cc/resources/auth/conftest.py | 24 +++++++++++ .../cc/resources/auth/test_auth.py | 10 ----- .../cc/resources/auth/test_registration.py | 13 ------ .../services/test_authentication_service.py | 24 ++++------- 8 files changed, 59 insertions(+), 70 deletions(-) create mode 100644 monkey/tests/unit_tests/monkey_island/cc/resources/auth/conftest.py diff --git a/monkey/monkey_island/cc/resources/auth/auth.py b/monkey/monkey_island/cc/resources/auth/auth.py index 811f4238b..69aa1ffcd 100644 --- a/monkey/monkey_island/cc/resources/auth/auth.py +++ b/monkey/monkey_island/cc/resources/auth/auth.py @@ -29,6 +29,9 @@ class Authenticate(AbstractResource): urls = ["/api/auth"] + def __init__(self, authentication_service: AuthenticationService): + self._authentication_service = authentication_service + def post(self): """ Example request: \ @@ -41,7 +44,7 @@ class Authenticate(AbstractResource): username, password = get_username_password_from_request(request) try: - AuthenticationService.authenticate(username, password) + self._authentication_service.authenticate(username, password) access_token = create_access_token(username) except IncorrectCredentialsError: return make_response({"error": "Invalid credentials"}, 401) diff --git a/monkey/monkey_island/cc/resources/auth/registration.py b/monkey/monkey_island/cc/resources/auth/registration.py index 9ef640915..e4928165f 100644 --- a/monkey/monkey_island/cc/resources/auth/registration.py +++ b/monkey/monkey_island/cc/resources/auth/registration.py @@ -14,14 +14,17 @@ class Registration(AbstractResource): urls = ["/api/registration"] + def __init__(self, authentication_service: AuthenticationService): + self._authentication_service = authentication_service + def get(self): - return {"needs_registration": AuthenticationService.needs_registration()} + return {"needs_registration": self._authentication_service.needs_registration()} def post(self): username, password = get_username_password_from_request(request) try: - AuthenticationService.register_new_user(username, password) + self._authentication_service.register_new_user(username, password) return make_response({"error": ""}, 200) # API Spec: HTTP status code for AlreadyRegisteredError should be 409 (CONFLICT) except (InvalidRegistrationCredentialsError, AlreadyRegisteredError) as e: diff --git a/monkey/monkey_island/cc/services/authentication/authentication_service.py b/monkey/monkey_island/cc/services/authentication/authentication_service.py index 93fbf32f4..9e52a2649 100644 --- a/monkey/monkey_island/cc/services/authentication/authentication_service.py +++ b/monkey/monkey_island/cc/services/authentication/authentication_service.py @@ -18,52 +18,40 @@ from .user_creds import UserCreds class AuthenticationService: - DATA_DIR = None - user_datastore = None + def __init__(self, data_dir: Path, user_datastore: IUserDatastore): + self._data_dir = data_dir + self._user_datastore = user_datastore - # TODO: A number of these services should be instance objects instead of - # static/singleton hybrids. At the moment, this requires invasive refactoring that's - # not a priority. - @classmethod - def initialize(cls, data_dir: Path, user_datastore: IUserDatastore): - cls.DATA_DIR = data_dir - cls.user_datastore = user_datastore + def needs_registration(self) -> bool: + return not self._user_datastore.has_registered_users() - @classmethod - def needs_registration(cls) -> bool: - return not cls.user_datastore.has_registered_users() - - @classmethod - def register_new_user(cls, username: str, password: str): + def register_new_user(self, username: str, password: str): if not username or not password: raise InvalidRegistrationCredentialsError("Username or password can not be empty.") credentials = UserCreds(username, _hash_password(password)) - cls.user_datastore.add_user(credentials) - cls._reset_datastore_encryptor(username, password) + self._user_datastore.add_user(credentials) + self._reset_datastore_encryptor(username, password) reset_database() - @classmethod - def authenticate(cls, username: str, password: str): + def authenticate(self, username: str, password: str): try: - registered_user = cls.user_datastore.get_user_credentials(username) + registered_user = self._user_datastore.get_user_credentials(username) except UnknownUserError: raise IncorrectCredentialsError() if not _credentials_match_registered_user(username, password, registered_user): raise IncorrectCredentialsError() - cls._unlock_datastore_encryptor(username, password) + self._unlock_datastore_encryptor(username, password) - @classmethod - def _unlock_datastore_encryptor(cls, username: str, password: str): + def _unlock_datastore_encryptor(self, username: str, password: str): secret = _get_secret_from_credentials(username, password) - unlock_datastore_encryptor(cls.DATA_DIR, secret) + unlock_datastore_encryptor(self._data_dir, secret) - @classmethod - def _reset_datastore_encryptor(cls, username: str, password: str): + def _reset_datastore_encryptor(self, username: str, password: str): secret = _get_secret_from_credentials(username, password) - reset_datastore_encryptor(cls.DATA_DIR, secret) + reset_datastore_encryptor(self._data_dir, secret) def _hash_password(plaintext_password: str) -> str: diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index 7d9bde9e6..54148e2a1 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -41,6 +41,7 @@ from monkey_island.cc.services.telemetry.processing.processing import ( from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL from . import AuthenticationService, JsonFileUserDatastore +from .authentication.i_user_datastore import IUserDatastore from .reporting.report import ReportService logger = logging.getLogger(__name__) @@ -64,7 +65,6 @@ def initialize_services(data_dir: Path) -> DIContainer: # This is temporary until we get DI all worked out. PostBreachFilesService.initialize(container.resolve(IFileRepository)) - AuthenticationService.initialize(data_dir, JsonFileUserDatastore(data_dir)) ReportService.initialize(container.resolve(AWSService)) return container @@ -95,6 +95,7 @@ def _register_repositories(container: DIContainer, data_dir: Path): container.register_instance( ICredentialsRepository, container.resolve(MongoCredentialsRepository) ) + container.register_instance(IUserDatastore, container.resolve(JsonFileUserDatastore)) def _decorate_file_repository(file_repository: IFileRepository) -> IFileRepository: @@ -140,6 +141,7 @@ def _register_services(container: DIContainer): container.register_instance(AWSService, container.resolve(AWSService)) container.register_instance(LocalMonkeyRunService, container.resolve(LocalMonkeyRunService)) container.register_instance(IslandModeService, container.resolve(IslandModeService)) + container.register_instance(AuthenticationService, container.resolve(AuthenticationService)) def _patch_credentials_parser(container: DIContainer): diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/auth/conftest.py b/monkey/tests/unit_tests/monkey_island/cc/resources/auth/conftest.py new file mode 100644 index 000000000..3cb37c638 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/auth/conftest.py @@ -0,0 +1,24 @@ +from unittest.mock import MagicMock + +import pytest +from tests.common import StubDIContainer + +from monkey_island.cc.services import AuthenticationService + + +@pytest.fixture +def mock_authentication_service(): + mock_service = MagicMock(spec=AuthenticationService) + mock_service.authenticate = MagicMock() + + return mock_service + + +@pytest.fixture +def flask_client(build_flask_client, mock_authentication_service): + container = StubDIContainer() + + container.register_instance(AuthenticationService, mock_authentication_service) + + with build_flask_client(container) as flask_client: + yield flask_client diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_auth.py b/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_auth.py index 73466a47d..b3f10f5df 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_auth.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_auth.py @@ -11,16 +11,6 @@ PASSWORD = "test_password" TEST_REQUEST = f'{{"username": "{USERNAME}", "password": "{PASSWORD}"}}' -@pytest.fixture -def mock_authentication_service(monkeypatch): - mock_service = MagicMock() - mock_service.authenticate = MagicMock() - - monkeypatch.setattr("monkey_island.cc.resources.auth.auth.AuthenticationService", mock_service) - - return mock_service - - @pytest.fixture def make_auth_request(flask_client): url = Authenticate.urls[0] diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_registration.py b/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_registration.py index e7bab544c..c10f0b011 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_registration.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_registration.py @@ -12,19 +12,6 @@ USERNAME = "test_user" PASSWORD = "test_password" -@pytest.fixture -def mock_authentication_service(monkeypatch): - mock_service = MagicMock() - mock_service.register_new_user = MagicMock() - mock_service.needs_registration = MagicMock() - - monkeypatch.setattr( - "monkey_island.cc.resources.auth.registration.AuthenticationService", mock_service - ) - - return mock_service - - @pytest.fixture def make_registration_request(flask_client): def inner(request_body): diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py index 8df77e00a..402f77616 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py @@ -69,8 +69,7 @@ def test_needs_registration__true(tmp_path): has_registered_users = False mock_user_datastore = MockUserDatastore(lambda: has_registered_users, None, None) - a_s = AuthenticationService() - a_s.initialize(tmp_path, mock_user_datastore) + a_s = AuthenticationService(tmp_path, mock_user_datastore) assert a_s.needs_registration() @@ -79,8 +78,7 @@ def test_needs_registration__false(tmp_path): has_registered_users = True mock_user_datastore = MockUserDatastore(lambda: has_registered_users, None, None) - a_s = AuthenticationService() - a_s.initialize(tmp_path, mock_user_datastore) + a_s = AuthenticationService(tmp_path, mock_user_datastore) assert not a_s.needs_registration() @@ -92,8 +90,7 @@ def test_register_new_user__fails( ): mock_user_datastore = MockUserDatastore(lambda: True, MagicMock(side_effect=error), None) - a_s = AuthenticationService() - a_s.initialize(tmp_path, mock_user_datastore) + a_s = AuthenticationService(tmp_path, mock_user_datastore) with pytest.raises(error): a_s.register_new_user(USERNAME, PASSWORD) @@ -107,8 +104,7 @@ def test_register_new_user__empty_password_fails( ): mock_user_datastore = MockUserDatastore(lambda: False, None, None) - a_s = AuthenticationService() - a_s.initialize(tmp_path, mock_user_datastore) + a_s = AuthenticationService(tmp_path, mock_user_datastore) with pytest.raises(InvalidRegistrationCredentialsError): a_s.register_new_user(USERNAME, "") @@ -122,8 +118,7 @@ def test_register_new_user(tmp_path, mock_reset_datastore_encryptor, mock_reset_ mock_add_user = MagicMock() mock_user_datastore = MockUserDatastore(lambda: False, mock_add_user, None) - a_s = AuthenticationService() - a_s.initialize(tmp_path, mock_user_datastore) + a_s = AuthenticationService(tmp_path, mock_user_datastore) a_s.register_new_user(USERNAME, PASSWORD) @@ -144,8 +139,7 @@ def test_authenticate__success(tmp_path, mock_unlock_datastore_encryptor): lambda _: UserCreds(USERNAME, PASSWORD_HASH), ) - a_s = AuthenticationService() - a_s.initialize(tmp_path, mock_user_datastore) + a_s = AuthenticationService(tmp_path, mock_user_datastore) # If authentication fails, this function will raise an exception and the test will fail. a_s.authenticate(USERNAME, PASSWORD) @@ -165,8 +159,7 @@ def test_authenticate__failed_wrong_credentials( lambda _: UserCreds(USERNAME, PASSWORD_HASH), ) - a_s = AuthenticationService() - a_s.initialize(tmp_path, mock_user_datastore) + a_s = AuthenticationService(tmp_path, mock_user_datastore) with pytest.raises(IncorrectCredentialsError): a_s.authenticate(username, password) @@ -179,8 +172,7 @@ def test_authenticate__failed_no_registered_user(tmp_path, mock_unlock_datastore lambda: True, None, MagicMock(side_effect=UnknownUserError) ) - a_s = AuthenticationService() - a_s.initialize(tmp_path, mock_user_datastore) + a_s = AuthenticationService(tmp_path, mock_user_datastore) with pytest.raises(IncorrectCredentialsError): a_s.authenticate(USERNAME, PASSWORD) From 50d08c3aefa36524b4edf597a27f3a4baf7055c5 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 12 Jul 2022 08:06:21 -0400 Subject: [PATCH 2/8] Island: Rename IUserDatastore -> IUserRepository --- .../cc/services/authentication/authentication_service.py | 4 ++-- .../cc/services/authentication/i_user_datastore.py | 2 +- .../cc/services/authentication/json_file_user_datastore.py | 4 ++-- monkey/monkey_island/cc/services/initialize.py | 4 ++-- .../monkey_island/cc/services/test_authentication_service.py | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/monkey/monkey_island/cc/services/authentication/authentication_service.py b/monkey/monkey_island/cc/services/authentication/authentication_service.py index 9e52a2649..8e825fbea 100644 --- a/monkey/monkey_island/cc/services/authentication/authentication_service.py +++ b/monkey/monkey_island/cc/services/authentication/authentication_service.py @@ -13,12 +13,12 @@ from monkey_island.cc.server_utils.encryption import ( ) from monkey_island.cc.setup.mongo.database_initializer import reset_database -from .i_user_datastore import IUserDatastore +from .i_user_datastore import IUserRepository from .user_creds import UserCreds class AuthenticationService: - def __init__(self, data_dir: Path, user_datastore: IUserDatastore): + def __init__(self, data_dir: Path, user_datastore: IUserRepository): self._data_dir = data_dir self._user_datastore = user_datastore diff --git a/monkey/monkey_island/cc/services/authentication/i_user_datastore.py b/monkey/monkey_island/cc/services/authentication/i_user_datastore.py index 38e93922e..c25c11ee6 100644 --- a/monkey/monkey_island/cc/services/authentication/i_user_datastore.py +++ b/monkey/monkey_island/cc/services/authentication/i_user_datastore.py @@ -3,7 +3,7 @@ import abc from .user_creds import UserCreds -class IUserDatastore(metaclass=abc.ABCMeta): +class IUserRepository(metaclass=abc.ABCMeta): """ Allows user credentials to be stored and retrieved. """ diff --git a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py b/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py index ff66a7c9b..8ac3e35d7 100644 --- a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py +++ b/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py @@ -8,13 +8,13 @@ from common.utils.exceptions import ( ) from monkey_island.cc.server_utils.file_utils import open_new_securely_permissioned_file -from .i_user_datastore import IUserDatastore +from .i_user_datastore import IUserRepository from .user_creds import UserCreds CREDENTIALS_FILE = "credentials.json" -class JsonFileUserDatastore(IUserDatastore): +class JsonFileUserDatastore(IUserRepository): def __init__(self, data_dir: Path): self._credentials = None self._credentials_file = data_dir / CREDENTIALS_FILE diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index 54148e2a1..02068c382 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -41,7 +41,7 @@ from monkey_island.cc.services.telemetry.processing.processing import ( from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL from . import AuthenticationService, JsonFileUserDatastore -from .authentication.i_user_datastore import IUserDatastore +from .authentication.i_user_datastore import IUserRepository from .reporting.report import ReportService logger = logging.getLogger(__name__) @@ -95,7 +95,7 @@ def _register_repositories(container: DIContainer, data_dir: Path): container.register_instance( ICredentialsRepository, container.resolve(MongoCredentialsRepository) ) - container.register_instance(IUserDatastore, container.resolve(JsonFileUserDatastore)) + container.register_instance(IUserRepository, container.resolve(JsonFileUserDatastore)) def _decorate_file_repository(file_repository: IFileRepository) -> IFileRepository: diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py index 402f77616..673d9ab33 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py @@ -10,7 +10,7 @@ from common.utils.exceptions import ( ) from monkey_island.cc.services import AuthenticationService from monkey_island.cc.services.authentication import authentication_service -from monkey_island.cc.services.authentication.i_user_datastore import IUserDatastore +from monkey_island.cc.services.authentication.i_user_datastore import IUserRepository from monkey_island.cc.services.authentication.user_creds import UserCreds USERNAME = "user1" @@ -18,7 +18,7 @@ PASSWORD = "test" PASSWORD_HASH = "$2b$12$YsGjjuJFddYJ6z5S5/nMCuKkCzKHB1AWY9SXkQ02i25d8TgdhIRS2" -class MockUserDatastore(IUserDatastore): +class MockUserDatastore(IUserRepository): def __init__(self, has_registered_users, add_user, get_user_credentials): self._has_registered_users = has_registered_users self._add_user = add_user From 7c98ca0004f3deb25ea2af4aad384abd0787c966 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 12 Jul 2022 08:07:33 -0400 Subject: [PATCH 3/8] Island: Rename JsonFileUserDatastore -> JSONFileUserRepository --- monkey/monkey_island/cc/services/__init__.py | 2 +- .../authentication/json_file_user_datastore.py | 2 +- monkey/monkey_island/cc/services/initialize.py | 4 ++-- .../cc/services/test_json_file_user_datastore.py | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/monkey/monkey_island/cc/services/__init__.py b/monkey/monkey_island/cc/services/__init__.py index bfecc7588..4adb18eb2 100644 --- a/monkey/monkey_island/cc/services/__init__.py +++ b/monkey/monkey_island/cc/services/__init__.py @@ -1,5 +1,5 @@ from .authentication.authentication_service import AuthenticationService -from .authentication.json_file_user_datastore import JsonFileUserDatastore +from .authentication.json_file_user_datastore import JSONFileUserRepository from .aws import AWSService from .island_mode_service import IslandModeService diff --git a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py b/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py index 8ac3e35d7..87ef873e1 100644 --- a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py +++ b/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py @@ -14,7 +14,7 @@ from .user_creds import UserCreds CREDENTIALS_FILE = "credentials.json" -class JsonFileUserDatastore(IUserRepository): +class JSONFileUserRepository(IUserRepository): def __init__(self, data_dir: Path): self._credentials = None self._credentials_file = data_dir / CREDENTIALS_FILE diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index 02068c382..d5b197059 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -40,7 +40,7 @@ from monkey_island.cc.services.telemetry.processing.processing import ( ) from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL -from . import AuthenticationService, JsonFileUserDatastore +from . import AuthenticationService, JSONFileUserRepository from .authentication.i_user_datastore import IUserRepository from .reporting.report import ReportService @@ -95,7 +95,7 @@ def _register_repositories(container: DIContainer, data_dir: Path): container.register_instance( ICredentialsRepository, container.resolve(MongoCredentialsRepository) ) - container.register_instance(IUserRepository, container.resolve(JsonFileUserDatastore)) + container.register_instance(IUserRepository, container.resolve(JSONFileUserRepository)) def _decorate_file_repository(file_repository: IFileRepository) -> IFileRepository: diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py index 4aae5cd3d..3681ee7fc 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py @@ -12,7 +12,7 @@ from common.utils.exceptions import ( from monkey_island.cc.server_utils.file_utils import is_windows_os from monkey_island.cc.services.authentication.json_file_user_datastore import ( CREDENTIALS_FILE, - JsonFileUserDatastore, + JSONFileUserRepository, ) from monkey_island.cc.services.authentication.user_creds import UserCreds @@ -22,12 +22,12 @@ PASSWORD_HASH = "DEADBEEF" @pytest.fixture def empty_datastore(tmp_path): - return JsonFileUserDatastore(tmp_path) + return JSONFileUserRepository(tmp_path) @pytest.fixture def populated_datastore(data_for_tests_dir): - return JsonFileUserDatastore(data_for_tests_dir) + return JSONFileUserRepository(data_for_tests_dir) @pytest.fixture @@ -91,10 +91,10 @@ def test_add_user__already_registered(populated_datastore): def test_get_user_credentials_from_file(tmp_path): - empty_datastore = JsonFileUserDatastore(tmp_path) + empty_datastore = JSONFileUserRepository(tmp_path) empty_datastore.add_user(UserCreds(USERNAME, PASSWORD_HASH)) - populated_datastore = JsonFileUserDatastore(tmp_path) + populated_datastore = JSONFileUserRepository(tmp_path) stored_user = populated_datastore.get_user_credentials(USERNAME) assert stored_user.username == USERNAME From 292887baf8d0475ca9b47f01bb79f188d8266107 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 12 Jul 2022 08:14:41 -0400 Subject: [PATCH 4/8] Island: Move IUserRepository to repository/ package --- monkey/monkey_island/cc/repository/__init__.py | 1 + .../i_user_datastore.py => repository/i_user_repository.py} | 2 +- .../cc/services/authentication/authentication_service.py | 2 +- .../cc/services/authentication/json_file_user_datastore.py | 2 +- monkey/monkey_island/cc/services/initialize.py | 2 +- .../monkey_island/cc/services/test_authentication_service.py | 2 +- 6 files changed, 6 insertions(+), 5 deletions(-) rename monkey/monkey_island/cc/{services/authentication/i_user_datastore.py => repository/i_user_repository.py} (93%) diff --git a/monkey/monkey_island/cc/repository/__init__.py b/monkey/monkey_island/cc/repository/__init__.py index d67515ee6..232a0b165 100644 --- a/monkey/monkey_island/cc/repository/__init__.py +++ b/monkey/monkey_island/cc/repository/__init__.py @@ -6,6 +6,7 @@ from .i_agent_binary_repository import IAgentBinaryRepository from .i_agent_configuration_repository import IAgentConfigurationRepository from .i_simulation_repository import ISimulationRepository from .i_credentials_repository import ICredentialsRepository +from .i_user_repository import IUserRepository from .local_storage_file_repository import LocalStorageFileRepository diff --git a/monkey/monkey_island/cc/services/authentication/i_user_datastore.py b/monkey/monkey_island/cc/repository/i_user_repository.py similarity index 93% rename from monkey/monkey_island/cc/services/authentication/i_user_datastore.py rename to monkey/monkey_island/cc/repository/i_user_repository.py index c25c11ee6..ee0e17f0a 100644 --- a/monkey/monkey_island/cc/services/authentication/i_user_datastore.py +++ b/monkey/monkey_island/cc/repository/i_user_repository.py @@ -1,6 +1,6 @@ import abc -from .user_creds import UserCreds +from monkey_island.cc.services.authentication.user_creds import UserCreds class IUserRepository(metaclass=abc.ABCMeta): diff --git a/monkey/monkey_island/cc/services/authentication/authentication_service.py b/monkey/monkey_island/cc/services/authentication/authentication_service.py index 8e825fbea..d8f445115 100644 --- a/monkey/monkey_island/cc/services/authentication/authentication_service.py +++ b/monkey/monkey_island/cc/services/authentication/authentication_service.py @@ -7,13 +7,13 @@ from common.utils.exceptions import ( InvalidRegistrationCredentialsError, UnknownUserError, ) +from monkey_island.cc.repository import IUserRepository from monkey_island.cc.server_utils.encryption import ( reset_datastore_encryptor, unlock_datastore_encryptor, ) from monkey_island.cc.setup.mongo.database_initializer import reset_database -from .i_user_datastore import IUserRepository from .user_creds import UserCreds diff --git a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py b/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py index 87ef873e1..a75310733 100644 --- a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py +++ b/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py @@ -6,9 +6,9 @@ from common.utils.exceptions import ( InvalidRegistrationCredentialsError, UnknownUserError, ) +from monkey_island.cc.repository import IUserRepository from monkey_island.cc.server_utils.file_utils import open_new_securely_permissioned_file -from .i_user_datastore import IUserRepository from .user_creds import UserCreds CREDENTIALS_FILE = "credentials.json" diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index d5b197059..9474ea3bd 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -24,6 +24,7 @@ from monkey_island.cc.repository import ( ICredentialsRepository, IFileRepository, ISimulationRepository, + IUserRepository, LocalStorageFileRepository, MongoCredentialsRepository, RetrievalError, @@ -41,7 +42,6 @@ from monkey_island.cc.services.telemetry.processing.processing import ( from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL from . import AuthenticationService, JSONFileUserRepository -from .authentication.i_user_datastore import IUserRepository from .reporting.report import ReportService logger = logging.getLogger(__name__) diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py index 673d9ab33..6aa3dfbbd 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py @@ -8,9 +8,9 @@ from common.utils.exceptions import ( InvalidRegistrationCredentialsError, UnknownUserError, ) +from monkey_island.cc.repository import IUserRepository from monkey_island.cc.services import AuthenticationService from monkey_island.cc.services.authentication import authentication_service -from monkey_island.cc.services.authentication.i_user_datastore import IUserRepository from monkey_island.cc.services.authentication.user_creds import UserCreds USERNAME = "user1" From 8608b05c465a46b2c0d8d0371ed5fe596606a9b5 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 12 Jul 2022 08:53:56 -0400 Subject: [PATCH 5/8] Island: Move UserCreds to models package --- monkey/monkey_island/cc/models/__init__.py | 1 + .../user_creds.py => models/user_credentials.py} | 0 monkey/monkey_island/cc/repository/i_user_repository.py | 2 +- .../cc/services/authentication/authentication_service.py | 3 +-- .../cc/services/authentication/json_file_user_datastore.py | 3 +-- .../test_user_creds.py => models/test_user_credentials.py} | 2 +- .../monkey_island/cc/services/test_authentication_service.py | 2 +- .../monkey_island/cc/services/test_json_file_user_datastore.py | 2 +- 8 files changed, 7 insertions(+), 8 deletions(-) rename monkey/monkey_island/cc/{services/authentication/user_creds.py => models/user_credentials.py} (100%) rename monkey/tests/unit_tests/monkey_island/cc/{services/authentication/test_user_creds.py => models/test_user_credentials.py} (91%) diff --git a/monkey/monkey_island/cc/models/__init__.py b/monkey/monkey_island/cc/models/__init__.py index bf2addf63..3ce8893de 100644 --- a/monkey/monkey_island/cc/models/__init__.py +++ b/monkey/monkey_island/cc/models/__init__.py @@ -9,3 +9,4 @@ from .pba_results import PbaResults from monkey_island.cc.models.report.report import Report from .stolen_credentials import StolenCredentials from .simulation import Simulation, SimulationSchema, IslandMode +from .user_credentials import UserCreds diff --git a/monkey/monkey_island/cc/services/authentication/user_creds.py b/monkey/monkey_island/cc/models/user_credentials.py similarity index 100% rename from monkey/monkey_island/cc/services/authentication/user_creds.py rename to monkey/monkey_island/cc/models/user_credentials.py diff --git a/monkey/monkey_island/cc/repository/i_user_repository.py b/monkey/monkey_island/cc/repository/i_user_repository.py index ee0e17f0a..1e5e7f0fd 100644 --- a/monkey/monkey_island/cc/repository/i_user_repository.py +++ b/monkey/monkey_island/cc/repository/i_user_repository.py @@ -1,6 +1,6 @@ import abc -from monkey_island.cc.services.authentication.user_creds import UserCreds +from monkey_island.cc.models import UserCreds class IUserRepository(metaclass=abc.ABCMeta): diff --git a/monkey/monkey_island/cc/services/authentication/authentication_service.py b/monkey/monkey_island/cc/services/authentication/authentication_service.py index d8f445115..8e5402be3 100644 --- a/monkey/monkey_island/cc/services/authentication/authentication_service.py +++ b/monkey/monkey_island/cc/services/authentication/authentication_service.py @@ -7,6 +7,7 @@ from common.utils.exceptions import ( InvalidRegistrationCredentialsError, UnknownUserError, ) +from monkey_island.cc.models import UserCreds from monkey_island.cc.repository import IUserRepository from monkey_island.cc.server_utils.encryption import ( reset_datastore_encryptor, @@ -14,8 +15,6 @@ from monkey_island.cc.server_utils.encryption import ( ) from monkey_island.cc.setup.mongo.database_initializer import reset_database -from .user_creds import UserCreds - class AuthenticationService: def __init__(self, data_dir: Path, user_datastore: IUserRepository): diff --git a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py b/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py index a75310733..3d9f0d45c 100644 --- a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py +++ b/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py @@ -6,11 +6,10 @@ from common.utils.exceptions import ( InvalidRegistrationCredentialsError, UnknownUserError, ) +from monkey_island.cc.models import UserCreds from monkey_island.cc.repository import IUserRepository from monkey_island.cc.server_utils.file_utils import open_new_securely_permissioned_file -from .user_creds import UserCreds - CREDENTIALS_FILE = "credentials.json" diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/authentication/test_user_creds.py b/monkey/tests/unit_tests/monkey_island/cc/models/test_user_credentials.py similarity index 91% rename from monkey/tests/unit_tests/monkey_island/cc/services/authentication/test_user_creds.py rename to monkey/tests/unit_tests/monkey_island/cc/models/test_user_credentials.py index 0bd9f6421..f294e2364 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/authentication/test_user_creds.py +++ b/monkey/tests/unit_tests/monkey_island/cc/models/test_user_credentials.py @@ -1,4 +1,4 @@ -from monkey_island.cc.services.authentication.user_creds import UserCreds +from monkey_island.cc.models import UserCreds TEST_USER = "Test" TEST_HASH = "abc1231234" diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py index 6aa3dfbbd..eac6f646c 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py @@ -8,10 +8,10 @@ from common.utils.exceptions import ( InvalidRegistrationCredentialsError, UnknownUserError, ) +from monkey_island.cc.models import UserCreds from monkey_island.cc.repository import IUserRepository from monkey_island.cc.services import AuthenticationService from monkey_island.cc.services.authentication import authentication_service -from monkey_island.cc.services.authentication.user_creds import UserCreds USERNAME = "user1" PASSWORD = "test" diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py index 3681ee7fc..9a8d1eb03 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py @@ -9,12 +9,12 @@ from common.utils.exceptions import ( InvalidRegistrationCredentialsError, UnknownUserError, ) +from monkey_island.cc.models import UserCreds from monkey_island.cc.server_utils.file_utils import is_windows_os from monkey_island.cc.services.authentication.json_file_user_datastore import ( CREDENTIALS_FILE, JSONFileUserRepository, ) -from monkey_island.cc.services.authentication.user_creds import UserCreds USERNAME = "test" PASSWORD_HASH = "DEADBEEF" From 735514d61fb3fac94427f7106b58b289aa3ab77a Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 12 Jul 2022 08:56:08 -0400 Subject: [PATCH 6/8] Island: Rename UserCreds -> UserCredentials --- monkey/monkey_island/cc/models/__init__.py | 2 +- .../monkey_island/cc/models/user_credentials.py | 2 +- .../cc/repository/i_user_repository.py | 6 +++--- .../authentication/authentication_service.py | 6 +++--- .../authentication/json_file_user_datastore.py | 10 +++++----- .../cc/models/test_user_credentials.py | 16 ++++++++-------- .../cc/services/test_authentication_service.py | 10 +++++----- .../cc/services/test_json_file_user_datastore.py | 16 ++++++++-------- 8 files changed, 34 insertions(+), 34 deletions(-) diff --git a/monkey/monkey_island/cc/models/__init__.py b/monkey/monkey_island/cc/models/__init__.py index 3ce8893de..5e4d1cef7 100644 --- a/monkey/monkey_island/cc/models/__init__.py +++ b/monkey/monkey_island/cc/models/__init__.py @@ -9,4 +9,4 @@ from .pba_results import PbaResults from monkey_island.cc.models.report.report import Report from .stolen_credentials import StolenCredentials from .simulation import Simulation, SimulationSchema, IslandMode -from .user_credentials import UserCreds +from .user_credentials import UserCredentials diff --git a/monkey/monkey_island/cc/models/user_credentials.py b/monkey/monkey_island/cc/models/user_credentials.py index a30edae5f..0e2c290b4 100644 --- a/monkey/monkey_island/cc/models/user_credentials.py +++ b/monkey/monkey_island/cc/models/user_credentials.py @@ -3,7 +3,7 @@ from __future__ import annotations from typing import Dict -class UserCreds: +class UserCredentials: def __init__(self, username, password_hash): self.username = username self.password_hash = password_hash diff --git a/monkey/monkey_island/cc/repository/i_user_repository.py b/monkey/monkey_island/cc/repository/i_user_repository.py index 1e5e7f0fd..906037b2a 100644 --- a/monkey/monkey_island/cc/repository/i_user_repository.py +++ b/monkey/monkey_island/cc/repository/i_user_repository.py @@ -1,6 +1,6 @@ import abc -from monkey_island.cc.models import UserCreds +from monkey_island.cc.models import UserCredentials class IUserRepository(metaclass=abc.ABCMeta): @@ -17,7 +17,7 @@ class IUserRepository(metaclass=abc.ABCMeta): """ @abc.abstractmethod - def add_user(self, credentials: UserCreds): + def add_user(self, credentials: UserCredentials): """ Adds a new user to the datastore. :param UserCreds credentials: New user credentials to persistant storage. @@ -26,7 +26,7 @@ class IUserRepository(metaclass=abc.ABCMeta): """ @abc.abstractmethod - def get_user_credentials(self, username: str) -> UserCreds: + def get_user_credentials(self, username: str) -> UserCredentials: """ Gets the user matching `username` from storage. :param str username: The username for which credentials will be retrieved diff --git a/monkey/monkey_island/cc/services/authentication/authentication_service.py b/monkey/monkey_island/cc/services/authentication/authentication_service.py index 8e5402be3..045ef75b2 100644 --- a/monkey/monkey_island/cc/services/authentication/authentication_service.py +++ b/monkey/monkey_island/cc/services/authentication/authentication_service.py @@ -7,7 +7,7 @@ from common.utils.exceptions import ( InvalidRegistrationCredentialsError, UnknownUserError, ) -from monkey_island.cc.models import UserCreds +from monkey_island.cc.models import UserCredentials from monkey_island.cc.repository import IUserRepository from monkey_island.cc.server_utils.encryption import ( reset_datastore_encryptor, @@ -28,7 +28,7 @@ class AuthenticationService: if not username or not password: raise InvalidRegistrationCredentialsError("Username or password can not be empty.") - credentials = UserCreds(username, _hash_password(password)) + credentials = UserCredentials(username, _hash_password(password)) self._user_datastore.add_user(credentials) self._reset_datastore_encryptor(username, password) reset_database() @@ -61,7 +61,7 @@ def _hash_password(plaintext_password: str) -> str: def _credentials_match_registered_user( - username: str, password: str, registered_user: UserCreds + username: str, password: str, registered_user: UserCredentials ) -> bool: return (registered_user.username == username) and _password_matches_hash( password, registered_user.password_hash diff --git a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py b/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py index 3d9f0d45c..985db9c69 100644 --- a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py +++ b/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py @@ -6,7 +6,7 @@ from common.utils.exceptions import ( InvalidRegistrationCredentialsError, UnknownUserError, ) -from monkey_island.cc.models import UserCreds +from monkey_island.cc.models import UserCredentials from monkey_island.cc.repository import IUserRepository from monkey_island.cc.server_utils.file_utils import open_new_securely_permissioned_file @@ -21,16 +21,16 @@ class JSONFileUserRepository(IUserRepository): if self._credentials_file.exists(): self._credentials = self._load_from_file() - def _load_from_file(self) -> UserCreds: + def _load_from_file(self) -> UserCredentials: with open(self._credentials_file, "r") as f: credentials_dict = json.load(f) - return UserCreds(credentials_dict["user"], credentials_dict["password_hash"]) + return UserCredentials(credentials_dict["user"], credentials_dict["password_hash"]) def has_registered_users(self) -> bool: return self._credentials is not None - def add_user(self, credentials: UserCreds): + def add_user(self, credentials: UserCredentials): if credentials is None: raise InvalidRegistrationCredentialsError("Credentials can not be 'None'") elif not credentials.username: @@ -50,7 +50,7 @@ class JSONFileUserRepository(IUserRepository): with open_new_securely_permissioned_file(self._credentials_file, "w") as f: json.dump(self._credentials.to_dict(), f, indent=2) - def get_user_credentials(self, username: str) -> UserCreds: + def get_user_credentials(self, username: str) -> UserCredentials: if self._credentials is None or self._credentials.username != username: raise UnknownUserError(f"User {username} does not exist.") diff --git a/monkey/tests/unit_tests/monkey_island/cc/models/test_user_credentials.py b/monkey/tests/unit_tests/monkey_island/cc/models/test_user_credentials.py index f294e2364..b90edde2c 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/models/test_user_credentials.py +++ b/monkey/tests/unit_tests/monkey_island/cc/models/test_user_credentials.py @@ -1,36 +1,36 @@ -from monkey_island.cc.models import UserCreds +from monkey_island.cc.models import UserCredentials TEST_USER = "Test" TEST_HASH = "abc1231234" def test_bool_true(): - assert UserCreds(TEST_USER, TEST_HASH) + assert UserCredentials(TEST_USER, TEST_HASH) def test_bool_false_empty_password_hash(): - assert not UserCreds(TEST_USER, "") + assert not UserCredentials(TEST_USER, "") def test_bool_false_empty_user(): - assert not UserCreds("", TEST_HASH) + assert not UserCredentials("", TEST_HASH) def test_bool_false_empty_user_and_password_hash(): - assert not UserCreds("", "") + assert not UserCredentials("", "") def test_to_dict_empty_creds(): - user_creds = UserCreds("", "") + user_creds = UserCredentials("", "") assert user_creds.to_dict() == {} def test_to_dict_full_creds(): - user_creds = UserCreds(TEST_USER, TEST_HASH) + user_creds = UserCredentials(TEST_USER, TEST_HASH) assert user_creds.to_dict() == {"user": TEST_USER, "password_hash": TEST_HASH} def test_member_values(monkeypatch): - creds = UserCreds(TEST_USER, TEST_HASH) + creds = UserCredentials(TEST_USER, TEST_HASH) assert creds.username == TEST_USER assert creds.password_hash == TEST_HASH diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py index eac6f646c..071f11828 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py @@ -8,7 +8,7 @@ from common.utils.exceptions import ( InvalidRegistrationCredentialsError, UnknownUserError, ) -from monkey_island.cc.models import UserCreds +from monkey_island.cc.models import UserCredentials from monkey_island.cc.repository import IUserRepository from monkey_island.cc.services import AuthenticationService from monkey_island.cc.services.authentication import authentication_service @@ -27,10 +27,10 @@ class MockUserDatastore(IUserRepository): def has_registered_users(self): return self._has_registered_users() - def add_user(self, credentials: UserCreds): + def add_user(self, credentials: UserCredentials): return self._add_user(credentials) - def get_user_credentials(self, username: str) -> UserCreds: + def get_user_credentials(self, username: str) -> UserCredentials: return self._get_user_credentials(username) @@ -136,7 +136,7 @@ def test_authenticate__success(tmp_path, mock_unlock_datastore_encryptor): mock_user_datastore = MockUserDatastore( lambda: True, None, - lambda _: UserCreds(USERNAME, PASSWORD_HASH), + lambda _: UserCredentials(USERNAME, PASSWORD_HASH), ) a_s = AuthenticationService(tmp_path, mock_user_datastore) @@ -156,7 +156,7 @@ def test_authenticate__failed_wrong_credentials( mock_user_datastore = MockUserDatastore( lambda: True, None, - lambda _: UserCreds(USERNAME, PASSWORD_HASH), + lambda _: UserCredentials(USERNAME, PASSWORD_HASH), ) a_s = AuthenticationService(tmp_path, mock_user_datastore) diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py index 9a8d1eb03..98119d7e5 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py @@ -9,7 +9,7 @@ from common.utils.exceptions import ( InvalidRegistrationCredentialsError, UnknownUserError, ) -from monkey_island.cc.models import UserCreds +from monkey_island.cc.models import UserCredentials from monkey_island.cc.server_utils.file_utils import is_windows_os from monkey_island.cc.services.authentication.json_file_user_datastore import ( CREDENTIALS_FILE, @@ -46,14 +46,14 @@ def test_has_registered_users_after_registration(populated_datastore): def test_add_user(empty_datastore, credentials_file_path): datastore = empty_datastore - datastore.add_user(UserCreds(USERNAME, PASSWORD_HASH)) + datastore.add_user(UserCredentials(USERNAME, PASSWORD_HASH)) assert datastore.has_registered_users() assert credentials_file_path.exists() @pytest.mark.skipif(is_windows_os(), reason="Tests Posix (not Windows) permissions.") def test_add_user__term_posix(empty_datastore, credentials_file_path): - empty_datastore.add_user(UserCreds(USERNAME, PASSWORD_HASH)) + empty_datastore.add_user(UserCredentials(USERNAME, PASSWORD_HASH)) st = os.stat(credentials_file_path) expected_mode = stat.S_IRUSR | stat.S_IWUSR @@ -66,7 +66,7 @@ def test_add_user__term_posix(empty_datastore, credentials_file_path): def test_add_user__term_windows(empty_datastore, credentials_file_path): datastore = empty_datastore - datastore.add_user(UserCreds(USERNAME, PASSWORD_HASH)) + datastore.add_user(UserCredentials(USERNAME, PASSWORD_HASH)) assert_windows_permissions(str(credentials_file_path)) @@ -77,22 +77,22 @@ def test_add_user__None_creds(empty_datastore): def test_add_user__empty_username(empty_datastore): with pytest.raises(InvalidRegistrationCredentialsError): - empty_datastore.add_user(UserCreds("", PASSWORD_HASH)) + empty_datastore.add_user(UserCredentials("", PASSWORD_HASH)) def test_add_user__empty_password_hash(empty_datastore): with pytest.raises(InvalidRegistrationCredentialsError): - empty_datastore.add_user(UserCreds(USERNAME, "")) + empty_datastore.add_user(UserCredentials(USERNAME, "")) def test_add_user__already_registered(populated_datastore): with pytest.raises(AlreadyRegisteredError): - populated_datastore.add_user(UserCreds("new_user", "new_hash")) + populated_datastore.add_user(UserCredentials("new_user", "new_hash")) def test_get_user_credentials_from_file(tmp_path): empty_datastore = JSONFileUserRepository(tmp_path) - empty_datastore.add_user(UserCreds(USERNAME, PASSWORD_HASH)) + empty_datastore.add_user(UserCredentials(USERNAME, PASSWORD_HASH)) populated_datastore = JSONFileUserRepository(tmp_path) stored_user = populated_datastore.get_user_credentials(USERNAME) From c1a9ad1e6fc59f81a47a9a58429c08a90c2343a5 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 12 Jul 2022 09:00:09 -0400 Subject: [PATCH 7/8] Island: Move JSONFileUserRepository to the repository package --- monkey/monkey_island/cc/repository/__init__.py | 1 + .../json_file_user_repository.py} | 0 monkey/monkey_island/cc/services/__init__.py | 1 - monkey/monkey_island/cc/services/initialize.py | 3 ++- .../test_json_file_user_repository.py} | 4 ++-- 5 files changed, 5 insertions(+), 4 deletions(-) rename monkey/monkey_island/cc/{services/authentication/json_file_user_datastore.py => repository/json_file_user_repository.py} (100%) rename monkey/tests/unit_tests/monkey_island/cc/{services/test_json_file_user_datastore.py => repository/test_json_file_user_repository.py} (97%) diff --git a/monkey/monkey_island/cc/repository/__init__.py b/monkey/monkey_island/cc/repository/__init__.py index 232a0b165..075317b43 100644 --- a/monkey/monkey_island/cc/repository/__init__.py +++ b/monkey/monkey_island/cc/repository/__init__.py @@ -17,4 +17,5 @@ from .file_repository_logging_decorator import FileRepositoryLoggingDecorator from .agent_binary_repository import AgentBinaryRepository from .file_agent_configuration_repository import FileAgentConfigurationRepository from .file_simulation_repository import FileSimulationRepository +from .json_file_user_repository import JSONFileUserRepository from .mongo_credentials_repository import MongoCredentialsRepository diff --git a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py b/monkey/monkey_island/cc/repository/json_file_user_repository.py similarity index 100% rename from monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py rename to monkey/monkey_island/cc/repository/json_file_user_repository.py diff --git a/monkey/monkey_island/cc/services/__init__.py b/monkey/monkey_island/cc/services/__init__.py index 4adb18eb2..91bea103c 100644 --- a/monkey/monkey_island/cc/services/__init__.py +++ b/monkey/monkey_island/cc/services/__init__.py @@ -1,5 +1,4 @@ from .authentication.authentication_service import AuthenticationService -from .authentication.json_file_user_datastore import JSONFileUserRepository from .aws import AWSService from .island_mode_service import IslandModeService diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index 9474ea3bd..4f326f58d 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -25,6 +25,7 @@ from monkey_island.cc.repository import ( IFileRepository, ISimulationRepository, IUserRepository, + JSONFileUserRepository, LocalStorageFileRepository, MongoCredentialsRepository, RetrievalError, @@ -41,7 +42,7 @@ from monkey_island.cc.services.telemetry.processing.processing import ( ) from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL -from . import AuthenticationService, JSONFileUserRepository +from . import AuthenticationService from .reporting.report import ReportService logger = logging.getLogger(__name__) diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py b/monkey/tests/unit_tests/monkey_island/cc/repository/test_json_file_user_repository.py similarity index 97% rename from monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py rename to monkey/tests/unit_tests/monkey_island/cc/repository/test_json_file_user_repository.py index 98119d7e5..050788c26 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repository/test_json_file_user_repository.py @@ -10,11 +10,11 @@ from common.utils.exceptions import ( UnknownUserError, ) from monkey_island.cc.models import UserCredentials -from monkey_island.cc.server_utils.file_utils import is_windows_os -from monkey_island.cc.services.authentication.json_file_user_datastore import ( +from monkey_island.cc.repository.json_file_user_repository import ( CREDENTIALS_FILE, JSONFileUserRepository, ) +from monkey_island.cc.server_utils.file_utils import is_windows_os USERNAME = "test" PASSWORD_HASH = "DEADBEEF" From 82d894f0c9ae9051facbe67786ce31054a61bb22 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 12 Jul 2022 09:01:18 -0400 Subject: [PATCH 8/8] Island: Remove unnecessary services.authentication subpackage --- monkey/monkey_island/cc/services/__init__.py | 2 +- monkey/monkey_island/cc/services/authentication/__init__.py | 0 .../cc/services/{authentication => }/authentication_service.py | 0 .../monkey_island/cc/services/test_authentication_service.py | 3 +-- 4 files changed, 2 insertions(+), 3 deletions(-) delete mode 100644 monkey/monkey_island/cc/services/authentication/__init__.py rename monkey/monkey_island/cc/services/{authentication => }/authentication_service.py (100%) diff --git a/monkey/monkey_island/cc/services/__init__.py b/monkey/monkey_island/cc/services/__init__.py index 91bea103c..14c5664e9 100644 --- a/monkey/monkey_island/cc/services/__init__.py +++ b/monkey/monkey_island/cc/services/__init__.py @@ -1,4 +1,4 @@ -from .authentication.authentication_service import AuthenticationService +from .authentication_service import AuthenticationService from .aws import AWSService from .island_mode_service import IslandModeService diff --git a/monkey/monkey_island/cc/services/authentication/__init__.py b/monkey/monkey_island/cc/services/authentication/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/monkey/monkey_island/cc/services/authentication/authentication_service.py b/monkey/monkey_island/cc/services/authentication_service.py similarity index 100% rename from monkey/monkey_island/cc/services/authentication/authentication_service.py rename to monkey/monkey_island/cc/services/authentication_service.py diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py index 071f11828..558d17a84 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_authentication_service.py @@ -10,8 +10,7 @@ from common.utils.exceptions import ( ) from monkey_island.cc.models import UserCredentials from monkey_island.cc.repository import IUserRepository -from monkey_island.cc.services import AuthenticationService -from monkey_island.cc.services.authentication import authentication_service +from monkey_island.cc.services import AuthenticationService, authentication_service USERNAME = "user1" PASSWORD = "test"