@@ -2402,62 +2402,6 @@ def export_selected(
24022402
24032403 return bundle
24042404
2405- def prepare_bundle_ids (self , bundle , use_json = True , keep_original_id = False ):
2406- if use_json :
2407- try :
2408- bundle_data = json .loads (bundle )
2409- except :
2410- raise Exception ("File data is not a valid JSON" )
2411- else :
2412- bundle_data = bundle
2413- cache_ids = {}
2414- # First iteration to cache all entity ids
2415- stix_helpers = self .get_stix_helper ()
2416- for item in bundle_data ["objects" ]:
2417- helper = stix_helpers .get (item ["type" ])
2418- if hasattr (helper , "generate_id_from_data" ):
2419- standard_id = helper .generate_id_from_data (item )
2420- if item ["id" ] != standard_id :
2421- cache_ids [item ["id" ]] = standard_id
2422-
2423- # If no id to rewrite, return the processed bundle
2424- if not bool (cache_ids ):
2425- # As rewrite can have multiple roundtrip
2426- # we kept the first original id in a meta attribute
2427- # final behavior is to rewrite this in the x_opencti_stix_ids
2428- for item in bundle_data ["objects" ]:
2429- if "x_keep_original_id" in item :
2430- item ["x_opencti_stix_ids" ] = item .get ("x_opencti_stix_ids" , []) + [
2431- item ["x_keep_original_id" ]
2432- ]
2433- del item ["x_keep_original_id" ]
2434- return json .dumps (bundle_data ) if use_json else bundle_data
2435-
2436- # Second iteration to replace and remap
2437- for item in bundle_data ["objects" ]:
2438- # For entities, try to replace the main id
2439- # Keep the current one if needed
2440- if cache_ids .get (item ["id" ]):
2441- original_id = item ["id" ]
2442- item ["id" ] = cache_ids [original_id ]
2443- if keep_original_id and "x_keep_original_id" not in item :
2444- item ["x_keep_original_id" ] = original_id
2445- # For all elements, replace all refs (source_ref, object_refs, ...)
2446- ref_keys = list (
2447- filter (lambda i : i .endswith ("_ref" ) or i .endswith ("_refs" ), item .keys ())
2448- )
2449- for ref_key in ref_keys :
2450- if ref_key .endswith ("_refs" ):
2451- item [ref_key ] = list (
2452- map (lambda id_ref : cache_ids .get (id_ref , id_ref ), item [ref_key ])
2453- )
2454- else :
2455- item [ref_key ] = cache_ids .get (item [ref_key ], item [ref_key ])
2456-
2457- return self .prepare_bundle_ids (
2458- bundle_data , False , keep_original_id = keep_original_id
2459- )
2460-
24612405 def import_item (
24622406 self ,
24632407 item ,
@@ -2708,11 +2652,6 @@ def import_bundle(
27082652 else None
27092653 )
27102654
2711- # Bundle ids must be rewritten
2712- stix_bundle = self .prepare_bundle_ids (
2713- bundle = stix_bundle , use_json = False , keep_original_id = keep_original_id
2714- )
2715-
27162655 stix2_splitter = OpenCTIStix2Splitter ()
27172656 _ , bundles = stix2_splitter .split_bundle_with_expectations (
27182657 stix_bundle , False , event_version
0 commit comments