Island: Use {from,to}_json() in FileAgentConfigurationRepository

This commit is contained in:
Mike Salvatore 2022-06-24 12:24:07 -04:00
parent e4eee6a5eb
commit 6a927266a4
1 changed files with 3 additions and 4 deletions

View File

@ -1,6 +1,6 @@
import io
from common.configuration import AgentConfiguration, AgentConfigurationSchema
from common.configuration import AgentConfiguration
from monkey_island.cc import repository
from monkey_island.cc.repository import (
IAgentConfigurationRepository,
@ -17,21 +17,20 @@ class FileAgentConfigurationRepository(IAgentConfigurationRepository):
):
self._default_agent_configuration = default_agent_configuration
self._file_repository = file_repository
self._schema = AgentConfigurationSchema()
def get_configuration(self) -> AgentConfiguration:
try:
with self._file_repository.open_file(AGENT_CONFIGURATION_FILE_NAME) as f:
configuration_json = f.read().decode()
return self._schema.loads(configuration_json)
return AgentConfiguration.from_json(configuration_json)
except repository.FileNotFoundError:
return self._default_agent_configuration
except Exception as err:
raise RetrievalError(f"Error retrieving the agent configuration: {err}")
def store_configuration(self, agent_configuration: AgentConfiguration):
configuration_json = self._schema.dumps(agent_configuration)
configuration_json = AgentConfiguration.to_json(agent_configuration)
self._file_repository.save_file(
AGENT_CONFIGURATION_FILE_NAME, io.BytesIO(configuration_json.encode())