Merge pull request #2291 from guardicore/2180-save-event-to-repository

2180 save event to repository
This commit is contained in:
Mike Salvatore 2022-09-14 13:40:19 -04:00 committed by GitHub
commit 0b5dd3832b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 82 additions and 2 deletions

View File

@ -0,0 +1 @@
from .save_event_to_event_repository import save_event_to_event_repository

View File

@ -0,0 +1,17 @@
import logging
from common.events import AbstractAgentEvent
from monkey_island.cc.repository import IEventRepository, StorageError
logger = logging.getLogger(__name__)
class save_event_to_event_repository:
def __init__(self, event_repository: IEventRepository):
self._event_repository = event_repository
def __call__(self, event: AbstractAgentEvent):
try:
self._event_repository.save_event(event)
except StorageError as err:
logger.error(f"Error occured storing event {event}: {err}")

View File

@ -26,3 +26,4 @@ from .mongo_credentials_repository import MongoCredentialsRepository
from .mongo_machine_repository import MongoMachineRepository
from .mongo_agent_repository import MongoAgentRepository
from .mongo_node_repository import MongoNodeRepository
from .stubbed_event_repository import StubbedEventRepository

View File

@ -0,0 +1,29 @@
from typing import Sequence, Type, TypeVar
from common.events import AbstractAgentEvent
from common.types import AgentID
from . import IEventRepository
T = TypeVar("T", bound=AbstractAgentEvent)
# TODO: Remove this class after #2180 is complete
class StubbedEventRepository(IEventRepository):
def save_event(self, event: AbstractAgentEvent):
return
def get_events(self) -> Sequence[AbstractAgentEvent]:
return []
def get_events_by_type(self, event_type: Type[T]) -> Sequence[T]:
return []
def get_events_by_tag(self, tag: str) -> Sequence[AbstractAgentEvent]:
return []
def get_events_by_source(self, source: AgentID) -> Sequence[AbstractAgentEvent]:
return []
def reset(self):
return

View File

@ -40,7 +40,11 @@ from monkey_island.cc.server_utils.island_logger import reset_logger, setup_logg
from monkey_island.cc.server_utils.network_utils import get_ip_addresses # noqa: E402
from monkey_island.cc.services.initialize import initialize_services # noqa: E402
from monkey_island.cc.setup import island_config_options_validator # noqa: E402
from monkey_island.cc.setup import PyWSGILoggingFilter, setup_island_event_handlers # noqa: E402
from monkey_island.cc.setup import ( # noqa: E402
PyWSGILoggingFilter,
setup_agent_event_handlers,
setup_island_event_handlers,
)
from monkey_island.cc.setup.data_dir import IncompatibleDataDirectory, setup_data_dir # noqa: E402
from monkey_island.cc.setup.gevent_hub_error_handler import GeventHubErrorHandler # noqa: E402
from monkey_island.cc.setup.island_config_options import IslandConfigOptions # noqa: E402
@ -63,6 +67,7 @@ def run_monkey_island():
_send_analytics(deployment, version)
container = _initialize_di_container(ip_addresses, version, config_options.data_dir)
setup_island_event_handlers(container)
setup_agent_event_handlers(container)
_setup_agent_event_serializers(container)
_initialize_mongodb_connection(config_options.start_mongodb, config_options.data_dir)

View File

@ -25,6 +25,7 @@ from monkey_island.cc.repository import (
IAgentBinaryRepository,
IAgentConfigurationRepository,
ICredentialsRepository,
IEventRepository,
IFileRepository,
ISimulationRepository,
IUserRepository,
@ -32,6 +33,7 @@ from monkey_island.cc.repository import (
LocalStorageFileRepository,
MongoCredentialsRepository,
RetrievalError,
StubbedEventRepository,
)
from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH
from monkey_island.cc.server_utils.encryption import ILockableEncryptor, RepositoryEncryptor
@ -106,6 +108,9 @@ def _register_repositories(container: DIContainer, data_dir: Path):
)
container.register_instance(IUserRepository, container.resolve(JSONFileUserRepository))
# TODO: Replace with MongoEventRepository
container.register_instance(IEventRepository, StubbedEventRepository())
def _decorate_file_repository(file_repository: IFileRepository) -> IFileRepository:
return FileRepositoryLockingDecorator(

View File

@ -1,2 +1,3 @@
from .pywsgi_logging_filter import PyWSGILoggingFilter
from .island_event_handlers import setup_island_event_handlers
from .agent_event_handlers import setup_agent_event_handlers

View File

@ -0,0 +1,16 @@
from common import DIContainer
from common.event_queue import IAgentEventQueue
from monkey_island.cc.agent_event_subscribers import save_event_to_event_repository
from monkey_island.cc.repository import IEventRepository
def setup_agent_event_handlers(container: DIContainer):
_subscribe_and_store_to_event_repository(container)
def _subscribe_and_store_to_event_repository(container: DIContainer):
agent_event_queue = container.resolve(IAgentEventQueue)
event_repository = container.resolve(IEventRepository)
save_event_subscriber = save_event_to_event_repository(event_repository)
agent_event_queue.subscribe_all_events(save_event_subscriber)

View File

@ -12,7 +12,11 @@ from infection_monkey.exploit.log4shell_utils.ldap_server import LDAPServerFacto
from monkey_island.cc.event_queue import IslandEventTopic, PyPubSubIslandEventQueue
from monkey_island.cc.models import Report
from monkey_island.cc.models.networkmap import Arc, NetworkMap
from monkey_island.cc.repository import MongoAgentRepository, MongoMachineRepository
from monkey_island.cc.repository import (
MongoAgentRepository,
MongoMachineRepository,
StubbedEventRepository,
)
from monkey_island.cc.repository.attack.IMitigationsRepository import IMitigationsRepository
from monkey_island.cc.repository.i_agent_repository import IAgentRepository
from monkey_island.cc.repository.i_attack_repository import IAttackRepository
@ -279,6 +283,7 @@ IEventRepository.get_events
IFindingRepository.get_findings
MongoAgentRepository
MongoMachineRepository
StubbedEventRepository
key_list
simulation
netmap