Merge pull request #2052 from guardicore/2036-reset-repository

2036 reset repository
This commit is contained in:
Mike Salvatore 2022-06-30 12:20:44 -04:00 committed by GitHub
commit bcb97ce35d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 1 deletions

View File

@ -35,3 +35,6 @@ class FileAgentConfigurationRepository(IAgentConfigurationRepository):
self._file_repository.save_file( self._file_repository.save_file(
AGENT_CONFIGURATION_FILE_NAME, io.BytesIO(configuration_json.encode()) AGENT_CONFIGURATION_FILE_NAME, io.BytesIO(configuration_json.encode())
) )
def reset_to_default(self):
self.store_configuration(self._default_agent_configuration)

View File

@ -28,3 +28,12 @@ class IAgentConfigurationRepository(ABC):
:raises StorageError: If the configuration could not be stored :raises StorageError: If the configuration could not be stored
""" """
pass pass
@abstractmethod
def reset_to_default(self):
"""
Remove any stored configuration from the repository
:raises RemovalError: If the repository could not be reset
"""
pass

View File

@ -6,10 +6,14 @@ from monkey_island.cc.repository import IAgentConfigurationRepository
class InMemoryAgentConfigurationRepository(IAgentConfigurationRepository): class InMemoryAgentConfigurationRepository(IAgentConfigurationRepository):
def __init__(self): def __init__(self):
self._configuration = AgentConfiguration.from_mapping(AGENT_CONFIGURATION) self._default_configuration = AgentConfiguration.from_mapping(AGENT_CONFIGURATION)
self._configuration = self._default_configuration
def get_configuration(self): def get_configuration(self):
return self._configuration return self._configuration
def store_configuration(self, agent_configuration): def store_configuration(self, agent_configuration):
self._configuration = agent_configuration self._configuration = agent_configuration
def reset_to_default(self):
self._configuration = self._default_configuration

View File

@ -33,3 +33,13 @@ def test_get_agent_config_retrieval_error(default_agent_configuration):
with pytest.raises(RetrievalError): with pytest.raises(RetrievalError):
repository.get_configuration() repository.get_configuration()
def test_reset_to_default(repository, default_agent_configuration):
agent_configuration = AgentConfiguration.from_mapping(AGENT_CONFIGURATION)
repository.store_configuration(agent_configuration)
repository.reset_to_default()
retrieved_agent_configuration = repository.get_configuration()
assert retrieved_agent_configuration == default_agent_configuration