From 8b8459ce5322a205454d87f0cf95ff042c9eb53a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Tue, 28 Aug 2018 17:30:07 -0400 Subject: [PATCH] fix: normalizing the outputs --- pymisp/abstract.py | 11 ++++++++++- pymisp/mispevent.py | 5 +---- tests/mispevent_testfiles/def_param.json | 6 +++--- tests/mispevent_testfiles/event_obj_attr_tag.json | 12 ++++++------ tests/mispevent_testfiles/event_obj_def_param.json | 12 ++++++------ tests/mispevent_testfiles/misp_custom_obj.json | 6 +++--- tests/mispevent_testfiles/sighting.json | 2 +- .../test_object_template/definition.json | 2 +- 8 files changed, 31 insertions(+), 25 deletions(-) diff --git a/pymisp/abstract.py b/pymisp/abstract.py index e1af10a..3fe4085 100644 --- a/pymisp/abstract.py +++ b/pymisp/abstract.py @@ -57,6 +57,14 @@ class Analysis(Enum): completed = 2 +def _int_to_str(d): + # transform all integer back to string + for k, v in d.items(): + if isinstance(v, (int, float)) and not isinstance(v, bool): + d[k] = str(v) + return d + + class MISPEncode(JSONEncoder): def default(self, obj): @@ -151,6 +159,7 @@ class AbstractMISP(collections.MutableMapping): else: val = self._datetime_to_timestamp(val) to_return[attribute] = val + to_return = _int_to_str(to_return) return to_return def jsonable(self): @@ -214,7 +223,7 @@ class AbstractMISP(collections.MutableMapping): """Convert a datetime.datetime object to a timestamp (int)""" if isinstance(d, (int, str)) or (sys.version_info < (3, 0) and isinstance(d, unicode)): # Assume we already have a timestamp - return d + return int(d) if sys.version_info >= (3, 3): return int(d.timestamp()) else: diff --git a/pymisp/mispevent.py b/pymisp/mispevent.py index a8e04ed..30d10bd 100644 --- a/pymisp/mispevent.py +++ b/pymisp/mispevent.py @@ -247,7 +247,6 @@ class MISPAttribute(AbstractMISP): to_return = super(MISPAttribute, self).to_dict() if to_return.get('data'): to_return['data'] = base64.b64encode(self.data.getvalue()).decode() - to_return = _int_to_str(to_return) return to_return def _prepare_new_malware_sample(self): @@ -573,9 +572,7 @@ class MISPEvent(AbstractMISP): if to_return.get('publish_timestamp'): to_return['publish_timestamp'] = self._datetime_to_timestamp(self.publish_timestamp) - to_return = _int_to_str(to_return) - to_return = {'Event': to_return} - return to_return + return {'Event': _int_to_str(to_return)} def add_proposal(self, shadow_attribute=None, **kwargs): """Alias for add_shadow_attribute""" diff --git a/tests/mispevent_testfiles/def_param.json b/tests/mispevent_testfiles/def_param.json index 46ccca3..9658189 100644 --- a/tests/mispevent_testfiles/def_param.json +++ b/tests/mispevent_testfiles/def_param.json @@ -37,12 +37,12 @@ } ], "description": "Whois records information for a domain name or an IP address.", - "distribution": 5, + "distribution": "5", "meta-category": "network", "name": "whois", - "sharing_group_id": 0, + "sharing_group_id": "0", "template_uuid": "429faea1-34ff-47af-8a00-7c62d3be5a6a", - "template_version": 10, + "template_version": "10", "uuid": "a" } ], diff --git a/tests/mispevent_testfiles/event_obj_attr_tag.json b/tests/mispevent_testfiles/event_obj_attr_tag.json index a258993..fd259fc 100644 --- a/tests/mispevent_testfiles/event_obj_attr_tag.json +++ b/tests/mispevent_testfiles/event_obj_attr_tag.json @@ -26,12 +26,12 @@ } ], "description": "File object describing a file with meta-information", - "distribution": 5, + "distribution": "5", "meta-category": "file", "name": "file", - "sharing_group_id": 0, + "sharing_group_id": "0", "template_uuid": "688c46fb-5edb-40a3-8273-1af7923e2215", - "template_version": 13, + "template_version": "13", "uuid": "a" }, { @@ -46,12 +46,12 @@ } ], "description": "url object describes an url along with its normalized field (like extracted using faup parsing library) and its metadata.", - "distribution": 5, + "distribution": "5", "meta-category": "network", "name": "url", - "sharing_group_id": 0, + "sharing_group_id": "0", "template_uuid": "60efb77b-40b5-4c46-871b-ed1ed999fce5", - "template_version": 6, + "template_version": "6", "uuid": "b" } ] diff --git a/tests/mispevent_testfiles/event_obj_def_param.json b/tests/mispevent_testfiles/event_obj_def_param.json index 720a1f9..dc8667a 100644 --- a/tests/mispevent_testfiles/event_obj_def_param.json +++ b/tests/mispevent_testfiles/event_obj_def_param.json @@ -18,12 +18,12 @@ } ], "description": "File object describing a file with meta-information", - "distribution": 5, + "distribution": "5", "meta-category": "file", "name": "file", - "sharing_group_id": 0, + "sharing_group_id": "0", "template_uuid": "688c46fb-5edb-40a3-8273-1af7923e2215", - "template_version": 13, + "template_version": "13", "uuid": "a" }, { @@ -43,12 +43,12 @@ } ], "description": "File object describing a file with meta-information", - "distribution": 5, + "distribution": "5", "meta-category": "file", "name": "file", - "sharing_group_id": 0, + "sharing_group_id": "0", "template_uuid": "688c46fb-5edb-40a3-8273-1af7923e2215", - "template_version": 13, + "template_version": "13", "uuid": "b" } ] diff --git a/tests/mispevent_testfiles/misp_custom_obj.json b/tests/mispevent_testfiles/misp_custom_obj.json index 024fd82..043957d 100644 --- a/tests/mispevent_testfiles/misp_custom_obj.json +++ b/tests/mispevent_testfiles/misp_custom_obj.json @@ -21,13 +21,13 @@ } ], "description": "TestTemplate.", - "distribution": 5, + "distribution": "5", "meta-category": "file", "misp_objects_path_custom": "tests/mispevent_testfiles", "name": "test_object_template", - "sharing_group_id": 0, + "sharing_group_id": "0", "template_uuid": "4ec55cc6-9e49-4c64-b794-03c25c1a6589", - "template_version": 1, + "template_version": "1", "uuid": "a" } ], diff --git a/tests/mispevent_testfiles/sighting.json b/tests/mispevent_testfiles/sighting.json index 1d7c043..06c1b48 100644 --- a/tests/mispevent_testfiles/sighting.json +++ b/tests/mispevent_testfiles/sighting.json @@ -1,5 +1,5 @@ { - "timestamp": 11111111, + "timestamp": "11111111", "type": "bar", "value": "1" } diff --git a/tests/mispevent_testfiles/test_object_template/definition.json b/tests/mispevent_testfiles/test_object_template/definition.json index 283b9de..2aeb307 100644 --- a/tests/mispevent_testfiles/test_object_template/definition.json +++ b/tests/mispevent_testfiles/test_object_template/definition.json @@ -21,7 +21,7 @@ "misp-attribute": "text" } }, - "version": 1, + "version": "1", "description": "TestTemplate.", "meta-category": "file", "uuid": "4ec55cc6-9e49-4c64-b794-03c25c1a6589",