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

Commit f9cd703

Browse files
committed
[client] report expection when element is ignored during import
1 parent c00ada1 commit f9cd703

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

pycti/connector/opencti_connector_helper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,7 @@ def send_stix2_bundle(self, bundle: str, **kwargs) -> list:
19211921
(
19221922
expectations_number,
19231923
bundles,
1924+
_
19241925
) = stix2_splitter.split_bundle_with_expectations(
19251926
bundle=bundle,
19261927
use_json=True,

pycti/utils/opencti_stix2.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2748,9 +2748,15 @@ def import_bundle(
27482748
)
27492749

27502750
stix2_splitter = OpenCTIStix2Splitter()
2751-
_, bundles = stix2_splitter.split_bundle_with_expectations(
2751+
_, nb_incompatible_elements, bundles = stix2_splitter.split_bundle_with_expectations(
27522752
stix_bundle, False, event_version
27532753
)
2754+
2755+
# Report every element ignored during bundle splitting
2756+
if work_id is not None:
2757+
for i in range(nb_incompatible_elements):
2758+
self.opencti.work.report_expectation(work_id, None)
2759+
27542760
# Import every element in a specific order
27552761
imported_elements = []
27562762
for bundle in bundles:

pycti/utils/opencti_stix2_splitter.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def __init__(self):
3535
self.cache_index = {}
3636
self.cache_refs = {}
3737
self.elements = []
38+
self.nb_incompatible_items = 0
3839

3940
def get_internal_ids_in_extension(self, item):
4041
ids = []
@@ -189,6 +190,8 @@ def enlist_element(
189190
is_compatible = is_id_supported(item_id)
190191
if is_compatible:
191192
self.elements.append(item)
193+
else:
194+
self.nb_incompatible_items = self.nb_incompatible_items + 1
192195
self.cache_index[item_id] = item
193196
for internal_id in self.get_internal_ids_in_extension(item):
194197
self.cache_index[internal_id] = item
@@ -201,7 +204,7 @@ def split_bundle_with_expectations(
201204
use_json=True,
202205
event_version=None,
203206
cleanup_inconsistent_bundle=False,
204-
) -> Tuple[int, list]:
207+
) -> Tuple[int, int, list]:
205208
"""splits a valid stix2 bundle into a list of bundles"""
206209
if use_json:
207210
try:
@@ -251,11 +254,11 @@ def by_dep_size(elem):
251254
)
252255
)
253256

254-
return number_expectations, bundles
257+
return number_expectations, self.nb_incompatible_items, bundles
255258

