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

Commit 9da0100

Browse files
richard-julienJeremyCloarec
authored andcommitted
[client] Introduce background tasks support
1 parent 741235e commit 9da0100

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

pycti/entities/opencti_stix_core_object.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,52 @@ def reports(self, **kwargs):
16801680
self.opencti.app_logger.error("Missing parameters: id")
16811681
return None
16821682

1683+
"""
1684+
Apply rule to Stix-Core-Object object
1685+
1686+
:param element_id: the Stix-Core-Object id
1687+
:param rule_id: the rule to apply
1688+
:return void
1689+
"""
1690+
1691+
def rule_apply(self, **kwargs):
1692+
rule_id = kwargs.get("rule_id", None)
1693+
element_id = kwargs.get("element_id", None)
1694+
if element_id is not None and rule_id is not None:
1695+
self.opencti.app_logger.info("Apply rule stix_core_object", {"id": element_id})
1696+
query = """
1697+
mutation StixCoreApplyRule($elementId: ID!, $ruleId: ID!) {
1698+
ruleApply(elementId: $elementId, ruleId: $ruleId)
1699+
}
1700+
"""
1701+
self.opencti.query(query, {"elementId": element_id, "ruleId": rule_id})
1702+
else:
1703+
self.opencti.app_logger.error("[stix_core_object] Missing parameters: id")
1704+
return None
1705+
1706+
"""
1707+
Apply rule clear to Stix-Core-Object object
1708+
1709+
:param element_id: the Stix-Core-Object id
1710+
:param rule_id: the rule to apply
1711+
:return void
1712+
"""
1713+
1714+
def rule_clear(self, **kwargs):
1715+
rule_id = kwargs.get("rule_id", None)
1716+
element_id = kwargs.get("element_id", None)
1717+
if element_id is not None and rule_id is not None:
1718+
self.opencti.app_logger.info("Apply rule clear stix_core_object", {"id": element_id})
1719+
query = """
1720+
mutation StixCoreClearRule($elementId: ID!, $ruleId: ID!) {
1721+
ruleClear(elementId: $elementId, ruleId: $ruleId)
1722+
}
1723+
"""
1724+
self.opencti.query(query, {"elementId": element_id, "ruleId": rule_id})
1725+
else:
1726+
self.opencti.app_logger.error("[stix_core_object] Missing parameters: id")
1727+
return None
1728+
16831729
"""
16841730
Delete a Stix-Core-Object object
16851731

pycti/utils/opencti_stix2.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,6 +2467,14 @@ def apply_patch(self, item):
24672467
)
24682468
self.apply_patch_files(item)
24692469

2470+
def rule_apply(self, item):
2471+
rule_id = item["opencti_rule"]
2472+
self.opencti.stix_core_object.rule_apply(element_id=item["id"], rule_id=rule_id)
2473+
2474+
def rule_clear(self, item):
2475+
rule_id = item["opencti_rule"]
2476+
self.opencti.stix_core_object.rule_clear(element_id=item["id"], rule_id=rule_id)
2477+
24702478
def apply_opencti_operation(self, item, operation):
24712479
if operation == "delete":
24722480
delete_id = item["id"]
@@ -2477,6 +2485,10 @@ def apply_opencti_operation(self, item, operation):
24772485
self.opencti.stix.merge(id=target_id, object_ids=source_ids)
24782486
elif operation == "patch":
24792487
self.apply_patch(item=item)
2488+
elif operation == "rule_apply":
2489+
self.rule_apply(item=item)
2490+
elif operation == "rule_clear":
2491+
self.rule_clear(item=item)
24802492
else:
24812493
raise ValueError("Not supported opencti_operation")
24822494

pycti/utils/opencti_stix2_splitter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ def enlist_element(
176176
# Put in cache
177177
if self.cache_index.get(item_id) is None:
178178
# enlist only if compatible
179-
if item["type"] == "relationship":
179+
if item["type"] == "relationship" and item.get("opencti_operation") is None:
180180
is_compatible = (
181181
item["source_ref"] is not None and item["target_ref"] is not None
182182
)
183-
elif item["type"] == "sighting":
183+
elif item["type"] == "sighting" and item.get("opencti_operation") is None:
184184
is_compatible = (
185185
item["sighting_of_ref"] is not None
186186
and len(item["where_sighted_refs"]) > 0

0 commit comments

Comments
 (0)