From 334d2a790f5fe9e3475005d1c8213d6b9afc7bec Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 24 Jun 2022 13:24:02 -0400 Subject: [PATCH] Common: Rename from_dict() -> from_mapping() --- monkey/common/configuration/agent_configuration.py | 6 +++--- monkey/infection_monkey/master/control_channel.py | 2 +- .../tests/data_for_tests/monkey_configs/default_config.py | 2 +- .../in_memory_agent_configuration_repository.py | 2 +- .../common/configuration/test_agent_configuration.py | 8 ++++---- .../test_file_agent_configuration_repository.py | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/monkey/common/configuration/agent_configuration.py b/monkey/common/configuration/agent_configuration.py index e3d039ecc..72b26d2f0 100644 --- a/monkey/common/configuration/agent_configuration.py +++ b/monkey/common/configuration/agent_configuration.py @@ -1,7 +1,7 @@ from __future__ import annotations from dataclasses import dataclass -from typing import List +from typing import Any, List, Mapping from marshmallow import Schema, fields from marshmallow.exceptions import MarshmallowError @@ -45,9 +45,9 @@ class AgentConfiguration: raise InvalidConfigurationError(f"{INVALID_CONFIGURATION_ERROR_MESSAGE}: {err}") @staticmethod - def from_dict(config_dict: dict): + def from_mapping(config_mapping: Mapping[str, Any]) -> AgentConfiguration: try: - config_dict = AgentConfigurationSchema().load(config_dict) + config_dict = AgentConfigurationSchema().load(config_mapping) return AgentConfiguration(**config_dict) except MarshmallowError as err: raise InvalidConfigurationError(f"{INVALID_CONFIGURATION_ERROR_MESSAGE}: {err}") diff --git a/monkey/infection_monkey/master/control_channel.py b/monkey/infection_monkey/master/control_channel.py index 8567a301f..c83942f5d 100644 --- a/monkey/infection_monkey/master/control_channel.py +++ b/monkey/infection_monkey/master/control_channel.py @@ -58,7 +58,7 @@ class ControlChannel(IControlChannel): ) response.raise_for_status() - return AgentConfiguration.from_dict(json.loads(response.text)["config"]) + return AgentConfiguration.from_mapping(json.loads(response.text)["config"]) except ( json.JSONDecodeError, requests.exceptions.ConnectionError, diff --git a/monkey/tests/data_for_tests/monkey_configs/default_config.py b/monkey/tests/data_for_tests/monkey_configs/default_config.py index 6c0771af0..c0eca7c43 100644 --- a/monkey/tests/data_for_tests/monkey_configs/default_config.py +++ b/monkey/tests/data_for_tests/monkey_configs/default_config.py @@ -116,4 +116,4 @@ flat_config = { }, } -DEFAULT_CONFIG = AgentConfiguration.from_dict(flat_config) +DEFAULT_CONFIG = AgentConfiguration.from_mapping(flat_config) diff --git a/monkey/tests/monkey_island/in_memory_agent_configuration_repository.py b/monkey/tests/monkey_island/in_memory_agent_configuration_repository.py index b18465568..e9bcbae62 100644 --- a/monkey/tests/monkey_island/in_memory_agent_configuration_repository.py +++ b/monkey/tests/monkey_island/in_memory_agent_configuration_repository.py @@ -6,7 +6,7 @@ from monkey_island.cc.repository import IAgentConfigurationRepository class InMemoryAgentConfigurationRepository(IAgentConfigurationRepository): def __init__(self): - self._configuration = AgentConfiguration.from_dict(AGENT_CONFIGURATION) + self._configuration = AgentConfiguration.from_mapping(AGENT_CONFIGURATION) def get_configuration(self): return self._configuration diff --git a/monkey/tests/unit_tests/common/configuration/test_agent_configuration.py b/monkey/tests/unit_tests/common/configuration/test_agent_configuration.py index f8c8f4c38..4b264c8cb 100644 --- a/monkey/tests/unit_tests/common/configuration/test_agent_configuration.py +++ b/monkey/tests/unit_tests/common/configuration/test_agent_configuration.py @@ -161,7 +161,7 @@ def test_propagation_configuration(): def test_agent_configuration(): - config = AgentConfiguration.from_dict(AGENT_CONFIGURATION) + config = AgentConfiguration.from_mapping(AGENT_CONFIGURATION) config_json = AgentConfiguration.to_json(config) assert isinstance(config, AgentConfiguration) @@ -175,7 +175,7 @@ def test_agent_configuration(): def test_incorrect_type(): - valid_config = AgentConfiguration.from_dict(AGENT_CONFIGURATION) + valid_config = AgentConfiguration.from_mapping(AGENT_CONFIGURATION) with pytest.raises(InvalidConfigurationError): valid_config_dict = valid_config.__dict__ valid_config_dict["keep_tunnel_open_time"] = "not_a_float" @@ -192,7 +192,7 @@ def test_from_dict(): schema = AgentConfigurationSchema() dict_ = json.loads(DEFAULT_AGENT_CONFIGURATION_JSON) - config = AgentConfiguration.from_dict(dict_) + config = AgentConfiguration.from_mapping(dict_) assert schema.dump(config) == dict_ @@ -202,7 +202,7 @@ def test_from_dict__invalid_data(): dict_["payloads"] = "payloads" with pytest.raises(InvalidConfigurationError): - AgentConfiguration.from_dict(dict_) + AgentConfiguration.from_mapping(dict_) def test_from_json(): diff --git a/monkey/tests/unit_tests/monkey_island/cc/repository/test_file_agent_configuration_repository.py b/monkey/tests/unit_tests/monkey_island/cc/repository/test_file_agent_configuration_repository.py index 21dc4503b..fb7863dc3 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repository/test_file_agent_configuration_repository.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repository/test_file_agent_configuration_repository.py @@ -12,7 +12,7 @@ def repository(default_agent_configuration): def test_store_agent_config(repository): - agent_configuration = AgentConfiguration.from_dict(AGENT_CONFIGURATION) + agent_configuration = AgentConfiguration.from_mapping(AGENT_CONFIGURATION) repository.store_configuration(agent_configuration) retrieved_agent_configuration = repository.get_configuration()