256259
@deprecated("Use split_bundle_with_expectations instead")
257260
def split_bundle(self, bundle, use_json=True, event_version=None) -> list:
258-
expectations, bundles = self.split_bundle_with_expectations(
261+
_, _, bundles = self.split_bundle_with_expectations(
259262
bundle, use_json, event_version
260263
)
261264
return bundles

tests/01-unit/utils/test_opencti_stix2_splitter.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ def test_split_bundle():
1010
stix_splitter = OpenCTIStix2Splitter()
1111
with open("./tests/data/enterprise-attack.json") as file:
1212
content = file.read()
13-
expectations, bundles = stix_splitter.split_bundle_with_expectations(content)
13+
expectations, _, bundles = stix_splitter.split_bundle_with_expectations(content)
1414
assert expectations == 7016
1515

1616

1717
def test_split_test_bundle():
1818
stix_splitter = OpenCTIStix2Splitter()
1919
with open("./tests/data/DATA-TEST-STIX2_v2.json") as file:
2020
content = file.read()
21-
expectations, bundles = stix_splitter.split_bundle_with_expectations(content)
21+
expectations, _, bundles = stix_splitter.split_bundle_with_expectations(content)
2222
assert expectations == 59
2323
base_bundles = json.loads(content)["objects"]
2424
for base in base_bundles:
@@ -40,13 +40,13 @@ def test_split_mono_entity_bundle():
4040
stix_splitter = OpenCTIStix2Splitter()
4141
with open("./tests/data/mono-bundle-entity.json") as file:
4242
content = file.read()
43-
expectations, bundles = stix_splitter.split_bundle_with_expectations(content)
43+
expectations, _, bundles = stix_splitter.split_bundle_with_expectations(content)
4444
assert expectations == 1
4545
json_bundle = json.loads(bundles[0])["objects"][0]
4646
assert json_bundle["created_by_ref"] == "fa42a846-8d90-4e51-bc29-71d5b4802168"
4747
# Split with cleanup_inconsistent_bundle
4848
stix_splitter = OpenCTIStix2Splitter()
49-
expectations, bundles = stix_splitter.split_bundle_with_expectations(
49+
expectations, _, bundles = stix_splitter.split_bundle_with_expectations(
5050
bundle=content, cleanup_inconsistent_bundle=True
5151
)
5252
assert expectations == 1
@@ -58,11 +58,11 @@ def test_split_mono_relationship_bundle():
5858
stix_splitter = OpenCTIStix2Splitter()
5959
with open("./tests/data/mono-bundle-relationship.json") as file:
6060
content = file.read()
61-
expectations, bundles = stix_splitter.split_bundle_with_expectations(content)
61+
expectations, _, bundles = stix_splitter.split_bundle_with_expectations(content)
6262
assert expectations == 1
6363
# Split with cleanup_inconsistent_bundle
6464
stix_splitter = OpenCTIStix2Splitter()
65-
expectations, bundles = stix_splitter.split_bundle_with_expectations(
65+
expectations, _, bundles = stix_splitter.split_bundle_with_expectations(
6666
bundle=content, cleanup_inconsistent_bundle=True
6767
)
6868
assert expectations == 0
@@ -72,19 +72,19 @@ def test_split_capec_bundle():
7272
stix_splitter = OpenCTIStix2Splitter()
7373
with open("./tests/data/mitre_att_capec.json") as file:
7474
content = file.read()
75-
expectations, bundles = stix_splitter.split_bundle_with_expectations(content)
75+
expectations, _, bundles = stix_splitter.split_bundle_with_expectations(content)
7676
assert expectations == 2610
7777

7878

7979
def test_split_internal_ids_bundle():
8080
stix_splitter = OpenCTIStix2Splitter()
8181
with open("./tests/data/bundle_with_internal_ids.json") as file:
8282
content = file.read()
83-
expectations, bundles = stix_splitter.split_bundle_with_expectations(content)
83+
expectations, _, bundles = stix_splitter.split_bundle_with_expectations(content)
8484
assert expectations == 4
8585
# Split with cleanup_inconsistent_bundle
8686
stix_splitter = OpenCTIStix2Splitter()
87-
expectations, bundles = stix_splitter.split_bundle_with_expectations(
87+
expectations, _, bundles = stix_splitter.split_bundle_with_expectations(
8888
bundle=content, cleanup_inconsistent_bundle=True
8989
)
9090
assert expectations == 4
@@ -101,11 +101,11 @@ def test_split_missing_refs_bundle():
101101
stix_splitter = OpenCTIStix2Splitter()
102102
with open("./tests/data/missing_refs.json") as file:
103103
content = file.read()
104-
expectations, bundles = stix_splitter.split_bundle_with_expectations(content)
104+
expectations, _, bundles = stix_splitter.split_bundle_with_expectations(content)
105105
assert expectations == 4
106106
# Split with cleanup_inconsistent_bundle
107107
stix_splitter = OpenCTIStix2Splitter()
108-
expectations, bundles = stix_splitter.split_bundle_with_expectations(
108+
expectations, _, bundles = stix_splitter.split_bundle_with_expectations(
109109
bundle=content, cleanup_inconsistent_bundle=True
110110
)
111111
assert expectations == 3
@@ -115,7 +115,7 @@ def test_split_cyclic_bundle():
115115
stix_splitter = OpenCTIStix2Splitter()
116116
with open("./tests/data/cyclic-bundle.json") as file:
117117
content = file.read()
118-
expectations, bundles = stix_splitter.split_bundle_with_expectations(content)
118+
expectations, _, bundles = stix_splitter.split_bundle_with_expectations(content)
119119
assert expectations == 6
120120
for bundle in bundles:
121121
json_bundle = json.loads(bundle)

0 commit comments

Comments
 (0)