Skip to content

Commit

Permalink
[client] Implement get_all everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelHassine committed Jul 12, 2023
1 parent cd25966 commit 4e97549
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 30 deletions.
30 changes: 26 additions & 4 deletions pycti/entities/opencti_campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def list(self, **kwargs):
get_all = kwargs.get("getAll", False)
with_pagination = kwargs.get("withPagination", False)
if get_all:
first = 500
first = 100

LOGGER.info("Listing Campaigns with filters %s.", json.dumps(filters))
query = (
Expand Down Expand Up @@ -196,9 +196,31 @@ def list(self, **kwargs):
"orderMode": order_mode,
},
)
return self.opencti.process_multiple(
result["data"]["campaigns"], with_pagination
)
if get_all:
final_data = []
data = self.opencti.process_multiple(result["data"]["campaigns"])
final_data = final_data + data
while result["data"]["campaigns"]["pageInfo"]["hasNextPage"]:
after = result["data"]["campaigns"]["pageInfo"]["endCursor"]
LOGGER.info("Listing Campaigns after " + after)
result = self.opencti.query(
query,
{
"filters": filters,
"search": search,
"first": first,
"after": after,
"orderBy": order_by,
"orderMode": order_mode,
},
)
data = self.opencti.process_multiple(result["data"]["campaigns"])
final_data = final_data + data
return final_data
else:
return self.opencti.process_multiple(
result["data"]["campaigns"], with_pagination
)

"""
Read a Campaign object
Expand Down
32 changes: 27 additions & 5 deletions pycti/entities/opencti_course_of_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ def list(self, **kwargs):
get_all = kwargs.get("getAll", False)
with_pagination = kwargs.get("withPagination", False)
if get_all:
first = 500
first = 100

LOGGER.info("Listing Course-Of-Actions with filters %s.", json.dumps(filters))
LOGGER.info("Listing Courses-Of-Action with filters %s.", json.dumps(filters))
query = (
"""
query CoursesOfAction($filters: [CoursesOfActionFiltering], $search: String, $first: Int, $after: ID, $orderBy: CoursesOfActionOrdering, $orderMode: OrderingMode) {
Expand Down Expand Up @@ -197,9 +197,31 @@ def list(self, **kwargs):
"orderMode": order_mode,
},
)
return self.opencti.process_multiple(
result["data"]["coursesOfAction"], with_pagination
)
if get_all:
final_data = []
data = self.opencti.process_multiple(result["data"]["coursesOfAction"])
final_data = final_data + data
while result["data"]["coursesOfAction"]["pageInfo"]["hasNextPage"]:
after = result["data"]["coursesOfAction"]["pageInfo"]["endCursor"]
LOGGER.info("Listing Courses-Of-Action after " + after)
result = self.opencti.query(
query,
{
"filters": filters,
"search": search,
"first": first,
"after": after,
"orderBy": order_by,
"orderMode": order_mode,
},
)
data = self.opencti.process_multiple(result["data"]["coursesOfAction"])
final_data = final_data + data
return final_data
else:
return self.opencti.process_multiple(
result["data"]["coursesOfAction"], with_pagination
)

"""
Read a Course-Of-Action object
Expand Down
38 changes: 29 additions & 9 deletions pycti/entities/opencti_data_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from stix2.canonicalization.Canonicalize import canonicalize

from pycti.entities import LOGGER


class DataComponent:
def __init__(self, opencti):
Expand Down Expand Up @@ -173,12 +175,9 @@ def list(self, **kwargs):
get_all = kwargs.get("getAll", False)
with_pagination = kwargs.get("withPagination", False)
if get_all:
first = 500
first = 100

self.opencti.log(
"info",
"Listing Data-Components with filters " + json.dumps(filters) + ".",
)
LOGGER.info("Listing Data-Components with filters " + json.dumps(filters) + ".")
query = (
"""
query DataComponents($filters: [DataComponentsFiltering!], $search: String, $first: Int, $after: ID, $orderBy: DataComponentsOrdering, $orderMode: OrderingMode) {
Expand Down Expand Up @@ -212,10 +211,31 @@ def list(self, **kwargs):
"orderMode": order_mode,
},
)
# TODO: get_all ?
return self.opencti.process_multiple(
result["data"]["dataComponents"], with_pagination
)
if get_all:
final_data = []
data = self.opencti.process_multiple(result["data"]["dataComponents"])
final_data = final_data + data
while result["data"]["dataComponents"]["pageInfo"]["hasNextPage"]:
after = result["data"]["dataComponents"]["pageInfo"]["endCursor"]
LOGGER.info("Listing Data-Components after " + after)
result = self.opencti.query(
query,
{
"filters": filters,
"search": search,
"first": first,
"after": after,
"orderBy": order_by,
"orderMode": order_mode,
},
)
data = self.opencti.process_multiple(result["data"]["dataComponents"])
final_data = final_data + data
return final_data
else:
return self.opencti.process_multiple(
result["data"]["dataComponents"], with_pagination
)

