From 717801e9ad49e4440557746a077abad50df7b6d6 Mon Sep 17 00:00:00 2001 From: vakaris_zilius Date: Thu, 23 Jun 2022 11:44:13 +0000 Subject: [PATCH] Agent: Add telemetry json encoder Telemetry json encoder is needed to encode OperatingSystems enums when sending back telemetries --- monkey/infection_monkey/telemetry/base_telem.py | 3 ++- monkey/infection_monkey/telemetry/telem_encoder.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 monkey/infection_monkey/telemetry/telem_encoder.py diff --git a/monkey/infection_monkey/telemetry/base_telem.py b/monkey/infection_monkey/telemetry/base_telem.py index 18e8d2158..32b5110f2 100644 --- a/monkey/infection_monkey/telemetry/base_telem.py +++ b/monkey/infection_monkey/telemetry/base_telem.py @@ -4,6 +4,7 @@ import logging from infection_monkey.control import ControlClient from infection_monkey.telemetry.i_telem import ITelem +from infection_monkey.telemetry.telem_encoder import TelemetryJSONEncoder logger = logging.getLogger(__name__) LOGGED_DATA_LENGTH = 300 # How many characters of telemetry data will be logged @@ -39,7 +40,7 @@ class BaseTelem(ITelem, metaclass=abc.ABCMeta): @property def json_encoder(self): - return json.JSONEncoder + return TelemetryJSONEncoder def _log_telem_sending(self, serialized_data: str, log_data=True): logger.debug(f"Sending {self.telem_category} telemetry.") diff --git a/monkey/infection_monkey/telemetry/telem_encoder.py b/monkey/infection_monkey/telemetry/telem_encoder.py new file mode 100644 index 000000000..019569107 --- /dev/null +++ b/monkey/infection_monkey/telemetry/telem_encoder.py @@ -0,0 +1,10 @@ +import json + +from common import OperatingSystems + + +class TelemetryJSONEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, OperatingSystems): + return obj.name + return json.JSONEncoder.default(self, obj)