Skip to content

Commit 2986c2d

Browse files
richard-julienJeremyCloarec
authored andcommitted
[client] Introduce background tasks support
1 parent 7aab3bd commit 2986c2d

14 files changed

+491
-10
lines changed

pycti/api/opencti_api_client.py

+8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010

1111
from pycti import __version__
1212
from pycti.api.opencti_api_connector import OpenCTIApiConnector
13+
from pycti.api.opencti_api_draft import OpenCTIApiDraft
1314
from pycti.api.opencti_api_playbook import OpenCTIApiPlaybook
15+
from pycti.api.opencti_api_public_dashboard import OpenCTIApiPublicDashboard
16+
from pycti.api.opencti_api_trash import OpenCTIApiTrash
1417
from pycti.api.opencti_api_work import OpenCTIApiWork
18+
from pycti.api.opencti_api_workspace import OpenCTIApiWorkspace
1519
from pycti.entities.opencti_attack_pattern import AttackPattern
1620
from pycti.entities.opencti_campaign import Campaign
1721
from pycti.entities.opencti_capability import Capability
@@ -167,6 +171,10 @@ def __init__(
167171
self.session = requests.session()
168172
# Define the dependencies
169173
self.work = OpenCTIApiWork(self)
174+
self.trash = OpenCTIApiTrash(self)
175+
self.draft = OpenCTIApiDraft(self)
176+
self.workspace = OpenCTIApiWorkspace(self)
177+
self.public_dashboard = OpenCTIApiPublicDashboard(self)
170178
self.playbook = OpenCTIApiPlaybook(self)
171179
self.connector = OpenCTIApiConnector(self)
172180
self.stix2 = OpenCTIStix2(self)

pycti/api/opencti_api_draft.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class OpenCTIApiDraft:
2+
"""OpenCTIApiDraft"""
3+
4+
def __init__(self, api):
5+
self.api = api
6+
7+
def delete(self, **kwargs):
8+
id = kwargs.get("id", None)
9+
query = """
10+
mutation DraftWorkspaceDelete($id: ID!) {
11+
draftWorkspaceDelete(id: $id)
12+
}
13+
"""
14+
self.api.query(
15+
query,
16+
{
17+
"id": id,
18+
},
19+
)

pycti/api/opencti_api_playbook.py

+14
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,17 @@ def playbook_step_execution(self, playbook: dict, bundle: str):
3232
"bundle": bundle,
3333
},
3434
)
35+
36+
def delete(self, **kwargs):
37+
id = kwargs.get("id", None)
38+
query = """
39+
mutation PlaybookDelete($id: ID!) {
40+
playbookDelete(id: $id)
41+
}
42+
"""
43+
self.api.query(
44+
query,
45+
{
46+
"id": id,
47+
},
48+
)
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class OpenCTIApiPublicDashboard:
2+
"""OpenCTIApiPublicDashboard"""
3+
4+
def __init__(self, api):
5+
self.api = api
6+
7+
def delete(self, **kwargs):
8+
id = kwargs.get("id", None)
9+
query = """
10+
mutation PublicDashboardDelete($id: ID!) {
11+
publicDashboardDelete(id: $id)
12+
}
13+
"""
14+
self.api.query(
15+
query,
16+
{
17+
"id": id,
18+
},
19+
)

pycti/api/opencti_api_trash.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class OpenCTIApiTrash:
2+
"""OpenCTIApiTrash"""
3+
4+
def __init__(self, api):
5+
self.api = api
6+
7+
def restore(self, operation_id: str):
8+
query = """
9+
mutation DeleteOperationRestore($id: ID!) {
10+
deleteOperationRestore(id: $id)
11+
}
12+
"""
13+
self.api.query(
14+
query,
15+
{
16+
"id": operation_id,
17+
},
18+
)
19+
20+
def delete(self, **kwargs):
21+
"""Delete a role given its ID
22+
23+
:param id: ID for the role on the platform.
24+
:type id: str
25+
"""
26+
id = kwargs.get("id", None)
27+
if id is None:
28+
self.api.admin_logger.error("[opencti_role] Missing parameter: id")
29+
return None
30+
31+
query = """
32+
mutation DeleteOperationConfirm($id: ID!) {
33+
deleteOperationConfirm(id: $id) {
34+
}
35+
"""
36+
self.api.query(
37+
query,
38+
{
39+
"id": id,
40+
},
41+
)

pycti/api/opencti_api_work.py

+14
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ def delete_work(self, work_id: str):
128128
)
129129
return work["data"]
130130

131+
def delete(self, **kwargs):
132+
id = kwargs.get("id", None)
133+
query = """
134+
mutation ConnectorWorksMutation($workId: ID!) {
135+
workEdit(id: $workId) {
136+
delete
137+
}
138+
}"""
139+
work = self.api.query(
140+
query,
141+
{"workId": id},
142+
)
143+
return work["data"]
144+
131145
def wait_for_work_to_finish(self, work_id: str):
132146
status = ""
133147
cnt = 0

pycti/api/opencti_api_workspace.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class OpenCTIApiWorkspace:
2+
"""OpenCTIApiWorkspace"""
3+
4+
def __init__(self, api):
5+
self.api = api
6+
7+
def delete(self, **kwargs):
8+
id = kwargs.get("id", None)
9+
query = """
10+
mutation WorkspaceDelete($id: ID!) {
11+
workspaceDelete(id: $id)
12+
}
13+
"""
14+
self.api.query(
15+
query,
16+
{
17+
"id": id,
18+
},
19+
)

pycti/entities/opencti_group.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,16 @@ def create(self, **kwargs) -> Optional[Dict]:
315315
)
316316
return self.opencti.process_multiple_fields(result["data"]["groupAdd"])
317317

318-
def delete(self, id: str):
318+
def delete(self, **kwargs):
319319
"""Delete a given group from OpenCTI
320320
321321
:param id: ID of the group to delete.
322322
:type id: str
323323
"""
324+
id = kwargs.get("id", None)
325+
if id is None:
326+
self.opencti.admin_logger.error("[opencti_user] Missing parameter: id")
327+
return None
324328
self.opencti.admin_logger.info("Deleting group", {"id": id})
325329
query = """
326330
mutation GroupDelete($id: ID!) {

pycti/entities/opencti_stix.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ def __init__(self, opencti):
1111

1212
def delete(self, **kwargs):
1313
id = kwargs.get("id", None)
14+
force_delete = kwargs.get("force_delete", True)
1415
if id is not None:
1516
self.opencti.app_logger.info("Deleting Stix element", {"id": id})
1617
query = """
17-
mutation StixEdit($id: ID!) {
18+
mutation StixEdit($id: ID!, $forceDelete: Boolean) {
1819
stixEdit(id: $id) {
19-
delete
20+
delete(forceDelete: $forceDelete)
2021
}
2122
}
2223
"""
23-
self.opencti.query(query, {"id": id})
24+
self.opencti.query(query, {"id": id, "forceDelete": force_delete})
2425
else:
2526
self.opencti.app_logger.error("[opencti_stix] Missing parameters: id")
2627
return None

0 commit comments

Comments
 (0)