"""
Read a Data-Component object
Expand Down
33 changes: 28 additions & 5 deletions pycti/entities/opencti_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from stix2.canonicalization.Canonicalize import canonicalize

from pycti.entities import LOGGER


class DataSource:
def __init__(self, opencti):
Expand Down Expand Up @@ -157,7 +159,7 @@ def list(self, **kwargs):
get_all = kwargs.get("getAll", False)
with_pagination = kwargs.get("withPagination", False)
if get_all:
first = 500
first = 100

self.opencti.log(
"info",
Expand Down Expand Up @@ -196,10 +198,31 @@ def list(self, **kwargs):
"orderMode": order_mode,
},
)
# TODO: get_all ?
return self.opencti.process_multiple(
result["data"]["dataSources"], with_pagination
)
if get_all:
final_data = []
data = self.opencti.process_multiple(result["data"]["dataSources"])
final_data = final_data + data
while result["data"]["dataSources"]["pageInfo"]["hasNextPage"]:
after = result["data"]["dataSources"]["pageInfo"]["endCursor"]
LOGGER.info("Listing Data-Sources after " + after)
result = self.opencti.query(
query,
{
"filters": filters,
"search": search,
"first": first,
"after": after,
"orderBy": order_by,
"orderMode": order_mode,
},
)
data = self.opencti.process_multiple(result["data"]["dataSources"])
final_data = final_data + data
return final_data
else:
return self.opencti.process_multiple(
result["data"]["dataSources"], with_pagination
)

"""
Read a Data-Source object
Expand Down
31 changes: 27 additions & 4 deletions pycti/entities/opencti_external_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def list(self, **kwargs):
get_all = kwargs.get("getAll", False)
with_pagination = kwargs.get("withPagination", False)
if get_all:
first = 500
first = 100

LOGGER.info("Listing External-Reference with filters %s.", json.dumps(filters))
query = (
Expand Down Expand Up @@ -109,9 +109,32 @@ def list(self, **kwargs):
"orderMode": order_mode,
},
)
return self.opencti.process_multiple(
result["data"]["externalReferences"], with_pagination
)
if get_all:
final_data = []
data = self.opencti.process_multiple(result["data"]["externalReferences"])
final_data = final_data + data
while result["data"]["externalReferences"]["pageInfo"]["hasNextPage"]:
after = result["data"]["externalReferences"]["pageInfo"]["endCursor"]
LOGGER.info("Listing External-References after " + after)
result = self.opencti.query(
query,
{
"filters": filters,
"first": first,
"after": after,
"orderBy": order_by,
"orderMode": order_mode,
},
)
data = self.opencti.process_multiple(
result["data"]["externalReferences"]
)
final_data = final_data + data
return final_data
else:
return self.opencti.process_multiple(
result["data"]["externalReferences"], with_pagination
)

"""
Read a External-Reference object
Expand Down
28 changes: 25 additions & 3 deletions pycti/entities/opencti_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,31 @@ def list(self, **kwargs):
"orderMode": order_mode,
},
)
return self.opencti.process_multiple(
result["data"]["identities"], with_pagination
)
if get_all:
final_data = []
data = self.opencti.process_multiple(result["data"]["identities"])
final_data = final_data + data
while result["data"]["identities"]["pageInfo"]["hasNextPage"]:
after = result["data"]["identities"]["pageInfo"]["endCursor"]
LOGGER.info("Listing Identities after " + after)
result = self.opencti.query(
query,
{
"filters": filters,
"search": search,
"first": first,
"after": after,
"orderBy": order_by,
"orderMode": order_mode,
},
)
data = self.opencti.process_multiple(result["data"]["identities"])
final_data = final_data + data
return final_data
else:
return self.opencti.process_multiple(
result["data"]["identities"], with_pagination
)

"""
Read a Identity object
Expand Down

0 comments on commit 4e97549

Please sign in to comment.