Skip to content

Commit db65081

Browse files
committed
Merge branch 'master' into release/current
2 parents 681b5e1 + d932db5 commit db65081

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

pycti/connector/opencti_connector_helper.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,14 +1918,13 @@ def send_stix2_bundle(self, bundle: str, **kwargs) -> list:
19181918
os.rename(write_file, final_write_file)
19191919

19201920
stix2_splitter = OpenCTIStix2Splitter()
1921-
(
1922-
expectations_number,
1923-
bundles,
1924-
) = stix2_splitter.split_bundle_with_expectations(
1925-
bundle=bundle,
1926-
use_json=True,
1927-
event_version=event_version,
1928-
cleanup_inconsistent_bundle=cleanup_inconsistent_bundle,
1921+
(expectations_number, _, bundles) = (
1922+
stix2_splitter.split_bundle_with_expectations(
1923+
bundle=bundle,
1924+
use_json=True,
1925+
event_version=event_version,
1926+
cleanup_inconsistent_bundle=cleanup_inconsistent_bundle,
1927+
)
19291928
)
19301929

19311930
if len(bundles) == 0:

pycti/utils/opencti_stix2.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2911,9 +2911,25 @@ def import_bundle(
29112911
)
29122912

29132913
stix2_splitter = OpenCTIStix2Splitter()
2914-
_, bundles = stix2_splitter.split_bundle_with_expectations(
2915-
stix_bundle, False, event_version
2914+
_, incompatible_elements, bundles = (
2915+
stix2_splitter.split_bundle_with_expectations(
2916+
stix_bundle, False, event_version
2917+
)
29162918
)
2919+
2920+
# Report every element ignored during bundle splitting
2921+
if work_id is not None:
2922+
for incompatible_element in incompatible_elements:
2923+
self.opencti.work.report_expectation(
2924+
work_id,
2925+
{
2926+
"error": "Incompatible element in bundle",
2927+
"source": "Element "
2928+
+ incompatible_element["id"]
2929+
+ " is incompatible and couldn't be processed",
2930+
},
2931+
)
2932+
29172933
# Import every element in a specific order
29182934
imported_elements = []
29192935
for bundle in bundles:

pycti/utils/opencti_stix2_splitter.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def __init__(self):
3838
self.cache_index = {}
3939
self.cache_refs = {}
4040
self.elements = []
41+
self.incompatible_items = []
4142

4243
def get_internal_ids_in_extension(self, item):
4344
ids = []
@@ -192,6 +193,8 @@ def enlist_element(
192193
is_compatible = is_id_supported(item_id)
193194
if is_compatible:
194195
self.elements.append(item)
196+
else:
197+
self.incompatible_items.append(item)
195198
self.cache_index[item_id] = item
196199
for internal_id in self.get_internal_ids_in_extension(item):
197200
self.cache_index[internal_id] = item
@@ -204,7 +207,7 @@ def split_bundle_with_expectations(
204207
use_json=True,
205208
event_version=None,
206209
cleanup_inconsistent_bundle=False,
207-
) -> Tuple[int, list]:
210+
) -> Tuple[int, list, list]:
208211
"""splits a valid stix2 bundle into a list of bundles"""
209212
if use_json:
210213
try:
@@ -254,11 +257,11 @@ def by_dep_size(elem):
254257
)
255258
)
256259

257-
return number_expectations, bundles
260+
return number_expectations, self.incompatible_items, bundles
258261

259262
@deprecated("Use split_bundle_with_expectations instead")
260263
def split_bundle(self, bundle, use_json=True, event_version=None) -> list:
261-
expectations, bundles = self.split_bundle_with_expectations(
264+
_, _, bundles = self.split_bundle_with_expectations(
262265
bundle, use_json, event_version
263266
)
264267
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)