Skip to content

[client] replace ruleApply in background task to direct inferred creation (opencti #11626) #935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pycti/api/opencti_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pycti import __version__
from pycti.api.opencti_api_connector import OpenCTIApiConnector
from pycti.api.opencti_api_draft import OpenCTIApiDraft
from pycti.api.opencti_api_inferred import OpenCTIApiInferred
from pycti.api.opencti_api_internal_file import OpenCTIApiInternalFile
from pycti.api.opencti_api_notification import OpenCTIApiNotification
from pycti.api.opencti_api_pir import OpenCTIApiPir
Expand Down Expand Up @@ -175,6 +176,7 @@ def __init__(
# Define the dependencies
self.work = OpenCTIApiWork(self)
self.notification = OpenCTIApiNotification(self)
self.inferred = OpenCTIApiInferred(self)
self.trash = OpenCTIApiTrash(self)
self.draft = OpenCTIApiDraft(self)
self.workspace = OpenCTIApiWorkspace(self)
Expand Down
25 changes: 25 additions & 0 deletions pycti/api/opencti_api_inferred.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class OpenCTIApiInferred:
"""OpenCTIApiInferred"""

def __init__(self, api):
self.api = api

def create_inferred_rel(self, **kwargs):
input = kwargs.get("input", None)
self.api.app_logger.info("Creating inferred rel", {"input": input})
query = """
mutation inferredRelationAdd($jsonInput: String!) {
inferredRelationAdd(jsonInput: $jsonInput)
}
"""
self.api.query(query, {"jsonInput": input})

def create_inferred_entity(self, **kwargs):
input = kwargs.get("input", None)
self.api.app_logger.info("Creating inferred entity", {"input": input})
query = """
mutation inferredEntityAdd($jsonInput: String!) {
inferredEntityAdd(jsonInput: $jsonInput)
}
"""
self.api.query(query, {"jsonInput": input})
14 changes: 14 additions & 0 deletions pycti/utils/opencti_stix2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2644,6 +2644,20 @@ def apply_opencti_operation(self, item, operation):
self.opencti.pir.pir_unflag_element(id=id, input=input)
elif operation == "rule_apply":
self.rule_apply(item=item)
elif operation == "inferred_entity":
opencti_inferred_input = self.opencti.get_attribute_in_extension(
"opencti_inferred_input", item
)
if opencti_inferred_input is None:
opencti_inferred_input = item["opencti_inferred_input"]
self.opencti.inferred.create_inferred_entity(input=opencti_inferred_input)
elif operation == "inferred_rel":
opencti_inferred_input = self.opencti.get_attribute_in_extension(
"opencti_inferred_input", item
)
if opencti_inferred_input is None:
opencti_inferred_input = item["opencti_inferred_input"]
self.opencti.inferred.create_inferred_rel(input=opencti_inferred_input)
elif operation == "rule_clear":
self.rule_clear(item=item)
elif operation == "rules_rescan":
Expand Down