Skip to content

Commit 1a0c3df

Browse files
mssalvatoreVakarisZ
authored andcommitted
Merge branch '2157-remove-i-user-repository' into 2157-switch-to-token-based-auth
Issue #2157 PR #3005
2 parents 7e90f2e + 7bc381d commit 1a0c3df

File tree

11 files changed

+8
-463
lines changed

11 files changed

+8
-463
lines changed

monkey/common/utils/exceptions.py

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ class AlreadyRegisteredError(Exception):
1010
"""Raise to indicate the reason why registration is not required"""
1111

1212

13-
class UnknownUserError(Exception):
14-
"""Raise to indicate that authentication failed"""
15-
16-
1713
class IncorrectCredentialsError(Exception):
1814
"""Raise to indicate that authentication failed"""
1915

monkey/monkey_island/cc/models/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Order of importing matters here, for registering the embedded and referenced documents before
22
# using them.
33
from .simulation import Simulation, IslandMode
4-
from .user_credentials import UserCredentials
54
from common.types import MachineID
65
from .machine import Machine, NetworkServices
76
from .communication_type import CommunicationType

monkey/monkey_island/cc/models/user_credentials.py

-20
This file was deleted.

monkey/monkey_island/cc/repositories/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from .i_agent_binary_repository import IAgentBinaryRepository
66
from .i_simulation_repository import ISimulationRepository
77
from .i_credentials_repository import ICredentialsRepository
8-
from .i_user_repository import IUserRepository
98
from .i_machine_repository import IMachineRepository
109
from .i_agent_repository import IAgentRepository
1110
from .i_node_repository import INodeRepository
@@ -25,7 +24,6 @@
2524

2625
from .agent_binary_repository import AgentBinaryRepository
2726
from .file_simulation_repository import FileSimulationRepository
28-
from .json_file_user_repository import JSONFileUserRepository
2927
from .mongo_credentials_repository import MongoCredentialsRepository
3028
from .mongo_machine_repository import MongoMachineRepository
3129
from .mongo_agent_repository import MongoAgentRepository

monkey/monkey_island/cc/repositories/i_user_repository.py

-36
This file was deleted.

monkey/monkey_island/cc/repositories/json_file_user_repository.py

-57
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
from pathlib import Path
22

3-
import bcrypt
4-
5-
from common.utils.exceptions import (
6-
IncorrectCredentialsError,
7-
InvalidRegistrationCredentialsError,
8-
UnknownUserError,
9-
)
3+
from common.utils.exceptions import InvalidRegistrationCredentialsError
104
from monkey_island.cc.event_queue import IIslandEventQueue, IslandEventTopic
11-
from monkey_island.cc.models import IslandMode, UserCredentials
12-
from monkey_island.cc.repositories import IUserRepository
5+
from monkey_island.cc.models import IslandMode
136
from monkey_island.cc.server_utils.encryption import ILockableEncryptor
147

158

@@ -21,23 +14,13 @@ class AuthenticationService:
2114
def __init__(
2215
self,
2316
data_dir: Path,
24-
user_repository: IUserRepository,
2517
repository_encryptor: ILockableEncryptor,
2618
island_event_queue: IIslandEventQueue,
2719
):
2820
self._data_dir = data_dir
29-
self._user_repository = user_repository
3021
self._repository_encryptor = repository_encryptor
3122
self._island_event_queue = island_event_queue
3223

33-
def needs_registration(self) -> bool:
34-
"""
35-
Checks if a user is already registered on the Island
36-
37-
:return: Whether registration is required on the Island
38-
"""
39-
return not self._user_repository.has_registered_users()
40-
4124
def register_new_user(self, username: str, password: str):
4225
"""
4326
Registers a new user on the Island, then resets the encryptor and database
@@ -49,9 +32,6 @@ def register_new_user(self, username: str, password: str):
4932
if not username or not password:
5033
raise InvalidRegistrationCredentialsError("Username or password can not be empty.")
5134

52-
credentials = UserCredentials(username, _hash_password(password))
53-
self._user_repository.add_user(credentials)
54-
5535
self._island_event_queue.publish(IslandEventTopic.CLEAR_SIMULATION_DATA)
5636
self._island_event_queue.publish(IslandEventTopic.RESET_AGENT_CONFIGURATION)
5737
self._island_event_queue.publish(
@@ -61,14 +41,6 @@ def register_new_user(self, username: str, password: str):
6141
self._reset_repository_encryptor(username, password)
6242

6343
def authenticate(self, username: str, password: str):
64-
try:
65-
registered_user = self._user_repository.get_user_credentials(username)
66-
except UnknownUserError:
67-
raise IncorrectCredentialsError()
68-
69-
if not _credentials_match_registered_user(username, password, registered_user):
70-
raise IncorrectCredentialsError()
71-
7244
self._unlock_repository_encryptor(username, password)
7345

7446
def _unlock_repository_encryptor(self, username: str, password: str):
@@ -81,24 +53,5 @@ def _reset_repository_encryptor(self, username: str, password: str):
8153
self._repository_encryptor.unlock(secret.encode())
8254

8355

84-
def _hash_password(plaintext_password: str) -> str:
85-
salt = bcrypt.gensalt()
86-
password_hash = bcrypt.hashpw(plaintext_password.encode("utf-8"), salt)
87-
88-
return password_hash.decode()
89-
90-
91-
def _credentials_match_registered_user(
92-
username: str, password: str, registered_user: UserCredentials
93-
) -> bool:
94-
return (registered_user.username == username) and _password_matches_hash(
95-
password, registered_user.password_hash
96-
)
97-
98-
99-
def _password_matches_hash(plaintext_password: str, password_hash: str) -> bool:
100-
return bcrypt.checkpw(plaintext_password.encode("utf-8"), password_hash.encode("utf-8"))
101-
102-
10356
def _get_secret_from_credentials(username: str, password: str) -> str:
10457
return f"{username}:{password}"

monkey/monkey_island/cc/services/initialize.py

-3
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
IMachineRepository,
5151
INodeRepository,
5252
ISimulationRepository,
53-
IUserRepository,
54-
JSONFileUserRepository,
5553
LocalStorageFileRepository,
5654
MongoAgentEventRepository,
5755
MongoAgentRepository,
@@ -164,7 +162,6 @@ def _register_repositories(container: DIContainer, data_dir: Path):
164162
container.register_instance(
165163
ICredentialsRepository, container.resolve(MongoCredentialsRepository)
166164
)
167-
container.register_instance(IUserRepository, container.resolve(JSONFileUserRepository))
168165
container.register_instance(IAgentEventRepository, container.resolve(MongoAgentEventRepository))
169166

170167
container.register_instance(INodeRepository, container.resolve(MongoNodeRepository))

monkey/tests/unit_tests/monkey_island/cc/models/test_user_credentials.py

-36
This file was deleted.

0 commit comments

Comments
 (0)