Common: Add validation to AgentConfiguration construction

This commit is contained in:
Mike Salvatore 2022-06-24 12:56:34 -04:00
parent ea02bec0b4
commit fc9d854c72
2 changed files with 14 additions and 0 deletions

View File

@ -26,6 +26,11 @@ class AgentConfiguration:
payloads: List[PluginConfiguration] payloads: List[PluginConfiguration]
propagation: PropagationConfiguration 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 @staticmethod
def from_dict(dict_: dict): def from_dict(dict_: dict):
config_dict = AgentConfigurationSchema().load(dict_) config_dict = AgentConfigurationSchema().load(dict_)

View File

@ -1,5 +1,6 @@
import json import json
import pytest
from tests.common.example_agent_configuration import ( from tests.common.example_agent_configuration import (
AGENT_CONFIGURATION, AGENT_CONFIGURATION,
BLOCKED_IPS, BLOCKED_IPS,
@ -169,6 +170,14 @@ def test_agent_configuration():
assert json.loads(config_json) == 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(): def test_default_agent_configuration():
config = AgentConfiguration.from_json(DEFAULT_AGENT_CONFIGURATION_JSON) config = AgentConfiguration.from_json(DEFAULT_AGENT_CONFIGURATION_JSON)