Skip to content

[client] report work expectation without impersonation (#10829) #890

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

Merged
merged 6 commits into from
May 26, 2025
Merged
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
11 changes: 8 additions & 3 deletions pycti/api/opencti_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,15 @@ def set_retry_number(self, retry_number):
"" if retry_number is None else str(retry_number)
)

def query(self, query, variables=None):
def query(self, query, variables=None, disable_impersonate=False):
"""submit a query to the OpenCTI GraphQL API

:param query: GraphQL query string
:type query: str
:param variables: GraphQL query variables, defaults to {}
:type variables: dict, optional
:param disable_impersonate: removes impersonate header if set to True, defaults to False
:type disable_impersonate: bool, optional
:return: returns the response json content
:rtype: Any
"""
Expand All @@ -295,6 +297,9 @@ def query(self, query, variables=None):
else:
query_var[key] = val

query_headers = self.request_headers.copy()
if disable_impersonate and "opencti-applicant-id" in query_headers:
del query_headers["opencti-applicant-id"]
# If yes, transform variable (file to null) and create multipart query
if len(files_vars) > 0:
multipart_data = {
Expand Down Expand Up @@ -361,7 +366,7 @@ def query(self, query, variables=None):
self.api_url,
data=multipart_data,
files=multipart_files,
headers=self.request_headers,
headers=query_headers,
verify=self.ssl_verify,
cert=self.cert,
proxies=self.proxies,
Expand All @@ -372,7 +377,7 @@ def query(self, query, variables=None):
r = self.session.post(
self.api_url,
json={"query": query, "variables": variables},
headers=self.request_headers,
headers=query_headers,
verify=self.ssl_verify,
cert=self.cert,
proxies=self.proxies,
Expand Down
29 changes: 15 additions & 14 deletions pycti/api/opencti_api_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def to_received(self, work_id: str, message: str):
}
}
"""
self.api.query(query, {"id": work_id, "message": message})
self.api.query(query, {"id": work_id, "message": message}, True)

def to_processed(self, work_id: str, message: str, in_error: bool = False):
if self.api.bundle_send_to_queue:
Expand All @@ -35,7 +35,7 @@ def to_processed(self, work_id: str, message: str, in_error: bool = False):
}
"""
self.api.query(
query, {"id": work_id, "message": message, "inError": in_error}
query, {"id": work_id, "message": message, "inError": in_error}, True
)

def ping(self, work_id: str):
Expand All @@ -60,7 +60,7 @@ def report_expectation(self, work_id: str, error):
}
"""
try:
self.api.query(query, {"id": work_id, "error": error})
self.api.query(query, {"id": work_id, "error": error}, True)
except:
self.api.app_logger.error("Cannot report expectation")

Expand All @@ -78,7 +78,9 @@ def add_expectations(self, work_id: str, expectations: int):
}
"""
try:
self.api.query(query, {"id": work_id, "expectations": expectations})
self.api.query(
query, {"id": work_id, "expectations": expectations}, True
)
except:
self.api.app_logger.error("Cannot report expectation")

Expand All @@ -96,7 +98,9 @@ def add_draft_context(self, work_id: str, draft_context: str):
}
"""
try:
self.api.query(query, {"id": work_id, "draftContext": draft_context})
self.api.query(
query, {"id": work_id, "draftContext": draft_context}, True
)
except:
self.api.app_logger.error("Cannot report draft context")

Expand All @@ -111,7 +115,9 @@ def initiate_work(self, connector_id: str, friendly_name: str) -> str:
}
"""
work = self.api.query(
query, {"connectorId": connector_id, "friendlyName": friendly_name}
query,
{"connectorId": connector_id, "friendlyName": friendly_name},
True,
)
return work["data"]["workAdd"]["id"]

Expand All @@ -122,10 +128,7 @@ def delete_work(self, work_id: str):
delete
}
}"""
work = self.api.query(
query,
{"workId": work_id},
)
work = self.api.query(query, {"workId": work_id}, True)
return work["data"]

def wait_for_work_to_finish(self, work_id: str):
Expand Down Expand Up @@ -179,10 +182,7 @@ def get_work(self, work_id: str) -> Dict:
}
}
"""
result = self.api.query(
query,
{"id": work_id},
)
result = self.api.query(query, {"id": work_id}, True)
return result["data"]["work"]

def get_connector_works(self, connector_id: str) -> List[Dict]:
Expand Down Expand Up @@ -243,6 +243,7 @@ def get_connector_works(self, connector_id: str) -> List[Dict]:
"filterGroups": [],
},
},
True,
)
result = result["data"]["works"]["edges"]
return_value = []
Expand Down