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

Commit 2fa8a8b

Browse files
author
Samuel Hassine
committed
[client] Enhance speed of searching entities
1 parent 95b41d5 commit 2fa8a8b

21 files changed

+58
-66
lines changed

examples/export_intrusion_set_stix2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# Create the bundle
1919
bundle = opencti_api_client.stix2.export_entity(
20-
"intrusion-set", intrusion_set["id"], "full"
20+
"indicator", "356fea34-f7f5-4110-937c-47c9a5abb8fa", "full"
2121
)
2222
json_bundle = json.dumps(bundle, indent=4)
2323

examples/import_stix2_file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from pycti import OpenCTIApiClient
44

55
# Variables
6-
api_url = "http://localhost:4000"
7-
api_token = "0b23f787-d013-41a8-8078-97bee84cc99d"
6+
api_url = "https://demo.opencti.io"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

pycti/api/opencti_api_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,10 @@ def process_multiple_fields(self, data):
319319
if "relation" in data["createdByRef"]:
320320
row["remote_relation_id"] = data["createdByRef"]["relation"]["id"]
321321
data["createdByRef"] = row
322+
data["createdByRefId"] = row["id"]
322323
else:
323324
data["createdByRef"] = None
325+
data["createdByRefId"] = None
324326
if "markingDefinitions" in data:
325327
data["markingDefinitions"] = self.process_multiple(
326328
data["markingDefinitions"]

pycti/entities/opencti_attack_pattern.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def create(self, **kwargs):
360360
filters=[{"key": "external_id", "values": [external_id]}]
361361
)
362362
if object_result is not None:
363-
if update or object_result["createdByRef"] == created_by_ref:
363+
if update or object_result["createdByRefId"] == created_by_ref:
364364
# name
365365
if object_result["name"] != name:
366366
self.opencti.stix_domain_entity.update_field(
@@ -411,13 +411,15 @@ def create(self, **kwargs):
411411
and object_result["external_id"] != external_id
412412
):
413413
self.opencti.stix_domain_entity.update_field(
414-
id=object_result["id"], key="external_id", value=external_id
414+
id=object_result["id"],
415+
key="external_id",
416+
value=str(external_id),
415417
)
416418
object_result["external_id"] = external_id
417419
# confidence
418420
if confidence is not None and object_result["confidence"] != confidence:
419421
self.opencti.stix_domain_entity.update_field(
420-
id=object_result["id"], key="confidence", value=confidence
422+
id=object_result["id"], key="confidence", value=str(confidence)
421423
)
422424
object_result["confidence"] = confidence
423425
return object_result

pycti/entities/opencti_campaign.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def create(self, **kwargs):
308308
customAttributes=custom_attributes,
309309
)
310310
if object_result is not None:
311-
if update or object_result["createdByRef"] == created_by_ref:
311+
if update or object_result["createdByRefId"] == created_by_ref:
312312
# name
313313
if object_result["name"] != name:
314314
self.opencti.stix_domain_entity.update_field(

pycti/entities/opencti_course_of_action.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def create(self, **kwargs):
296296
customAttributes=custom_attributes,
297297
)
298298
if object_result is not None:
299-
if update or object_result["createdByRef"] == created_by_ref:
299+
if update or object_result["createdByRefId"] == created_by_ref:
300300
# name
301301
if object_result["name"] != name:
302302
self.opencti.stix_domain_entity.update_field(

pycti/entities/opencti_identity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def create(self, **kwargs):
295295
customAttributes=custom_attributes,
296296
)
297297
if object_result is not None:
298-
if update or object_result["createdByRef"] == created_by_ref:
298+
if update or object_result["createdByRefId"] == created_by_ref:
299299
# name
300300
if object_result["name"] != name:
301301
self.opencti.stix_domain_entity.update_field(

pycti/entities/opencti_incident.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def create(self, **kwargs):
314314
customAttributes=custom_attributes,
315315
)
316316
if object_result is not None:
317-
if update or object_result["createdByRef"] == created_by_ref:
317+
if update or object_result["createdByRefId"] == created_by_ref:
318318
# name
319319
if object_result["name"] != name:
320320
self.opencti.stix_domain_entity.update_field(

pycti/entities/opencti_indicator.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def __init__(self, opencti):
2020
graph_data
2121
indicator_pattern
2222
pattern_type
23+
detection
24+
confidence
2325
valid_from
2426
valid_until
2527
score
@@ -243,7 +245,7 @@ def read(self, **kwargs):
243245
result = self.opencti.query(query, {"id": id})
244246
return self.opencti.process_multiple_fields(result["data"]["indicator"])
245247
elif filters is not None:
246-
result = self.list(filters=filters)
248+
result = self.list(filters=filters, customAttributes=custom_attributes)
247249
if len(result) > 0:
248250
return result[0]
249251
else:
@@ -390,7 +392,7 @@ def create(self, **kwargs):
390392
"""
391393
object_result = None
392394
if stix_id_key is not None:
393-
object_result = self.opencti.indicator.read(
395+
object_result = self.read(
394396
id=stix_id_key, customAttributes=custom_attributes
395397
)
396398
if object_result is None:
@@ -405,7 +407,7 @@ def create(self, **kwargs):
405407
customAttributes=custom_attributes,
406408
)
407409
if object_result is not None:
408-
if update or object_result["createdByRef"] == created_by_ref:
410+
if update or object_result["createdByRefId"] == created_by_ref:
409411
# name
410412
if name is not None and object_result["name"] != name:
411413
self.opencti.stix_domain_entity.update_field(
@@ -424,19 +426,21 @@ def create(self, **kwargs):
424426
# score
425427
if score is not None and object_result["score"] != score:
426428
self.opencti.stix_domain_entity.update_field(
427-
id=object_result["id"], key="score", value=score
429+
id=object_result["id"], key="score", value=str(score)
428430
)
429431
object_result["score"] = score
430432
# confidence
431433
if confidence is not None and object_result["confidence"] != confidence:
432434
self.opencti.stix_domain_entity.update_field(
433-
id=object_result["id"], key="confidence", value=confidence
435+
id=object_result["id"], key="confidence", value=str(confidence)
434436
)
435437
object_result["confidence"] = confidence
436438
# detection
437439
if detection is not None and object_result["detection"] != detection:
438440
self.opencti.stix_domain_entity.update_field(
439-
id=object_result["id"], key="detection", value=detection
441+
id=object_result["id"],
442+
key="detection",
443+
value=str(detection).lower(),
440444
)
441445
object_result["detection"] = detection
442446
return object_result

pycti/entities/opencti_intrusion_set.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def create(self, **kwargs):
330330
customAttributes=custom_attributes,
331331
)
332332
if object_result is not None:
333-
if update or object_result["createdByRef"] == created_by_ref:
333+
if update or object_result["createdByRefId"] == created_by_ref:
334334
# name
335335
if object_result["name"] != name:
336336
self.opencti.stix_domain_entity.update_field(

0 commit comments

Comments
 (0)