From 1bd0e39148afbb6a0c6f7779acf399039a94b4ff Mon Sep 17 00:00:00 2001 From: Bhavin Patel Date: Wed, 14 Jun 2023 14:34:12 -0700 Subject: [PATCH 1/2] updated code to building filter macros --- .../adapter/obj_to_json_adapter.py | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/bin/contentctl_project/contentctl_infrastructure/adapter/obj_to_json_adapter.py b/bin/contentctl_project/contentctl_infrastructure/adapter/obj_to_json_adapter.py index 0e6dce1880..cb9dd06ddb 100644 --- a/bin/contentctl_project/contentctl_infrastructure/adapter/obj_to_json_adapter.py +++ b/bin/contentctl_project/contentctl_infrastructure/adapter/obj_to_json_adapter.py @@ -1,4 +1,5 @@ import os +import json from bin.contentctl_project.contentctl_core.application.adapter.adapter import Adapter @@ -29,6 +30,30 @@ def writeObjects(self, objects: list, output_path: str, type: SecurityContentTyp )) JsonWriter.writeJsonObject(os.path.join(output_path, 'detections.json'), {'detections': obj_array }) + + ### Code to be added to contentctl to ship filter macros to macros.json + + array_of_objects = [] + + for detection in objects: + detection_dict = detection.dict() + if "macros" in detection_dict: + for macro in detection_dict["macros"]: + array_of_objects.append(macro) + + uniques:set[str] = set() + for obj in array_of_objects: + if obj.get("arguments",None) != None: + uniques.add(json.dumps(obj,sort_keys=True)) + else: + obj.pop("arguments") + uniques.add(json.dumps(obj, sort_keys=True)) + output = [] + for item in uniques: + output.append(json.loads(item)) + + JsonWriter.writeJsonObject(os.path.join(output_path, 'macros.json'), {'macros': output}) + elif type == SecurityContentType.stories: obj_array = [] @@ -64,16 +89,20 @@ def writeObjects(self, objects: list, output_path: str, type: SecurityContentTyp elif type == SecurityContentType.lookups: obj_array = [] for lookup in objects: + obj_array.append(lookup.dict(exclude_none=True)) + JsonWriter.writeJsonObject(os.path.join(output_path, 'lookups.json'), {'lookups': obj_array }) - elif type == SecurityContentType.macros: - obj_array = [] - for macro in objects: - obj_array.append(macro.dict(exclude_none=True)) + # elif type == SecurityContentType.macros: + # obj_array = [] + # for macro in objects: + # obj_array.append(macro.dict(exclude_none=True)) - JsonWriter.writeJsonObject(os.path.join(output_path, 'macros.json'), {'macros': obj_array }) + # JsonWriter.writeJsonObject(os.path.join(output_path, 'macros.json'), {'macros222': obj_array }) + + elif type == SecurityContentType.deployments: obj_array = [] From e66f6bcf44dc91d206f1fed96ebdd5692794685e Mon Sep 17 00:00:00 2001 From: Bhavin Patel Date: Thu, 22 Jun 2023 13:37:02 -0700 Subject: [PATCH 2/2] variable consistency --- .../adapter/obj_to_json_adapter.py | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/bin/contentctl_project/contentctl_infrastructure/adapter/obj_to_json_adapter.py b/bin/contentctl_project/contentctl_infrastructure/adapter/obj_to_json_adapter.py index cb9dd06ddb..72b4b36ee1 100644 --- a/bin/contentctl_project/contentctl_infrastructure/adapter/obj_to_json_adapter.py +++ b/bin/contentctl_project/contentctl_infrastructure/adapter/obj_to_json_adapter.py @@ -33,26 +33,26 @@ def writeObjects(self, objects: list, output_path: str, type: SecurityContentTyp ### Code to be added to contentctl to ship filter macros to macros.json - array_of_objects = [] - + obj_array = [] for detection in objects: detection_dict = detection.dict() if "macros" in detection_dict: for macro in detection_dict["macros"]: - array_of_objects.append(macro) + obj_array.append(macro) uniques:set[str] = set() - for obj in array_of_objects: + for obj in obj_array: if obj.get("arguments",None) != None: uniques.add(json.dumps(obj,sort_keys=True)) else: obj.pop("arguments") uniques.add(json.dumps(obj, sort_keys=True)) - output = [] + + obj_array = [] for item in uniques: - output.append(json.loads(item)) + obj_array.append(json.loads(item)) - JsonWriter.writeJsonObject(os.path.join(output_path, 'macros.json'), {'macros': output}) + JsonWriter.writeJsonObject(os.path.join(output_path, 'macros.json'), {'macros': obj_array}) elif type == SecurityContentType.stories: @@ -95,14 +95,6 @@ def writeObjects(self, objects: list, output_path: str, type: SecurityContentTyp JsonWriter.writeJsonObject(os.path.join(output_path, 'lookups.json'), {'lookups': obj_array }) - # elif type == SecurityContentType.macros: - # obj_array = [] - # for macro in objects: - # obj_array.append(macro.dict(exclude_none=True)) - - # JsonWriter.writeJsonObject(os.path.join(output_path, 'macros.json'), {'macros222': obj_array }) - - elif type == SecurityContentType.deployments: obj_array = []