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

Commit f0558e2

Browse files
author
Samuel Hassine
committed
[client] Adjust new report API
1 parent d008b33 commit f0558e2

File tree

6 files changed

+71
-18
lines changed

6 files changed

+71
-18
lines changed

pycti/api/opencti_api_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __init__(self, url, token, log_level="info", ssl_verify=False, proxies={}):
110110
# Define the entities
111111
self.label = Label(self)
112112
self.marking_definition = MarkingDefinition(self)
113-
self.external_reference = ExternalReference(self)
113+
self.external_reference = ExternalReference(self, File)
114114
self.kill_chain_phase = KillChainPhase(self)
115115
self.opencti_stix_object_or_stix_relationship = StixObjectOrStixRelationship(
116116
self

pycti/entities/opencti_external_reference.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# coding: utf-8
22

33
import json
4+
import os
5+
import magic
46

57

68
class ExternalReference:
7-
def __init__(self, opencti):
9+
def __init__(self, opencti, file):
810
self.opencti = opencti
11+
self.file = file
912
self.properties = """
1013
id
1114
standard_id
@@ -20,6 +23,15 @@ def __init__(self, opencti):
2023
url
2124
hash
2225
external_id
26+
importFiles {
27+
edges {
28+
node {
29+
id
30+
name
31+
size
32+
}
33+
}
34+
}
2335
"""
2436

2537
"""
@@ -181,6 +193,63 @@ def create(self, **kwargs):
181193
"[opencti_external_reference] Missing parameters: source_name and url",
182194
)
183195

196+
"""
197+
Upload a file in this External-Reference
198+
199+
:param id: the Stix-Domain-Object id
200+
:param file_name
201+
:param data
202+
:return void
203+
"""
204+
205+
def add_file(self, **kwargs):
206+
id = kwargs.get("id", None)
207+
file_name = kwargs.get("file_name", None)
208+
data = kwargs.get("data", None)
209+
mime_type = kwargs.get("mime_type", "text/plain")
210+
if id is not None and file_name is not None:
211+
external_reference = self.read(id=id)
212+
if external_reference is None:
213+
self.opencti.log("error", "Cannot add File, entity not found")
214+
return False
215+
final_file_name = os.path.basename(file_name)
216+
current_files = {}
217+
for file in external_reference["importFiles"]:
218+
current_files[file["name"]] = file
219+
if final_file_name in current_files:
220+
return current_files[final_file_name]
221+
else:
222+
self.opencti.log(
223+
"info", "Uploading a file in Stix-Domain-Object {" + id + "}."
224+
)
225+
query = """
226+
mutation ExternalReferenceEdit($id: ID!, $file: Upload!) {
227+
externalReferenceEdit(id: $id) {
228+
importPush(file: $file) {
229+
id
230+
name
231+
}
232+
}
233+
}
234+
"""
235+
if data is None:
236+
data = open(file_name, "rb")
237+
if file_name.endswith(".json"):
238+
mime_type = "application/json"
239+
else:
240+
mime_type = magic.from_file(file_name, mime=True)
241+
242+
return self.opencti.query(
243+
query,
244+
{"id": id, "file": (self.file(final_file_name, data, mime_type))},
245+
)
246+
else:
247+
self.opencti.log(
248+
"error",
249+
"[opencti_stix_domain_object] Missing parameters: id or file_name",
250+
)
251+
return None
252+
184253
"""
185254
Update a External Reference object field
186255

pycti/entities/opencti_report.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ def __init__(self, opencti):
9999
report_types
100100
published
101101
x_opencti_graph_data
102-
x_opencti_report_status
103102
objects {
104103
edges {
105104
node {
@@ -399,7 +398,6 @@ def create(self, **kwargs):
399398
description = kwargs.get("description", "")
400399
report_types = kwargs.get("report_types", None)
401400
published = kwargs.get("published", None)
402-
x_opencti_report_status = kwargs.get("x_opencti_report_status", None)
403401
x_opencti_stix_ids = kwargs.get("x_opencti_stix_ids", None)
404402
update = kwargs.get("update", False)
405403

@@ -433,7 +431,6 @@ def create(self, **kwargs):
433431
"description": description,
434432
"report_types": report_types,
435433
"published": published,
436-
"x_opencti_report_status": x_opencti_report_status,
437434
"x_opencti_stix_ids": x_opencti_stix_ids,
438435
"update": update,
439436
}
@@ -568,12 +565,6 @@ def import_from_stix2(self, **kwargs):
568565
if "x_opencti_source_confidence_level" in stix_object
569566
else 0
570567
)
571-
if "x_opencti_report_status" not in stix_object:
572-
stix_object["x_opencti_report_status"] = (
573-
stix_object["x_opencti_object_status"]
574-
if "x_opencti_object_status" in stix_object
575-
else 0
576-
)
577568

578569
return self.create(
579570
stix_id=stix_object["id"],
@@ -608,9 +599,6 @@ def import_from_stix2(self, **kwargs):
608599
published=stix_object["published"]
609600
if "published" in stix_object
610601
else None,
611-
x_opencti_report_status=stix_object["x_opencti_report_status"]
612-
if "x_opencti_report_status" in stix_object
613-
else None,
614602
x_opencti_stix_ids=stix_object["x_opencti_stix_ids"]
615603
if "x_opencti_stix_ids" in stix_object
616604
else None,

pycti/entities/opencti_stix_domain_object.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import json
44
import os
5-
65
import magic
76

87

@@ -183,7 +182,6 @@ def __init__(self, opencti, file):
183182
description
184183
report_types
185184
published
186-
x_opencti_report_status
187185
objects {
188186
edges {
189187
node {

pycti/entities/opencti_stix_object_or_stix_relationship.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ def __init__(self, opencti):
145145
description
146146
report_types
147147
published
148-
x_opencti_report_status
149148
}
150149
... on CourseOfAction {
151150
name

pycti/utils/opencti_stix2.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,6 @@ def extract_embedded_relationships(
455455
else "",
456456
report_types="threat-report",
457457
published=published,
458-
x_opencti_report_status=2,
459458
update=True,
460459
)
461460
reports[external_reference_id] = report

0 commit comments

Comments
 (0)