Island: Rename UserCreds -> UserCredentials

This commit is contained in:
Mike Salvatore 2022-07-12 08:56:08 -04:00
parent 8608b05c46
commit 735514d61f
8 changed files with 34 additions and 34 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.")

View File

@ -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

View File

@ -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)

View File

@ -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)