Skip to content

Commit

Permalink
Merge pull request #2043 from guardicore/1960-deserialize-config
Browse files Browse the repository at this point in the history
1960 deserialize config
  • Loading branch information
mssalvatore authored Jun 27, 2022
2 parents d079d74 + e6d3854 commit 44a6197
Show file tree
Hide file tree
Showing 35 changed files with 513 additions and 435 deletions.
18 changes: 13 additions & 5 deletions monkey/common/configuration/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from .agent_configuration import (
AgentConfiguration,
AgentConfigurationSchema,
from .agent_configuration import AgentConfiguration, InvalidConfigurationError
from .agent_sub_configurations import (
CustomPBAConfiguration,
PluginConfiguration,
ScanTargetConfiguration,
ICMPScanConfiguration,
TCPScanConfiguration,
NetworkScanConfiguration,
ExploitationOptionsConfiguration,
ExploiterConfiguration,
ExploitationConfiguration,
PropagationConfiguration,
)
from .default_agent_configuration import (
DEFAULT_AGENT_CONFIGURATION_JSON,
build_default_agent_configuration,
DEFAULT_AGENT_CONFIGURATION,
)
71 changes: 65 additions & 6 deletions monkey/common/configuration/agent_configuration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import List
from typing import Any, List, Mapping

from marshmallow import Schema, fields, post_load
from marshmallow import Schema, fields
from marshmallow.exceptions import MarshmallowError

from .agent_sub_configuration_schemas import (
CustomPBAConfigurationSchema,
Expand All @@ -15,6 +18,15 @@
)


class InvalidConfigurationError(Exception):
pass


INVALID_CONFIGURATION_ERROR_MESSAGE = (
"Cannot construct an AgentConfiguration object with the supplied, invalid data:"
)


@dataclass(frozen=True)
class AgentConfiguration:
keep_tunnel_open_time: float
Expand All @@ -24,6 +36,57 @@ 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
try:
AgentConfigurationSchema().dump(self)
except Exception as err:
raise InvalidConfigurationError(f"{INVALID_CONFIGURATION_ERROR_MESSAGE}: {err}")

@staticmethod
def from_mapping(config_mapping: Mapping[str, Any]) -> AgentConfiguration:
"""
Construct an AgentConfiguration from a Mapping
:param config_mapping: A Mapping that represents an AgentConfiguration
:return: An AgentConfiguration
:raises: InvalidConfigurationError if the provided Mapping does not represent a valid
AgentConfiguration
"""

try:
config_dict = AgentConfigurationSchema().load(config_mapping)
return AgentConfiguration(**config_dict)
except MarshmallowError as err:
raise InvalidConfigurationError(f"{INVALID_CONFIGURATION_ERROR_MESSAGE}: {err}")

@staticmethod
def from_json(config_json: str) -> AgentConfiguration:
"""
Construct an AgentConfiguration from a JSON string
:param config_json: A JSON string that represents an AgentConfiguration
:return: An AgentConfiguration
:raises: InvalidConfigurationError if the provided JSON does not represent a valid
AgentConfiguration
"""
try:
config_dict = AgentConfigurationSchema().loads(config_json)
return AgentConfiguration(**config_dict)
except MarshmallowError as err:
raise InvalidConfigurationError(f"{INVALID_CONFIGURATION_ERROR_MESSAGE}: {err}")

@staticmethod
def to_json(config: AgentConfiguration) -> str:
"""
Serialize an AgentConfiguration to JSON
:param config: An AgentConfiguration
:return: A JSON string representing the AgentConfiguration
"""
return AgentConfigurationSchema().dumps(config)


class AgentConfigurationSchema(Schema):
keep_tunnel_open_time = fields.Float()
Expand All @@ -32,7 +95,3 @@ class AgentConfigurationSchema(Schema):
credential_collectors = fields.List(fields.Nested(PluginConfigurationSchema))
payloads = fields.List(fields.Nested(PluginConfigurationSchema))
propagation = fields.Nested(PropagationConfigurationSchema)

@post_load
def _make_agent_configuration(self, data, **kwargs):
return AgentConfiguration(**data)
Loading

0 comments on commit 44a6197

Please sign in to comment.