Skip to content

Commit 681b5e1

Browse files
[client] background tasks fixes (opencti #10274)
1 parent 0c5881c commit 681b5e1

File tree

3 files changed

+201
-7
lines changed

3 files changed

+201
-7
lines changed

pycti/entities/opencti_stix_core_relationship.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,3 +1247,86 @@ def import_from_stix2(self, **kwargs):
12471247
self.opencti.app_logger.error(
12481248
"[opencti_stix_core_relationship] Missing parameters: stixObject"
12491249
)
1250+
1251+
"""
1252+
Share element to multiple organizations
1253+
1254+
:param entity_id: the stix_core_relationship id
1255+
:param organization_id:s the organization to share with
1256+
:return void
1257+
"""
1258+
1259+
def organization_share(self, entity_id, organization_ids, sharing_direct_container):
1260+
query = """
1261+
mutation StixCoreRelationshipEdit($id: ID!, $organizationId: [ID!]!, $directContainerSharing: Boolean) {
1262+
stixCoreRelationshipEdit(id: $id) {
1263+
restrictionOrganizationAdd(organizationId: $organizationId, directContainerSharing: $directContainerSharing) {
1264+
id
1265+
}
1266+
}
1267+
}
1268+
"""
1269+
self.opencti.query(
1270+
query,
1271+
{
1272+
"id": entity_id,
1273+
"organizationId": organization_ids,
1274+
"directContainerSharing": sharing_direct_container,
1275+
},
1276+
)
1277+
1278+
"""
1279+
Unshare element from multiple organizations
1280+
1281+
:param entity_id: the stix_core_relationship id
1282+
:param organization_id:s the organization to share with
1283+
:return void
1284+
"""
1285+
1286+
def organization_unshare(
1287+
self, entity_id, organization_ids, sharing_direct_container
1288+
):
1289+
query = """
1290+
mutation StixCoreRelationshipEdit($id: ID!, $organizationId: [ID!]!, $directContainerSharing: Boolean) {
1291+
stixCoreRelationshipEdit(id: $id) {
1292+
restrictionOrganizationDelete(organizationId: $organizationId, directContainerSharing: $directContainerSharing) {
1293+
id
1294+
}
1295+
}
1296+
}
1297+
"""
1298+
self.opencti.query(
1299+
query,
1300+
{
1301+
"id": entity_id,
1302+
"organizationId": organization_ids,
1303+
"directContainerSharing": sharing_direct_container,
1304+
},
1305+
)
1306+
1307+
"""
1308+
Remove a stix_core_relationship object from draft (revert)
1309+
1310+
:param id: the stix_core_relationship id
1311+
:return void
1312+
"""
1313+
1314+
def remove_from_draft(self, **kwargs):
1315+
id = kwargs.get("id", None)
1316+
if id is not None:
1317+
self.opencti.app_logger.info(
1318+
"Draft remove stix_core_relationship", {"id": id}
1319+
)
1320+
query = """
1321+
mutation StixCoreRelationshipEditDraftRemove($id: ID!) {
1322+
stixCoreRelationshipEdit(id: $id) {
1323+
removeFromDraft
1324+
}
1325+
}
1326+
"""
1327+
self.opencti.query(query, {"id": id})
1328+
else:
1329+
self.opencti.app_logger.error(
1330+
"[stix_core_relationship] Cant remove from draft, missing parameters: id"
1331+
)
1332+
return None

pycti/entities/opencti_stix_sighting_relationship.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,87 @@ def update_created_by(self, **kwargs):
802802
self.opencti.app_logger.error("Missing parameters: id")
803803
return False
804804

805+
"""
806+
Share element to multiple organizations
807+
808+
:param entity_id: the stix_sighting id
809+
:param organization_id:s the organization to share with
810+
:return void
811+
"""
812+
813+
def organization_share(self, entity_id, organization_ids, sharing_direct_container):
814+
query = """
815+
mutation StixSightingRelationshipEdit($id: ID!, $organizationId: [ID!]!, $directContainerSharing: Boolean) {
816+
stixSightingRelationshipEdit(id: $id) {
817+
restrictionOrganizationAdd(organizationId: $organizationId, directContainerSharing: $directContainerSharing) {
818+
id
819+
}
820+
}
821+
}
822+
"""
823+
self.opencti.query(
824+
query,
825+
{
826+
"id": entity_id,
827+
"organizationId": organization_ids,
828+
"directContainerSharing": sharing_direct_container,
829+
},
830+
)
831+
832+
"""
833+
Unshare element from multiple organizations
834+
835+
:param entity_id: the stix_sighting id
836+
:param organization_id:s the organization to share with
837+
:return void
838+
"""
839+
840+
def organization_unshare(
841+
self, entity_id, organization_ids, sharing_direct_container
842+
):
843+
query = """
844+
mutation StixSightingRelationshipEdit($id: ID!, $organizationId: [ID!]!, $directContainerSharing: Boolean) {
845+
stixSightingRelationshipEdit(id: $id) {
846+
restrictionOrganizationDelete(organizationId: $organizationId, directContainerSharing: $directContainerSharing) {
847+
id
848+
}
849+
}
850+
}
851+
"""
852+
self.opencti.query(
853+
query,
854+
{
855+
"id": entity_id,
856+
"organizationId": organization_ids,
857+
"directContainerSharing": sharing_direct_container,
858+
},
859+
)
860+
861+
"""
862+
Remove a stix_sighting object from draft (revert)
863+
864+
:param id: the stix_sighting id
865+
:return void
866+
"""
867+
868+
def remove_from_draft(self, **kwargs):
869+
id = kwargs.get("id", None)
870+
if id is not None:
871+
self.opencti.app_logger.info("Draft remove stix_sighting", {"id": id})
872+
query = """
873+
mutation StixSightingRelationshipEditDraftRemove($id: ID!) {
874+
stixSightingRelationshipEdit(id: $id) {
875+
removeFromDraft
876+
}
877+
}
878+
"""
879+
self.opencti.query(query, {"id": id})
880+
else:
881+
self.opencti.app_logger.error(
882+
"[stix_sighting] Cant remove from draft, missing parameters: id"
883+
)
884+
return None
885+
805886
"""
806887
Delete a stix_sighting
807888

pycti/utils/opencti_stix2.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,9 +2522,20 @@ def organization_share(self, item):
25222522
)
25232523
if sharing_direct_container is None:
25242524
sharing_direct_container = item["sharing_direct_container"]
2525-
self.opencti.stix_core_object.organization_share(
2526-
item["id"], organization_ids, sharing_direct_container
2527-
)
2525+
2526+
if item["type"] == "relationship":
2527+
self.opencti.stix_core_relationship.organization_share(
2528+
item["id"], organization_ids, sharing_direct_container
2529+
)
2530+
elif item["type"] == "sighting":
2531+
self.opencti.stix_sighting_relationship.organization_share(
2532+
item["id"], organization_ids, sharing_direct_container
2533+
)
2534+
else:
2535+
# Element is considered stix core object
2536+
self.opencti.stix_core_object.organization_share(
2537+
item["id"], organization_ids, sharing_direct_container
2538+
)
25282539

25292540
def organization_unshare(self, item):
25302541
organization_ids = self.opencti.get_attribute_in_extension(
@@ -2537,9 +2548,19 @@ def organization_unshare(self, item):
25372548
)
25382549
if sharing_direct_container is None:
25392550
sharing_direct_container = item["sharing_direct_container"]
2540-
self.opencti.stix_core_object.organization_unshare(
2541-
item["id"], organization_ids, sharing_direct_container
2542-
)
2551+
if item["type"] == "relationship":
2552+
self.opencti.stix_core_relationship.organization_unshare(
2553+
item["id"], organization_ids, sharing_direct_container
2554+
)
2555+
elif item["type"] == "sighting":
2556+
self.opencti.stix_sighting_relationship.organization_unshare(
2557+
item["id"], organization_ids, sharing_direct_container
2558+
)
2559+
else:
2560+
# Element is considered stix core object
2561+
self.opencti.stix_core_object.organization_unshare(
2562+
item["id"], organization_ids, sharing_direct_container
2563+
)
25432564

25442565
def element_operation_delete(self, item, operation):
25452566
# If data is stix, just use the generic stix function for deletion
@@ -2564,11 +2585,20 @@ def element_operation_delete(self, item, operation):
25642585
"Delete operation or not found stix helper", {"type": item["type"]}
25652586
)
25662587

2588+
def element_remove_from_draft(self, item):
2589+
if item["type"] == "relationship":
2590+
self.opencti.stix_core_relationship.remove_from_draft(id=item["id"])
2591+
elif item["type"] == "sighting":
2592+
self.opencti.stix_sighting_relationship.remove_from_draft(id=item["id"])
2593+
else:
2594+
# Element is considered stix core object
2595+
self.opencti.stix_core_object.remove_from_draft(id=item["id"])
2596+
25672597
def apply_opencti_operation(self, item, operation):
25682598
if operation == "delete" or operation == "delete_force":
25692599
self.element_operation_delete(item=item, operation=operation)
25702600
elif operation == "revert_draft":
2571-
self.opencti.stix_core_object.remove_from_draft(id=item["id"])
2601+
self.element_remove_from_draft(item=item)
25722602
elif operation == "restore":
25732603
self.opencti.trash.restore(item["id"])
25742604
elif operation == "merge":

0 commit comments

Comments
 (0)