From 735514d61fb3fac94427f7106b58b289aa3ab77a Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 12 Jul 2022 08:56:08 -0400 Subject: [PATCH] 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)