Merge pull request #2278 from guardicore/2234-remove-repository-service

Island: Remove RepositoryService
This commit is contained in:
Mike Salvatore 2022-09-13 08:01:36 -04:00 committed by GitHub
commit ef2bac6de2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 2 additions and 128 deletions

View File

@ -2,4 +2,3 @@ from .authentication_service import AuthenticationService
from .aws import AWSService
from .island_mode_service import IslandModeService
from .repository_service import RepositoryService

View File

@ -10,7 +10,7 @@ from monkey_island.cc.models.attack.attack_mitigations import AttackMitigations
logger = logging.getLogger(__name__)
# NOTE: This service is being replaced by the RepositoryService
# NOTE: This service is being replaced a little at a time by repositories
class Database(object):
def __init__(self):
pass

View File

@ -35,7 +35,7 @@ from monkey_island.cc.repository import (
)
from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH
from monkey_island.cc.server_utils.encryption import ILockableEncryptor, RepositoryEncryptor
from monkey_island.cc.services import AWSService, IslandModeService, RepositoryService
from monkey_island.cc.services import AWSService, IslandModeService
from monkey_island.cc.services.attack.technique_reports.T1003 import T1003, T1003GetReportData
from monkey_island.cc.services.run_local_monkey import LocalMonkeyRunService
from monkey_island.cc.services.telemetry.processing.credentials.credentials_parser import (
@ -151,7 +151,6 @@ def _register_services(container: DIContainer):
container.register_instance(LocalMonkeyRunService, container.resolve(LocalMonkeyRunService))
container.register_instance(IslandModeService, container.resolve(IslandModeService))
container.register_instance(AuthenticationService, container.resolve(AuthenticationService))
container.register_instance(RepositoryService, container.resolve(RepositoryService))
def _dirty_hacks(container: DIContainer):

View File

@ -1,42 +0,0 @@
from monkey_island.cc.repository import (
IAgentConfigurationRepository,
ICredentialsRepository,
IFileRepository,
)
from monkey_island.cc.services.database import Database
class RepositoryService:
def __init__(
self,
agent_configuration_repository: IAgentConfigurationRepository,
file_repository: IFileRepository,
credentials_repository: ICredentialsRepository,
):
self._agent_configuration_repository = agent_configuration_repository
self._file_repository = file_repository
self._credentials_repository = credentials_repository
def reset_agent_configuration(self):
# NOTE: This method will be replaced by an event when we implement pub/sub in the island.
# Different plugins and components will be able to register for the event and reset
# their configurations.
self._remove_pba_files()
self._agent_configuration_repository.reset_to_default()
def _remove_pba_files(self):
agent_configuration = self._agent_configuration_repository.get_configuration()
custom_pbas = agent_configuration.custom_pbas
if custom_pbas.linux_filename:
self._file_repository.delete_file(custom_pbas.linux_filename)
if custom_pbas.windows_filename:
self._file_repository.delete_file(custom_pbas.windows_filename)
def clear_simulation_data(self):
# NOTE: This method will be replaced by an event when we implement pub/sub in the island.
# Different plugins and components will be able to register for the event and clear
# any configuration data they've collected.
Database.reset_db(reset_config=False)
self._credentials_repository.remove_stolen_credentials()

View File

@ -1,79 +0,0 @@
from unittest.mock import MagicMock
import pytest
from tests.monkey_island import InMemoryAgentConfigurationRepository
from common.agent_configuration import AgentConfiguration
from monkey_island.cc.repository import (
IAgentConfigurationRepository,
ICredentialsRepository,
IFileRepository,
)
from monkey_island.cc.services import RepositoryService
LINUX_FILENAME = "linux_pba_file.sh"
WINDOWS_FILENAME = "windows_pba_file.ps1"
@pytest.fixture
def agent_configuration(default_agent_configuration: AgentConfiguration) -> AgentConfiguration:
custom_pbas = default_agent_configuration.custom_pbas.copy(
update={"linux_filename": LINUX_FILENAME, "windows_filename": WINDOWS_FILENAME},
)
return default_agent_configuration.copy(update={"custom_pbas": custom_pbas})
@pytest.fixture
def agent_configuration_repository(
agent_configuration: AgentConfiguration,
) -> IAgentConfigurationRepository:
agent_configuration_repository = InMemoryAgentConfigurationRepository()
agent_configuration_repository.store_configuration(agent_configuration)
return agent_configuration_repository
@pytest.fixture
def mock_file_repository() -> IFileRepository:
return MagicMock(spec=IFileRepository)
@pytest.fixture
def mock_credentials_repository() -> ICredentialsRepository:
return MagicMock(spec=ICredentialsRepository)
@pytest.fixture
def repository_service(
agent_configuration_repository, mock_file_repository, mock_credentials_repository
) -> RepositoryService:
return RepositoryService(
agent_configuration_repository, mock_file_repository, mock_credentials_repository
)
def test_reset_configuration__remove_pba_files(repository_service, mock_file_repository):
repository_service.reset_agent_configuration()
assert mock_file_repository.delete_file.called_with(LINUX_FILENAME)
assert mock_file_repository.delete_file.called_with(WINDOWS_FILENAME)
def test_reset_configuration__agent_configuration_changed(
repository_service, agent_configuration_repository, agent_configuration
):
repository_service.reset_agent_configuration()
assert agent_configuration_repository.get_configuration() != agent_configuration
@pytest.mark.usefixtures("uses_database")
def test_clear_simulation_data(
repository_service: RepositoryService,
mock_credentials_repository: ICredentialsRepository,
monkeypatch,
):
monkeypatch.setattr("monkey_island.cc.services.repository_service.Database", MagicMock())
repository_service.clear_simulation_data()
mock_credentials_repository.remove_stolen_credentials.assert_called_once()

View File

@ -299,9 +299,6 @@ event
deserialize
serialized_event
# TODO: Remove once #2277 is merged
clear_simulation_data
# pydantic base models
underscore_attrs_are_private
extra