From fc9d854c72d7ba64fe721698912e80f02d3e9621 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 24 Jun 2022 12:56:34 -0400 Subject: [PATCH] Common: Add validation to AgentConfiguration construction --- monkey/common/configuration/agent_configuration.py | 5 +++++ .../common/configuration/test_agent_configuration.py | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/monkey/common/configuration/agent_configuration.py b/monkey/common/configuration/agent_configuration.py index 8c5b88646..71c4bfd49 100644 --- a/monkey/common/configuration/agent_configuration.py +++ b/monkey/common/configuration/agent_configuration.py @@ -26,6 +26,11 @@ class AgentConfiguration: payloads: List[PluginConfiguration] propagation: PropagationConfiguration + def __post_init__(self): + # This will raise an exception if the object is invalid. Calling this in __post__init() + # makes it impossible to construct an invalid object + AgentConfigurationSchema().dump(self) + @staticmethod def from_dict(dict_: dict): config_dict = AgentConfigurationSchema().load(dict_) 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 0de34e4a2..d4a285364 100644 --- a/monkey/tests/unit_tests/common/configuration/test_agent_configuration.py +++ b/monkey/tests/unit_tests/common/configuration/test_agent_configuration.py @@ -1,5 +1,6 @@ import json +import pytest from tests.common.example_agent_configuration import ( AGENT_CONFIGURATION, BLOCKED_IPS, @@ -169,6 +170,14 @@ def test_agent_configuration(): assert json.loads(config_json) == AGENT_CONFIGURATION +def test_incorrect_type(): + valid_config = AgentConfiguration.from_dict(AGENT_CONFIGURATION) + with pytest.raises(Exception): + valid_config_dict = valid_config.__dict__ + valid_config_dict["keep_tunnel_open_time"] = "not_a_float" + AgentConfiguration(**valid_config_dict) + + def test_default_agent_configuration(): config = AgentConfiguration.from_json(DEFAULT_AGENT_CONFIGURATION_JSON)