Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 3928659

Browse files
[client] handle file update in patch operation (opencti #9300)
1 parent 8203d2f commit 3928659

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

pycti/utils/opencti_stix2.py

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,20 +2407,56 @@ def export_selected(
24072407

24082408
return bundle
24092409

2410-
def apply_patch(self, item):
2411-
input = item["opencti_field_patch"]
2412-
if item["type"] == "relationship":
2413-
self.opencti.stix_core_relationship.update_field(id=item["id"], input=input)
2414-
elif item["type"] == "sighting":
2415-
self.opencti.stix_sighting_relationship.update_field(
2416-
id=item["id"], input=input
2417-
)
2418-
elif StixCyberObservableTypes.has_value(item["type"]):
2419-
self.opencti.stix_cyber_observable.update_field(id=item["id"], input=input)
2410+
def apply_patch_files(self, item):
2411+
field_patch = item["opencti_field_patch"]
2412+
field_patch_files = next(
2413+
(op for op in field_patch if op["key"] == "x_opencti_files"), None
2414+
)
2415+
do_add_file = self.opencti.stix_domain_object.add_file
2416+
if StixCyberObservableTypes.has_value(item["type"]):
2417+
do_add_file = self.opencti.stix_cyber_observable.add_file
24202418
elif item["type"] == "external-reference":
2421-
self.opencti.external_reference.update_field(id=item["id"], input=input)
2422-
else:
2423-
self.opencti.stix_domain_object.update_field(id=item["id"], input=input)
2419+
do_add_file = self.opencti.external_reference.add_file
2420+
if field_patch_files is not None:
2421+
for file in field_patch_files["value"]:
2422+
if "data" in file:
2423+
do_add_file(
2424+
id=item["id"],
2425+
file_name=file["name"],
2426+
version=file.get("version", None),
2427+
data=base64.b64decode(file["data"]),
2428+
fileMarkings=file.get("object_marking_refs", None),
2429+
mime_type=file.get("mime_type", None),
2430+
no_trigger_import=file.get("no_trigger_import", False),
2431+
)
2432+
2433+
def apply_patch(self, item):
2434+
field_patch = item["opencti_field_patch"]
2435+
field_patch_without_files = [
2436+
op for op in field_patch if op["key"] != "x_opencti_files"
2437+
]
2438+
if len(field_patch_without_files) > 0:
2439+
if item["type"] == "relationship":
2440+
self.opencti.stix_core_relationship.update_field(
2441+
id=item["id"], input=field_patch_without_files
2442+
)
2443+
elif item["type"] == "sighting":
2444+
self.opencti.stix_sighting_relationship.update_field(
2445+
id=item["id"], input=field_patch_without_files
2446+
)
2447+
elif StixCyberObservableTypes.has_value(item["type"]):
2448+
self.opencti.stix_cyber_observable.update_field(
2449+
id=item["id"], input=field_patch_without_files
2450+
)
2451+
elif item["type"] == "external-reference":
2452+
self.opencti.external_reference.update_field(
2453+
id=item["id"], input=field_patch_without_files
2454+
)
2455+
else:
2456+
self.opencti.stix_domain_object.update_field(
2457+
id=item["id"], input=field_patch_without_files
2458+
)
2459+
self.apply_patch_files(item)
24242460

24252461
def import_item(
24262462
self,

0 commit comments

Comments
 (0)