From bc0c4a1c8e697b1e27dee331fd72a4302f87b633 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Sat, 2 Jul 2022 09:49:12 -0400 Subject: [PATCH] Island: Add FileRepositoryLoggingDecorator --- .../monkey_island/cc/repository/__init__.py | 1 + .../file_repository_logging_decorator.py | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 monkey/monkey_island/cc/repository/file_repository_logging_decorator.py diff --git a/monkey/monkey_island/cc/repository/__init__.py b/monkey/monkey_island/cc/repository/__init__.py index 153d57e27..3b18883c2 100644 --- a/monkey/monkey_island/cc/repository/__init__.py +++ b/monkey/monkey_island/cc/repository/__init__.py @@ -1,6 +1,7 @@ from .errors import RemovalError, RetrievalError, StorageError from .i_file_repository import FileNotFoundError, IFileRepository from .local_storage_file_repository import LocalStorageFileRepository +from .file_repository_logging_decorator import FileRepositoryLoggingDecorator from .i_agent_binary_repository import IAgentBinaryRepository from .agent_binary_repository import AgentBinaryRepository from .i_agent_configuration_repository import IAgentConfigurationRepository diff --git a/monkey/monkey_island/cc/repository/file_repository_logging_decorator.py b/monkey/monkey_island/cc/repository/file_repository_logging_decorator.py new file mode 100644 index 000000000..47ba16ca1 --- /dev/null +++ b/monkey/monkey_island/cc/repository/file_repository_logging_decorator.py @@ -0,0 +1,31 @@ +import logging +from typing import BinaryIO + +from . import IFileRepository + +logger = logging.getLogger(__name__) + + +class FileRepositoryLoggingDecorator(IFileRepository): + def __init__(self, file_repository: IFileRepository): + self._file_repository = file_repository + + def save_file(self, unsafe_file_name: str, file_contents: BinaryIO): + logger.info(f"Saving file {unsafe_file_name}") + + return self._file_repository.save_file(unsafe_file_name, file_contents) + + def open_file(self, unsafe_file_name: str) -> BinaryIO: + logger.info(f"Opening file {unsafe_file_name}") + + return self._file_repository.open_file(unsafe_file_name) + + def delete_file(self, unsafe_file_name: str): + logger.info(f"Deleting file {unsafe_file_name}") + + return self._file_repository.delete_file(unsafe_file_name) + + def delete_all_files(self): + logger.info("Deleting all files in the repository") + + return self._file_repository.delete_all_files()