Common: Rename from_dict() -> from_mapping()
This commit is contained in:
parent
e2f365a1f9
commit
334d2a790f
|
@ -1,7 +1,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import List
|
from typing import Any, List, Mapping
|
||||||
|
|
||||||
from marshmallow import Schema, fields
|
from marshmallow import Schema, fields
|
||||||
from marshmallow.exceptions import MarshmallowError
|
from marshmallow.exceptions import MarshmallowError
|
||||||
|
@ -45,9 +45,9 @@ class AgentConfiguration:
|
||||||
raise InvalidConfigurationError(f"{INVALID_CONFIGURATION_ERROR_MESSAGE}: {err}")
|
raise InvalidConfigurationError(f"{INVALID_CONFIGURATION_ERROR_MESSAGE}: {err}")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dict(config_dict: dict):
|
def from_mapping(config_mapping: Mapping[str, Any]) -> AgentConfiguration:
|
||||||
try:
|
try:
|
||||||
config_dict = AgentConfigurationSchema().load(config_dict)
|
config_dict = AgentConfigurationSchema().load(config_mapping)
|
||||||
return AgentConfiguration(**config_dict)
|
return AgentConfiguration(**config_dict)
|
||||||
except MarshmallowError as err:
|
except MarshmallowError as err:
|
||||||
raise InvalidConfigurationError(f"{INVALID_CONFIGURATION_ERROR_MESSAGE}: {err}")
|
raise InvalidConfigurationError(f"{INVALID_CONFIGURATION_ERROR_MESSAGE}: {err}")
|
||||||
|
|
|
@ -58,7 +58,7 @@ class ControlChannel(IControlChannel):
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
return AgentConfiguration.from_dict(json.loads(response.text)["config"])
|
return AgentConfiguration.from_mapping(json.loads(response.text)["config"])
|
||||||
except (
|
except (
|
||||||
json.JSONDecodeError,
|
json.JSONDecodeError,
|
||||||
requests.exceptions.ConnectionError,
|
requests.exceptions.ConnectionError,
|
||||||
|
|
|
@ -116,4 +116,4 @@ flat_config = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
DEFAULT_CONFIG = AgentConfiguration.from_dict(flat_config)
|
DEFAULT_CONFIG = AgentConfiguration.from_mapping(flat_config)
|
||||||
|
|
|
@ -6,7 +6,7 @@ from monkey_island.cc.repository import IAgentConfigurationRepository
|
||||||
|
|
||||||
class InMemoryAgentConfigurationRepository(IAgentConfigurationRepository):
|
class InMemoryAgentConfigurationRepository(IAgentConfigurationRepository):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._configuration = AgentConfiguration.from_dict(AGENT_CONFIGURATION)
|
self._configuration = AgentConfiguration.from_mapping(AGENT_CONFIGURATION)
|
||||||
|
|
||||||
def get_configuration(self):
|
def get_configuration(self):
|
||||||
return self._configuration
|
return self._configuration
|
||||||
|
|
|
@ -161,7 +161,7 @@ def test_propagation_configuration():
|
||||||
|
|
||||||
|
|
||||||
def test_agent_configuration():
|
def test_agent_configuration():
|
||||||
config = AgentConfiguration.from_dict(AGENT_CONFIGURATION)
|
config = AgentConfiguration.from_mapping(AGENT_CONFIGURATION)
|
||||||
config_json = AgentConfiguration.to_json(config)
|
config_json = AgentConfiguration.to_json(config)
|
||||||
|
|
||||||
assert isinstance(config, AgentConfiguration)
|
assert isinstance(config, AgentConfiguration)
|
||||||
|
@ -175,7 +175,7 @@ def test_agent_configuration():
|
||||||
|
|
||||||
|
|
||||||
def test_incorrect_type():
|
def test_incorrect_type():
|
||||||
valid_config = AgentConfiguration.from_dict(AGENT_CONFIGURATION)
|
valid_config = AgentConfiguration.from_mapping(AGENT_CONFIGURATION)
|
||||||
with pytest.raises(InvalidConfigurationError):
|
with pytest.raises(InvalidConfigurationError):
|
||||||
valid_config_dict = valid_config.__dict__
|
valid_config_dict = valid_config.__dict__
|
||||||
valid_config_dict["keep_tunnel_open_time"] = "not_a_float"
|
valid_config_dict["keep_tunnel_open_time"] = "not_a_float"
|
||||||
|
@ -192,7 +192,7 @@ def test_from_dict():
|
||||||
schema = AgentConfigurationSchema()
|
schema = AgentConfigurationSchema()
|
||||||
dict_ = json.loads(DEFAULT_AGENT_CONFIGURATION_JSON)
|
dict_ = json.loads(DEFAULT_AGENT_CONFIGURATION_JSON)
|
||||||
|
|
||||||
config = AgentConfiguration.from_dict(dict_)
|
config = AgentConfiguration.from_mapping(dict_)
|
||||||
|
|
||||||
assert schema.dump(config) == dict_
|
assert schema.dump(config) == dict_
|
||||||
|
|
||||||
|
@ -202,7 +202,7 @@ def test_from_dict__invalid_data():
|
||||||
dict_["payloads"] = "payloads"
|
dict_["payloads"] = "payloads"
|
||||||
|
|
||||||
with pytest.raises(InvalidConfigurationError):
|
with pytest.raises(InvalidConfigurationError):
|
||||||
AgentConfiguration.from_dict(dict_)
|
AgentConfiguration.from_mapping(dict_)
|
||||||
|
|
||||||
|
|
||||||
def test_from_json():
|
def test_from_json():
|
||||||
|
|
|
@ -12,7 +12,7 @@ def repository(default_agent_configuration):
|
||||||
|
|
||||||
|
|
||||||
def test_store_agent_config(repository):
|
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)
|
repository.store_configuration(agent_configuration)
|
||||||
retrieved_agent_configuration = repository.get_configuration()
|
retrieved_agent_configuration = repository.get_configuration()
|
||||||
|
|
Loading…
Reference in New Issue