2018-04-09 19:29:53 +02:00
|
|
|
"""Python STIX 2.0 DataStore API.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-10-05 20:45:31 +02:00
|
|
|
.. autosummary::
|
2018-03-01 15:04:42 +01:00
|
|
|
:toctree: datastore
|
2017-10-05 20:45:31 +02:00
|
|
|
|
|
|
|
filesystem
|
|
|
|
filters
|
|
|
|
memory
|
|
|
|
taxii
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-10-06 02:44:58 +02:00
|
|
|
|
|
2017-05-26 21:24:33 +02:00
|
|
|
"""
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
from abc import ABCMeta, abstractmethod
|
2017-05-24 17:25:40 +02:00
|
|
|
import uuid
|
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
from six import with_metaclass
|
|
|
|
|
2018-04-13 17:08:03 +02:00
|
|
|
from stix2.datastore.filters import Filter, FilterSet
|
2017-09-29 17:24:19 +02:00
|
|
|
from stix2.utils import deduplicate
|
2017-08-09 20:49:06 +02:00
|
|
|
|
|
|
|
|
2017-05-24 17:25:40 +02:00
|
|
|
def make_id():
|
2017-05-26 21:24:33 +02:00
|
|
|
return str(uuid.uuid4())
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-08-11 14:10:20 +02:00
|
|
|
|
2018-05-16 19:23:50 +02:00
|
|
|
class DataSourceError(Exception):
|
|
|
|
"""General DataSource error instance, used primarily for wrapping
|
|
|
|
lower level errors
|
|
|
|
|
|
|
|
Args:
|
|
|
|
message (str): error message
|
|
|
|
root_exception (Exception): Exception instance of root exception
|
|
|
|
in the case that DataSourceError is wrapping a lower level or
|
|
|
|
other exception
|
|
|
|
"""
|
|
|
|
def __init__(self, message, root_exception=None):
|
|
|
|
self.message = message
|
|
|
|
self.root_exception = root_exception
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self.root_exception:
|
2018-05-22 16:03:06 +02:00
|
|
|
return "{} \"{}\"".format(self.message, self.root_exception)
|
|
|
|
else:
|
|
|
|
return self.message
|
2018-05-16 19:23:50 +02:00
|
|
|
|
|
|
|
|
2018-03-01 17:27:37 +01:00
|
|
|
class DataStoreMixin(object):
|
|
|
|
"""Provides mechanisms for storing and retrieving STIX data. The specific
|
|
|
|
behavior can be customized by subclasses.
|
2017-09-22 17:29:17 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
source (DataSource): An existing DataSource to use
|
2018-05-16 19:23:50 +02:00
|
|
|
as this DataStore's DataSource component
|
2017-09-22 17:29:17 +02:00
|
|
|
sink (DataSink): An existing DataSink to use
|
2018-05-16 19:23:50 +02:00
|
|
|
as this DataStore's DataSink component
|
2017-08-11 14:10:20 +02:00
|
|
|
|
2017-09-01 14:15:50 +02:00
|
|
|
Attributes:
|
|
|
|
id (str): A unique UUIDv4 to identify this DataStore.
|
2017-09-22 17:29:17 +02:00
|
|
|
source (DataSource): An object that implements DataSource class.
|
2017-09-01 14:15:50 +02:00
|
|
|
sink (DataSink): An object that implements DataSink class.
|
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
"""
|
2017-09-01 14:15:50 +02:00
|
|
|
def __init__(self, source=None, sink=None):
|
2018-03-01 17:27:37 +01:00
|
|
|
super(DataStoreMixin, self).__init__()
|
2017-09-01 14:15:50 +02:00
|
|
|
self.id = make_id()
|
2017-08-28 20:32:51 +02:00
|
|
|
self.source = source
|
|
|
|
self.sink = sink
|
2017-07-12 16:58:31 +02:00
|
|
|
|
2017-11-08 19:53:21 +01:00
|
|
|
def get(self, *args, **kwargs):
|
2017-09-08 17:15:10 +02:00
|
|
|
"""Retrieve the most recent version of a single STIX object by ID.
|
|
|
|
|
2017-09-22 17:29:17 +02:00
|
|
|
Translate get() call to the appropriate DataSource call.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
|
|
|
Args:
|
2017-09-22 17:29:17 +02:00
|
|
|
stix_id (str): the id of the STIX object to retrieve.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
|
|
|
Returns:
|
2017-09-22 17:29:17 +02:00
|
|
|
stix_obj: the single most recent version of the STIX
|
2017-09-08 17:15:10 +02:00
|
|
|
object specified by the "id".
|
2017-08-11 14:10:20 +02:00
|
|
|
|
2017-05-26 21:24:33 +02:00
|
|
|
"""
|
2017-11-16 22:25:57 +01:00
|
|
|
try:
|
|
|
|
return self.source.get(*args, **kwargs)
|
|
|
|
except AttributeError:
|
2018-06-30 00:38:04 +02:00
|
|
|
msg = "%s has no data source to query"
|
|
|
|
raise AttributeError(msg % self.__class__.__name__)
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-11-08 19:53:21 +01:00
|
|
|
def all_versions(self, *args, **kwargs):
|
2017-09-08 17:15:10 +02:00
|
|
|
"""Retrieve all versions of a single STIX object by ID.
|
|
|
|
|
2017-11-08 19:53:21 +01:00
|
|
|
Translate all_versions() call to the appropriate DataSource call.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
Args:
|
2017-09-22 17:29:17 +02:00
|
|
|
stix_id (str): the id of the STIX object to retrieve.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
list: All versions of the specified STIX object.
|
2017-08-11 14:10:20 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
"""
|
2017-11-16 22:25:57 +01:00
|
|
|
try:
|
|
|
|
return self.source.all_versions(*args, **kwargs)
|
|
|
|
except AttributeError:
|
2018-06-30 00:38:04 +02:00
|
|
|
msg = "%s has no data source to query"
|
|
|
|
raise AttributeError(msg % self.__class__.__name__)
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-11-08 19:53:21 +01:00
|
|
|
def query(self, *args, **kwargs):
|
2017-09-08 17:15:10 +02:00
|
|
|
"""Retrieve STIX objects matching a set of filters.
|
|
|
|
|
2017-11-08 19:53:21 +01:00
|
|
|
Translate query() call to the appropriate DataSource call.
|
2017-11-03 02:29:25 +01:00
|
|
|
|
2017-05-24 17:25:40 +02:00
|
|
|
Args:
|
2017-07-12 16:58:31 +02:00
|
|
|
query (list): a list of filters (which collectively are the query)
|
2017-08-11 14:10:20 +02:00
|
|
|
to conduct search on.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
list: The STIX objects matching the query.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
"""
|
2017-11-16 22:25:57 +01:00
|
|
|
try:
|
|
|
|
return self.source.query(*args, **kwargs)
|
|
|
|
except AttributeError:
|
2018-06-30 00:38:04 +02:00
|
|
|
msg = "%s has no data source to query"
|
|
|
|
raise AttributeError(msg % self.__class__.__name__)
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-11-21 22:29:06 +01:00
|
|
|
def creator_of(self, *args, **kwargs):
|
|
|
|
"""Retrieve the Identity refered to by the object's `created_by_ref`.
|
|
|
|
|
|
|
|
Translate creator_of() call to the appropriate DataSource call.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj: The STIX object whose `created_by_ref` property will be looked
|
|
|
|
up.
|
|
|
|
|
|
|
|
Returns:
|
2017-11-21 22:37:58 +01:00
|
|
|
The STIX object's creator, or None, if the object contains no
|
|
|
|
`created_by_ref` property or the object's creator cannot be found.
|
2017-11-21 22:29:06 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self.source.creator_of(*args, **kwargs)
|
|
|
|
except AttributeError:
|
2018-06-30 00:38:04 +02:00
|
|
|
msg = "%s has no data source to query"
|
|
|
|
raise AttributeError(msg % self.__class__.__name__)
|
2017-11-21 22:29:06 +01:00
|
|
|
|
2017-11-15 16:37:17 +01:00
|
|
|
def relationships(self, *args, **kwargs):
|
|
|
|
"""Retrieve Relationships involving the given STIX object.
|
|
|
|
|
|
|
|
Translate relationships() call to the appropriate DataSource call.
|
|
|
|
|
|
|
|
Only one of `source_only` and `target_only` may be `True`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj (STIX object OR dict OR str): The STIX object (or its ID) whose
|
|
|
|
relationships will be looked up.
|
|
|
|
relationship_type (str): Only retrieve Relationships of this type.
|
2018-05-22 16:03:06 +02:00
|
|
|
If None, all relationships will be returned, regardless of type.
|
2017-11-15 16:37:17 +01:00
|
|
|
source_only (bool): Only retrieve Relationships for which this
|
|
|
|
object is the source_ref. Default: False.
|
|
|
|
target_only (bool): Only retrieve Relationships for which this
|
|
|
|
object is the target_ref. Default: False.
|
|
|
|
|
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
list: The Relationship objects involving the given STIX object.
|
2017-11-15 16:37:17 +01:00
|
|
|
|
|
|
|
"""
|
2017-11-16 22:25:57 +01:00
|
|
|
try:
|
|
|
|
return self.source.relationships(*args, **kwargs)
|
|
|
|
except AttributeError:
|
2018-06-30 00:38:04 +02:00
|
|
|
msg = "%s has no data source to query"
|
|
|
|
raise AttributeError(msg % self.__class__.__name__)
|
2017-11-15 16:37:17 +01:00
|
|
|
|
2017-11-16 20:58:59 +01:00
|
|
|
def related_to(self, *args, **kwargs):
|
|
|
|
"""Retrieve STIX Objects that have a Relationship involving the given
|
|
|
|
STIX object.
|
|
|
|
|
|
|
|
Translate related_to() call to the appropriate DataSource call.
|
|
|
|
|
|
|
|
Only one of `source_only` and `target_only` may be `True`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj (STIX object OR dict OR str): The STIX object (or its ID) whose
|
|
|
|
related objects will be looked up.
|
|
|
|
relationship_type (str): Only retrieve objects related by this
|
2017-11-17 23:30:24 +01:00
|
|
|
Relationships type. If None, all related objects will be
|
|
|
|
returned, regardless of type.
|
2017-11-16 20:58:59 +01:00
|
|
|
source_only (bool): Only examine Relationships for which this
|
|
|
|
object is the source_ref. Default: False.
|
|
|
|
target_only (bool): Only examine Relationships for which this
|
|
|
|
object is the target_ref. Default: False.
|
2018-03-30 17:53:15 +02:00
|
|
|
filters (list): list of additional filters the related objects must
|
|
|
|
match.
|
2017-11-16 20:58:59 +01:00
|
|
|
|
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
list: The STIX objects related to the given STIX object.
|
2017-11-16 20:58:59 +01:00
|
|
|
|
|
|
|
"""
|
2017-11-16 22:25:57 +01:00
|
|
|
try:
|
|
|
|
return self.source.related_to(*args, **kwargs)
|
|
|
|
except AttributeError:
|
2018-06-30 00:38:04 +02:00
|
|
|
msg = "%s has no data source to query"
|
|
|
|
raise AttributeError(msg % self.__class__.__name__)
|
2017-11-16 20:58:59 +01:00
|
|
|
|
2017-11-08 19:53:21 +01:00
|
|
|
def add(self, *args, **kwargs):
|
2017-11-03 02:29:25 +01:00
|
|
|
"""Method for storing STIX objects.
|
2017-09-08 17:15:10 +02:00
|
|
|
|
2018-03-30 17:53:15 +02:00
|
|
|
Defines custom behavior before storing STIX objects using the
|
|
|
|
appropriate method call on the associated DataSink.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-09-08 17:15:10 +02:00
|
|
|
Args:
|
2017-09-22 17:29:17 +02:00
|
|
|
stix_objs (list): a list of STIX objects
|
2017-11-03 02:29:25 +01:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
"""
|
2017-11-16 22:25:57 +01:00
|
|
|
try:
|
|
|
|
return self.sink.add(*args, **kwargs)
|
|
|
|
except AttributeError:
|
2018-06-30 00:38:04 +02:00
|
|
|
msg = "%s has no data sink to put objects in"
|
|
|
|
raise AttributeError(msg % self.__class__.__name__)
|
2017-05-24 17:25:40 +02:00
|
|
|
|
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
class DataSink(with_metaclass(ABCMeta)):
|
2017-09-29 17:24:19 +02:00
|
|
|
"""An implementer will create a concrete subclass from
|
2017-09-22 17:29:17 +02:00
|
|
|
this class for the specific DataSink.
|
2017-08-11 14:10:20 +02:00
|
|
|
|
|
|
|
Attributes:
|
2017-09-01 14:15:50 +02:00
|
|
|
id (str): A unique UUIDv4 to identify this DataSink.
|
2017-08-11 14:10:20 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
"""
|
2017-09-01 14:15:50 +02:00
|
|
|
def __init__(self):
|
2017-11-03 02:29:25 +01:00
|
|
|
super(DataSink, self).__init__()
|
2017-09-01 14:15:50 +02:00
|
|
|
self.id = make_id()
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
@abstractmethod
|
2017-11-08 19:53:21 +01:00
|
|
|
def add(self, stix_objs):
|
2017-11-03 02:29:25 +01:00
|
|
|
"""Method for storing STIX objects.
|
2017-09-08 17:15:10 +02:00
|
|
|
|
2017-09-22 17:29:17 +02:00
|
|
|
Implement: Specific data sink API calls, processing,
|
|
|
|
functionality required for adding data to the sink
|
2017-08-11 14:10:20 +02:00
|
|
|
|
2017-09-08 17:15:10 +02:00
|
|
|
Args:
|
|
|
|
stix_objs (list): a list of STIX objects (where each object is a
|
|
|
|
STIX object)
|
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
"""
|
2017-05-24 17:25:40 +02:00
|
|
|
|
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
class DataSource(with_metaclass(ABCMeta)):
|
2017-09-29 17:24:19 +02:00
|
|
|
"""An implementer will create a concrete subclass from
|
2017-09-22 17:29:17 +02:00
|
|
|
this class for the specific DataSource.
|
2017-08-11 14:10:20 +02:00
|
|
|
|
|
|
|
Attributes:
|
2017-09-01 14:15:50 +02:00
|
|
|
id (str): A unique UUIDv4 to identify this DataSource.
|
2018-04-11 19:36:52 +02:00
|
|
|
filters (FilterSet): A collection of filters attached to this DataSource.
|
2017-08-11 14:10:20 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
"""
|
2017-09-01 14:15:50 +02:00
|
|
|
def __init__(self):
|
2017-11-03 02:29:25 +01:00
|
|
|
super(DataSource, self).__init__()
|
2017-09-01 14:15:50 +02:00
|
|
|
self.id = make_id()
|
2018-04-11 19:36:52 +02:00
|
|
|
self.filters = FilterSet()
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
@abstractmethod
|
2017-11-08 19:53:21 +01:00
|
|
|
def get(self, stix_id):
|
2017-07-12 16:58:31 +02:00
|
|
|
"""
|
2017-09-22 17:29:17 +02:00
|
|
|
Implement: Specific data source API calls, processing,
|
|
|
|
functionality required for retrieving data from the data source
|
2017-05-24 17:25:40 +02:00
|
|
|
|
|
|
|
Args:
|
2017-07-12 16:58:31 +02:00
|
|
|
stix_id (str): the id of the STIX 2.0 object to retrieve. Should
|
|
|
|
return a single object, the most recent version of the object
|
|
|
|
specified by the "id".
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
stix_obj: The STIX object.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
"""
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
@abstractmethod
|
2017-11-08 19:53:21 +01:00
|
|
|
def all_versions(self, stix_id):
|
2017-07-12 16:58:31 +02:00
|
|
|
"""
|
2017-11-03 02:29:25 +01:00
|
|
|
Implement: Similar to get() except returns list of all object versions
|
|
|
|
of the specified "id". In addition, implement the specific data
|
2017-09-22 17:29:17 +02:00
|
|
|
source API calls, processing, functionality required for retrieving
|
|
|
|
data from the data source.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
|
|
|
Args:
|
2017-08-11 14:10:20 +02:00
|
|
|
stix_id (str): The id of the STIX 2.0 object to retrieve. Should
|
2017-07-12 16:58:31 +02:00
|
|
|
return a list of objects, all the versions of the object
|
|
|
|
specified by the "id".
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
list: All versions of the specified STIX object.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-08-11 14:10:20 +02:00
|
|
|
"""
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
@abstractmethod
|
2017-11-08 19:53:21 +01:00
|
|
|
def query(self, query=None):
|
2017-07-12 16:58:31 +02:00
|
|
|
"""
|
2017-11-03 02:29:25 +01:00
|
|
|
Implement: The specific data source API calls, processing,
|
2017-09-22 17:29:17 +02:00
|
|
|
functionality required for retrieving query from the data source
|
2017-05-24 17:25:40 +02:00
|
|
|
|
|
|
|
Args:
|
2017-07-12 16:58:31 +02:00
|
|
|
query (list): a list of filters (which collectively are the query)
|
2017-11-03 02:29:25 +01:00
|
|
|
to conduct search on.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
list: The STIX objects that matched the query.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-05-26 21:24:33 +02:00
|
|
|
"""
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-11-21 22:29:06 +01:00
|
|
|
def creator_of(self, obj):
|
|
|
|
"""Retrieve the Identity refered to by the object's `created_by_ref`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj: The STIX object whose `created_by_ref` property will be looked
|
|
|
|
up.
|
|
|
|
|
|
|
|
Returns:
|
2017-11-21 22:37:58 +01:00
|
|
|
The STIX object's creator, or None, if the object contains no
|
|
|
|
`created_by_ref` property or the object's creator cannot be found.
|
2017-11-21 22:29:06 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
creator_id = obj.get('created_by_ref', '')
|
|
|
|
if creator_id:
|
|
|
|
return self.get(creator_id)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2017-11-15 16:37:17 +01:00
|
|
|
def relationships(self, obj, relationship_type=None, source_only=False, target_only=False):
|
2017-11-16 22:25:57 +01:00
|
|
|
"""Retrieve Relationships involving the given STIX object.
|
2017-11-15 16:37:17 +01:00
|
|
|
|
|
|
|
Only one of `source_only` and `target_only` may be `True`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj (STIX object OR dict OR str): The STIX object (or its ID) whose
|
|
|
|
relationships will be looked up.
|
|
|
|
relationship_type (str): Only retrieve Relationships of this type.
|
2017-11-17 23:30:24 +01:00
|
|
|
If None, all relationships will be returned, regardless of type.
|
2017-11-15 16:37:17 +01:00
|
|
|
source_only (bool): Only retrieve Relationships for which this
|
|
|
|
object is the source_ref. Default: False.
|
|
|
|
target_only (bool): Only retrieve Relationships for which this
|
|
|
|
object is the target_ref. Default: False.
|
|
|
|
|
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
list: The Relationship objects involving the given STIX object.
|
2017-11-15 16:37:17 +01:00
|
|
|
|
|
|
|
"""
|
2017-11-16 22:25:57 +01:00
|
|
|
results = []
|
|
|
|
filters = [Filter('type', '=', 'relationship')]
|
|
|
|
|
|
|
|
try:
|
2017-11-21 16:29:57 +01:00
|
|
|
obj_id = obj['id']
|
|
|
|
except KeyError:
|
|
|
|
raise ValueError("STIX object has no 'id' property")
|
|
|
|
except TypeError:
|
|
|
|
# Assume `obj` is an ID string
|
2017-11-16 22:25:57 +01:00
|
|
|
obj_id = obj
|
|
|
|
|
|
|
|
if relationship_type:
|
|
|
|
filters.append(Filter('relationship_type', '=', relationship_type))
|
|
|
|
|
|
|
|
if source_only and target_only:
|
|
|
|
raise ValueError("Search either source only or target only, but not both")
|
|
|
|
|
|
|
|
if not target_only:
|
|
|
|
results.extend(self.query(filters + [Filter('source_ref', '=', obj_id)]))
|
|
|
|
if not source_only:
|
|
|
|
results.extend(self.query(filters + [Filter('target_ref', '=', obj_id)]))
|
|
|
|
|
|
|
|
return results
|
2017-11-15 16:37:17 +01:00
|
|
|
|
2018-04-05 16:07:35 +02:00
|
|
|
def related_to(self, obj, relationship_type=None, source_only=False, target_only=False, filters=None):
|
2017-11-16 20:58:59 +01:00
|
|
|
"""Retrieve STIX Objects that have a Relationship involving the given
|
|
|
|
STIX object.
|
|
|
|
|
|
|
|
Only one of `source_only` and `target_only` may be `True`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj (STIX object OR dict OR str): The STIX object (or its ID) whose
|
|
|
|
related objects will be looked up.
|
|
|
|
relationship_type (str): Only retrieve objects related by this
|
2017-11-17 23:30:24 +01:00
|
|
|
Relationships type. If None, all related objects will be
|
|
|
|
returned, regardless of type.
|
2017-11-16 20:58:59 +01:00
|
|
|
source_only (bool): Only examine Relationships for which this
|
|
|
|
object is the source_ref. Default: False.
|
|
|
|
target_only (bool): Only examine Relationships for which this
|
|
|
|
object is the target_ref. Default: False.
|
2018-03-19 20:56:20 +01:00
|
|
|
filters (list): list of additional filters the related objects must
|
|
|
|
match.
|
2017-11-16 20:58:59 +01:00
|
|
|
|
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
list: The STIX objects related to the given STIX object.
|
2017-11-16 20:58:59 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
results = []
|
|
|
|
rels = self.relationships(obj, relationship_type, source_only, target_only)
|
|
|
|
|
|
|
|
try:
|
2017-11-21 16:29:57 +01:00
|
|
|
obj_id = obj['id']
|
|
|
|
except TypeError:
|
|
|
|
# Assume `obj` is an ID string
|
2017-11-16 20:58:59 +01:00
|
|
|
obj_id = obj
|
|
|
|
|
2017-11-21 22:19:15 +01:00
|
|
|
# Get all unique ids from the relationships except that of the object
|
|
|
|
ids = set()
|
2017-11-16 20:58:59 +01:00
|
|
|
for r in rels:
|
2017-11-21 22:19:15 +01:00
|
|
|
ids.update((r.source_ref, r.target_ref))
|
2018-03-30 17:53:15 +02:00
|
|
|
ids.discard(obj_id)
|
2017-11-21 22:19:15 +01:00
|
|
|
|
2018-03-19 20:56:20 +01:00
|
|
|
# Assemble filters
|
2018-04-13 17:08:03 +02:00
|
|
|
filter_list = FilterSet(filters)
|
2017-11-21 22:19:15 +01:00
|
|
|
|
|
|
|
for i in ids:
|
2018-04-13 17:08:03 +02:00
|
|
|
results.extend(self.query([f for f in filter_list] + [Filter('id', '=', i)]))
|
2017-11-16 20:58:59 +01:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-08-16 15:58:33 +02:00
|
|
|
class CompositeDataSource(DataSource):
|
2017-09-29 17:24:19 +02:00
|
|
|
"""Controller for all the attached DataSources.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-09-22 17:29:17 +02:00
|
|
|
A user can have a single CompositeDataSource as an interface
|
|
|
|
the a set of DataSources. When an API call is made to the
|
|
|
|
CompositeDataSource, it is delegated to each of the (real)
|
|
|
|
DataSources that are attached to it.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-09-22 17:29:17 +02:00
|
|
|
DataSources can be attached to CompositeDataSource for a variety
|
|
|
|
of reasons, e.g. common filters, organization, less API calls.
|
2017-08-11 14:10:20 +02:00
|
|
|
|
|
|
|
Attributes:
|
2017-09-22 17:29:17 +02:00
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
data_sources (list): A dictionary of DataSource objects; to be
|
2017-08-11 14:10:20 +02:00
|
|
|
controlled and used by the Data Source Controller object.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-05-26 21:24:33 +02:00
|
|
|
"""
|
2017-09-01 14:15:50 +02:00
|
|
|
def __init__(self):
|
2017-09-06 22:20:16 +02:00
|
|
|
"""Create a new STIX Data Source.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
Args:
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
"""
|
2017-09-01 14:15:50 +02:00
|
|
|
super(CompositeDataSource, self).__init__()
|
2017-09-29 17:24:19 +02:00
|
|
|
self.data_sources = []
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
def get(self, stix_id, _composite_filters=None):
|
2017-09-22 17:29:17 +02:00
|
|
|
"""Retrieve STIX object by STIX ID
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-09-22 17:29:17 +02:00
|
|
|
Federated retrieve method, iterates through all DataSources
|
2017-07-12 16:58:31 +02:00
|
|
|
defined in the "data_sources" parameter. Each data source has a
|
|
|
|
specific API retrieve-like function and associated parameters. This
|
|
|
|
function does a federated retrieval and consolidation of the data
|
|
|
|
returned from all the STIX data sources.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-09-22 17:29:17 +02:00
|
|
|
A composite data source will pass its attached filters to
|
|
|
|
each configured data source, pushing filtering to them to handle.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
Args:
|
2017-08-11 14:10:20 +02:00
|
|
|
stix_id (str): the id of the STIX object to retrieve.
|
2018-04-11 19:36:52 +02:00
|
|
|
_composite_filters (FilterSet): a collection of filters passed from a
|
2017-09-22 17:29:17 +02:00
|
|
|
CompositeDataSource (i.e. if this CompositeDataSource is attached
|
2017-11-03 02:29:25 +01:00
|
|
|
to another parent CompositeDataSource), not user supplied.
|
2017-08-16 15:58:33 +02:00
|
|
|
|
2017-05-24 17:25:40 +02:00
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
stix_obj: The STIX object to be returned.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-05-26 21:24:33 +02:00
|
|
|
"""
|
2017-09-29 17:24:19 +02:00
|
|
|
if not self.has_data_sources():
|
2018-06-30 00:38:04 +02:00
|
|
|
raise AttributeError("CompositeDataSource has no data sources")
|
2017-09-08 15:01:12 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
all_data = []
|
2018-04-11 19:36:52 +02:00
|
|
|
all_filters = FilterSet()
|
|
|
|
|
|
|
|
all_filters.add(self.filters)
|
2017-09-22 17:29:17 +02:00
|
|
|
|
|
|
|
if _composite_filters:
|
2018-04-11 19:36:52 +02:00
|
|
|
all_filters.add(_composite_filters)
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
# for every configured Data Source, call its retrieve handler
|
2017-09-29 17:24:19 +02:00
|
|
|
for ds in self.data_sources:
|
2017-11-03 02:29:25 +01:00
|
|
|
data = ds.get(stix_id=stix_id, _composite_filters=all_filters)
|
2017-10-04 21:57:38 +02:00
|
|
|
if data:
|
|
|
|
all_data.append(data)
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
# remove duplicate versions
|
|
|
|
if len(all_data) > 0:
|
2017-09-22 17:29:17 +02:00
|
|
|
all_data = deduplicate(all_data)
|
2017-10-24 20:20:42 +02:00
|
|
|
else:
|
|
|
|
return None
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
# reduce to most recent version
|
2017-08-28 20:32:51 +02:00
|
|
|
stix_obj = sorted(all_data, key=lambda k: k['modified'], reverse=True)[0]
|
2017-07-12 16:58:31 +02:00
|
|
|
|
|
|
|
return stix_obj
|
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
def all_versions(self, stix_id, _composite_filters=None):
|
|
|
|
"""Retrieve all versions of a STIX object by STIX ID.
|
2017-07-12 16:58:31 +02:00
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
Federated all_versions retrieve method - iterates through all
|
|
|
|
DataSources defined in "data_sources".
|
2017-07-12 16:58:31 +02:00
|
|
|
|
2017-09-22 17:29:17 +02:00
|
|
|
A composite data source will pass its attached filters to
|
2017-11-03 02:29:25 +01:00
|
|
|
each configured data source, pushing filtering to them to handle.
|
2017-07-12 16:58:31 +02:00
|
|
|
|
|
|
|
Args:
|
2017-11-03 02:29:25 +01:00
|
|
|
stix_id (str): id of the STIX objects to retrieve.
|
2018-04-11 19:36:52 +02:00
|
|
|
_composite_filters (FilterSet): a collection of filters passed from a
|
2017-11-03 02:29:25 +01:00
|
|
|
CompositeDataSource (i.e. if this CompositeDataSource is
|
|
|
|
attached to a parent CompositeDataSource), not user supplied.
|
2017-08-16 15:58:33 +02:00
|
|
|
|
2017-05-24 17:25:40 +02:00
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
list: The STIX objects that have the specified id.
|
2017-08-11 14:10:20 +02:00
|
|
|
|
2017-05-26 21:24:33 +02:00
|
|
|
"""
|
2017-09-29 17:24:19 +02:00
|
|
|
if not self.has_data_sources():
|
2018-06-30 00:38:04 +02:00
|
|
|
raise AttributeError("CompositeDataSource has no data sources")
|
2017-09-08 15:01:12 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
all_data = []
|
2018-04-11 19:36:52 +02:00
|
|
|
all_filters = FilterSet()
|
2017-09-22 17:29:17 +02:00
|
|
|
|
2018-04-11 19:36:52 +02:00
|
|
|
all_filters.add(self.filters)
|
2017-08-28 20:32:51 +02:00
|
|
|
|
|
|
|
if _composite_filters:
|
2018-04-11 19:36:52 +02:00
|
|
|
all_filters.add(_composite_filters)
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
# retrieve STIX objects from all configured data sources
|
2017-09-29 17:24:19 +02:00
|
|
|
for ds in self.data_sources:
|
2017-11-03 02:29:25 +01:00
|
|
|
data = ds.all_versions(stix_id=stix_id, _composite_filters=all_filters)
|
2017-07-12 16:58:31 +02:00
|
|
|
all_data.extend(data)
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
# remove exact duplicates (where duplicates are STIX 2.0 objects
|
|
|
|
# with the same 'id' and 'modified' values)
|
|
|
|
if len(all_data) > 0:
|
2017-09-22 17:29:17 +02:00
|
|
|
all_data = deduplicate(all_data)
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
return all_data
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-11-03 02:29:25 +01:00
|
|
|
def query(self, query=None, _composite_filters=None):
|
|
|
|
"""Retrieve STIX objects that match a query.
|
2017-09-22 17:29:17 +02:00
|
|
|
|
|
|
|
Federate the query to all DataSources attached to the
|
2017-08-11 14:10:20 +02:00
|
|
|
Composite Data Source.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
Args:
|
2017-11-03 02:29:25 +01:00
|
|
|
query (list): list of filters to search on.
|
2018-04-11 19:36:52 +02:00
|
|
|
_composite_filters (FilterSet): a collection of filters passed from a
|
2017-11-03 02:29:25 +01:00
|
|
|
CompositeDataSource (i.e. if this CompositeDataSource is
|
|
|
|
attached to a parent CompositeDataSource), not user supplied.
|
2017-07-12 16:58:31 +02:00
|
|
|
|
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
list: The STIX objects to be returned.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-05-26 21:24:33 +02:00
|
|
|
"""
|
2017-09-29 17:24:19 +02:00
|
|
|
if not self.has_data_sources():
|
2018-06-30 00:38:04 +02:00
|
|
|
raise AttributeError("CompositeDataSource has no data sources")
|
2017-09-08 15:01:12 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
if not query:
|
2018-04-11 19:36:52 +02:00
|
|
|
# don't mess with the query (i.e. deduplicate, as that's done
|
2017-09-22 17:29:17 +02:00
|
|
|
# within the specific DataSources that are called)
|
2017-07-12 16:58:31 +02:00
|
|
|
query = []
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
all_data = []
|
2018-04-11 19:36:52 +02:00
|
|
|
all_filters = FilterSet()
|
2017-09-22 17:29:17 +02:00
|
|
|
|
2018-04-11 19:36:52 +02:00
|
|
|
all_filters.add(self.filters)
|
2017-08-28 20:32:51 +02:00
|
|
|
|
|
|
|
if _composite_filters:
|
2018-04-11 19:36:52 +02:00
|
|
|
all_filters.add(_composite_filters)
|
2017-07-12 16:58:31 +02:00
|
|
|
|
|
|
|
# federate query to all attached data sources,
|
|
|
|
# pass composite filters to id
|
2017-09-29 17:24:19 +02:00
|
|
|
for ds in self.data_sources:
|
2017-11-03 02:29:25 +01:00
|
|
|
data = ds.query(query=query, _composite_filters=all_filters)
|
2017-07-12 16:58:31 +02:00
|
|
|
all_data.extend(data)
|
|
|
|
|
|
|
|
# remove exact duplicates (where duplicates are STIX 2.0
|
|
|
|
# objects with the same 'id' and 'modified' values)
|
|
|
|
if len(all_data) > 0:
|
2017-09-22 17:29:17 +02:00
|
|
|
all_data = deduplicate(all_data)
|
2017-07-12 16:58:31 +02:00
|
|
|
|
|
|
|
return all_data
|
|
|
|
|
2017-11-21 21:57:35 +01:00
|
|
|
def relationships(self, *args, **kwargs):
|
2017-11-15 16:37:17 +01:00
|
|
|
"""Retrieve Relationships involving the given STIX object.
|
|
|
|
|
|
|
|
Only one of `source_only` and `target_only` may be `True`.
|
|
|
|
|
|
|
|
Federated relationships retrieve method - iterates through all
|
|
|
|
DataSources defined in "data_sources".
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj (STIX object OR dict OR str): The STIX object (or its ID) whose
|
|
|
|
relationships will be looked up.
|
|
|
|
relationship_type (str): Only retrieve Relationships of this type.
|
2017-11-17 23:30:24 +01:00
|
|
|
If None, all relationships will be returned, regardless of type.
|
2017-11-15 16:37:17 +01:00
|
|
|
source_only (bool): Only retrieve Relationships for which this
|
|
|
|
object is the source_ref. Default: False.
|
|
|
|
target_only (bool): Only retrieve Relationships for which this
|
|
|
|
object is the target_ref. Default: False.
|
|
|
|
|
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
list: The Relationship objects involving the given STIX object.
|
2017-11-15 16:37:17 +01:00
|
|
|
|
|
|
|
"""
|
2017-11-16 22:25:57 +01:00
|
|
|
if not self.has_data_sources():
|
2018-06-30 00:38:04 +02:00
|
|
|
raise AttributeError("CompositeDataSource has no data sources")
|
2017-11-16 22:25:57 +01:00
|
|
|
|
2017-11-15 16:37:17 +01:00
|
|
|
results = []
|
|
|
|
for ds in self.data_sources:
|
2017-11-21 21:57:35 +01:00
|
|
|
results.extend(ds.relationships(*args, **kwargs))
|
|
|
|
|
|
|
|
# remove exact duplicates (where duplicates are STIX 2.0
|
|
|
|
# objects with the same 'id' and 'modified' values)
|
|
|
|
if len(results) > 0:
|
|
|
|
results = deduplicate(results)
|
2017-11-15 16:37:17 +01:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
2017-11-21 21:57:35 +01:00
|
|
|
def related_to(self, *args, **kwargs):
|
2017-11-16 22:25:57 +01:00
|
|
|
"""Retrieve STIX Objects that have a Relationship involving the given
|
|
|
|
STIX object.
|
|
|
|
|
|
|
|
Only one of `source_only` and `target_only` may be `True`.
|
|
|
|
|
|
|
|
Federated related objects method - iterates through all
|
|
|
|
DataSources defined in "data_sources".
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj (STIX object OR dict OR str): The STIX object (or its ID) whose
|
|
|
|
related objects will be looked up.
|
|
|
|
relationship_type (str): Only retrieve objects related by this
|
2017-11-17 23:30:24 +01:00
|
|
|
Relationships type. If None, all related objects will be
|
|
|
|
returned, regardless of type.
|
2017-11-16 22:25:57 +01:00
|
|
|
source_only (bool): Only examine Relationships for which this
|
|
|
|
object is the source_ref. Default: False.
|
|
|
|
target_only (bool): Only examine Relationships for which this
|
|
|
|
object is the target_ref. Default: False.
|
2018-03-30 17:53:15 +02:00
|
|
|
filters (list): list of additional filters the related objects must
|
|
|
|
match.
|
2017-11-16 22:25:57 +01:00
|
|
|
|
|
|
|
Returns:
|
2018-03-16 16:40:46 +01:00
|
|
|
list: The STIX objects related to the given STIX object.
|
2017-11-16 22:25:57 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
if not self.has_data_sources():
|
2018-06-30 00:38:04 +02:00
|
|
|
raise AttributeError("CompositeDataSource has no data sources")
|
2017-11-16 22:25:57 +01:00
|
|
|
|
|
|
|
results = []
|
|
|
|
for ds in self.data_sources:
|
2017-11-21 21:57:35 +01:00
|
|
|
results.extend(ds.related_to(*args, **kwargs))
|
2017-11-16 22:25:57 +01:00
|
|
|
|
2017-11-21 21:57:35 +01:00
|
|
|
# remove exact duplicates (where duplicates are STIX 2.0
|
|
|
|
# objects with the same 'id' and 'modified' values)
|
|
|
|
if len(results) > 0:
|
|
|
|
results = deduplicate(results)
|
2017-11-16 22:25:57 +01:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
2017-09-29 17:24:19 +02:00
|
|
|
def add_data_source(self, data_source):
|
|
|
|
"""Attach a DataSource to CompositeDataSource instance
|
2017-07-12 16:58:31 +02:00
|
|
|
|
|
|
|
Args:
|
2017-09-29 17:24:19 +02:00
|
|
|
data_source (DataSource): a stix2.DataSource to attach
|
2017-09-22 17:29:17 +02:00
|
|
|
to the CompositeDataSource
|
2017-07-12 16:58:31 +02:00
|
|
|
|
2017-05-26 21:24:33 +02:00
|
|
|
"""
|
2017-09-29 17:24:19 +02:00
|
|
|
if issubclass(data_source.__class__, DataSource):
|
|
|
|
if data_source.id not in [ds_.id for ds_ in self.data_sources]:
|
|
|
|
# check DataSource not already attached CompositeDataSource
|
|
|
|
self.data_sources.append(data_source)
|
|
|
|
else:
|
|
|
|
raise TypeError("DataSource (to be added) is not of type stix2.DataSource. DataSource type is '%s'" % type(data_source))
|
|
|
|
|
|
|
|
return
|
2017-07-12 16:58:31 +02:00
|
|
|
|
2017-09-29 17:24:19 +02:00
|
|
|
def add_data_sources(self, data_sources):
|
|
|
|
"""Attach list of DataSources to CompositeDataSource instance
|
2017-07-12 16:58:31 +02:00
|
|
|
|
2017-09-29 17:24:19 +02:00
|
|
|
Args:
|
|
|
|
data_sources (list): stix2.DataSources to attach to
|
|
|
|
CompositeDataSource
|
|
|
|
"""
|
|
|
|
for ds in data_sources:
|
|
|
|
self.add_data_source(ds)
|
2017-07-12 16:58:31 +02:00
|
|
|
return
|
|
|
|
|
2017-09-29 17:24:19 +02:00
|
|
|
def remove_data_source(self, data_source_id):
|
2017-09-22 17:29:17 +02:00
|
|
|
"""Remove DataSource from the CompositeDataSource instance
|
2017-07-12 16:58:31 +02:00
|
|
|
|
2017-05-24 17:25:40 +02:00
|
|
|
Args:
|
2017-09-29 17:24:19 +02:00
|
|
|
data_source_id (str): DataSource IDs.
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-05-26 21:24:33 +02:00
|
|
|
"""
|
2017-09-29 17:24:19 +02:00
|
|
|
def _match(ds_id, candidate_ds_id):
|
|
|
|
return ds_id == candidate_ds_id
|
|
|
|
|
|
|
|
self.data_sources[:] = [ds for ds in self.data_sources if not _match(ds.id, data_source_id)]
|
|
|
|
|
2017-07-12 16:58:31 +02:00
|
|
|
return
|
2017-05-24 17:25:40 +02:00
|
|
|
|
2017-09-29 17:24:19 +02:00
|
|
|
def remove_data_sources(self, data_source_ids):
|
|
|
|
"""Remove DataSources from the CompositeDataSource instance
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data_source_ids (list): DataSource IDs
|
2017-07-12 16:58:31 +02:00
|
|
|
|
|
|
|
"""
|
2017-09-29 17:24:19 +02:00
|
|
|
for ds_id in data_source_ids:
|
|
|
|
self.remove_data_source(ds_id)
|
|
|
|
return
|
|
|
|
|
|
|
|
def has_data_sources(self):
|
|
|
|
return len(self.data_sources)
|
|
|
|
|
2017-08-28 20:32:51 +02:00
|
|
|
def get_all_data_sources(self):
|
2017-09-29 17:24:19 +02:00
|
|
|
return self.data_sources
|