2017-07-18 18:05:19 +02:00
|
|
|
import copy
|
|
|
|
|
2017-09-08 18:39:36 +02:00
|
|
|
from .core import parse as _parse
|
2017-09-29 17:24:19 +02:00
|
|
|
from .sources import CompositeDataSource, DataStore
|
2017-07-12 17:36:15 +02:00
|
|
|
|
2017-07-17 21:21:49 +02:00
|
|
|
|
2017-09-06 22:20:16 +02:00
|
|
|
class ObjectFactory(object):
|
|
|
|
"""Easily create STIX objects with default values for certain properties.
|
2017-07-17 21:21:49 +02:00
|
|
|
|
|
|
|
Args:
|
2017-09-08 17:15:10 +02:00
|
|
|
created_by_ref (optional): Default created_by_ref value to apply to all
|
2017-07-17 21:21:49 +02:00
|
|
|
objects created by this factory.
|
2017-09-08 17:15:10 +02:00
|
|
|
created (optional): Default created value to apply to all
|
2017-07-17 21:21:49 +02:00
|
|
|
objects created by this factory.
|
2017-09-08 17:15:10 +02:00
|
|
|
external_references (optional): Default `external_references` value to apply
|
2017-07-17 21:21:49 +02:00
|
|
|
to all objects created by this factory.
|
2017-09-08 17:15:10 +02:00
|
|
|
object_marking_refs (optional): Default `object_marking_refs` value to apply
|
2017-07-17 21:21:49 +02:00
|
|
|
to all objects created by this factory.
|
2017-09-08 17:15:10 +02:00
|
|
|
list_append (bool, optional): When a default is set for a list property like
|
2017-07-17 21:21:49 +02:00
|
|
|
`external_references` or `object_marking_refs` and a value for
|
|
|
|
that property is passed into `create()`, if this is set to True,
|
|
|
|
that value will be added to the list alongside the default. If
|
|
|
|
this is set to False, the passed in value will replace the
|
|
|
|
default. Defaults to True.
|
|
|
|
"""
|
2017-07-12 17:36:15 +02:00
|
|
|
|
2017-07-13 15:45:43 +02:00
|
|
|
def __init__(self, created_by_ref=None, created=None,
|
2017-07-17 20:56:13 +02:00
|
|
|
external_references=None, object_marking_refs=None,
|
|
|
|
list_append=True):
|
2017-07-13 17:03:31 +02:00
|
|
|
|
|
|
|
self._defaults = {}
|
|
|
|
if created_by_ref:
|
|
|
|
self._defaults['created_by_ref'] = created_by_ref
|
|
|
|
if created:
|
|
|
|
self._defaults['created'] = created
|
|
|
|
# If the user provides a default "created" time, we also want to use
|
|
|
|
# that as the modified time.
|
|
|
|
self._defaults['modified'] = created
|
|
|
|
if external_references:
|
|
|
|
self._defaults['external_references'] = external_references
|
|
|
|
if object_marking_refs:
|
|
|
|
self._defaults['object_marking_refs'] = object_marking_refs
|
2017-07-17 20:56:13 +02:00
|
|
|
self._list_append = list_append
|
|
|
|
self._list_properties = ['external_references', 'object_marking_refs']
|
2017-07-12 17:36:15 +02:00
|
|
|
|
2017-07-12 21:22:50 +02:00
|
|
|
def create(self, cls, **kwargs):
|
2017-09-08 17:15:10 +02:00
|
|
|
"""Create a STIX object using object factory defaults.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
cls: the python-stix2 class of the object to be created (eg. Indicator)
|
|
|
|
**kwargs: The property/value pairs of the STIX object to be created
|
|
|
|
"""
|
|
|
|
|
2017-07-13 17:03:31 +02:00
|
|
|
# Use self.defaults as the base, but update with any explicit args
|
|
|
|
# provided by the user.
|
2017-07-18 18:05:19 +02:00
|
|
|
properties = copy.deepcopy(self._defaults)
|
2017-07-13 17:03:31 +02:00
|
|
|
if kwargs:
|
2017-07-17 20:56:13 +02:00
|
|
|
if self._list_append:
|
|
|
|
# Append provided items to list properties instead of replacing them
|
|
|
|
for list_prop in set(self._list_properties).intersection(kwargs.keys(), properties.keys()):
|
|
|
|
kwarg_prop = kwargs.pop(list_prop)
|
|
|
|
if kwarg_prop is None:
|
|
|
|
del properties[list_prop]
|
|
|
|
continue
|
|
|
|
if not isinstance(properties[list_prop], list):
|
|
|
|
properties[list_prop] = [properties[list_prop]]
|
2017-07-18 18:05:19 +02:00
|
|
|
|
|
|
|
if isinstance(kwarg_prop, list):
|
|
|
|
properties[list_prop].extend(kwarg_prop)
|
|
|
|
else:
|
|
|
|
properties[list_prop].append(kwarg_prop)
|
2017-07-17 20:56:13 +02:00
|
|
|
|
2017-07-13 17:03:31 +02:00
|
|
|
properties.update(**kwargs)
|
2017-07-12 17:36:15 +02:00
|
|
|
|
2017-07-13 17:03:31 +02:00
|
|
|
return cls(**properties)
|
2017-09-06 22:20:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Environment(object):
|
2017-11-09 21:42:59 +01:00
|
|
|
"""Abstract away some of the nasty details of working with STIX content.
|
2017-09-06 22:20:16 +02:00
|
|
|
|
|
|
|
Args:
|
2017-09-08 17:15:10 +02:00
|
|
|
factory (ObjectFactory, optional): Factory for creating objects with common
|
2017-09-06 22:20:16 +02:00
|
|
|
defaults for certain properties.
|
2017-09-08 17:15:10 +02:00
|
|
|
store (DataStore, optional): Data store providing the source and sink for the
|
2017-09-06 22:20:16 +02:00
|
|
|
environment.
|
2017-09-08 17:15:10 +02:00
|
|
|
source (DataSource, optional): Source for retrieving STIX objects.
|
|
|
|
sink (DataSink, optional): Destination for saving STIX objects.
|
2017-09-06 22:20:16 +02:00
|
|
|
Invalid if `store` is also provided.
|
|
|
|
"""
|
|
|
|
|
2017-09-08 15:01:12 +02:00
|
|
|
def __init__(self, factory=ObjectFactory(), store=None, source=None, sink=None):
|
2017-09-06 22:20:16 +02:00
|
|
|
self.factory = factory
|
|
|
|
self.source = CompositeDataSource()
|
|
|
|
if store:
|
|
|
|
self.source.add_data_source(store.source)
|
|
|
|
self.sink = store.sink
|
|
|
|
if source:
|
|
|
|
self.source.add_data_source(source)
|
|
|
|
if sink:
|
|
|
|
if store:
|
|
|
|
raise ValueError("Data store already provided! Environment may only have one data sink.")
|
|
|
|
self.sink = sink
|
|
|
|
|
2017-09-08 15:01:12 +02:00
|
|
|
def create(self, *args, **kwargs):
|
|
|
|
return self.factory.create(*args, **kwargs)
|
2017-09-08 17:15:10 +02:00
|
|
|
create.__doc__ = ObjectFactory.create.__doc__
|
2017-09-06 22:20:16 +02:00
|
|
|
|
2017-11-16 22:25:57 +01:00
|
|
|
get = DataStore.__dict__['get']
|
|
|
|
all_versions = DataStore.__dict__['all_versions']
|
|
|
|
query = DataStore.__dict__['query']
|
2017-11-21 22:29:06 +01:00
|
|
|
creator_of = DataStore.__dict__['creator_of']
|
2017-11-16 22:25:57 +01:00
|
|
|
relationships = DataStore.__dict__['relationships']
|
|
|
|
related_to = DataStore.__dict__['related_to']
|
|
|
|
add = DataStore.__dict__['add']
|
2017-09-08 15:01:12 +02:00
|
|
|
|
|
|
|
def add_filters(self, *args, **kwargs):
|
|
|
|
try:
|
2017-09-29 17:24:19 +02:00
|
|
|
return self.source.filters.update(*args, **kwargs)
|
|
|
|
except AttributeError:
|
|
|
|
raise AttributeError('Environment has no data source')
|
|
|
|
|
|
|
|
def add_filter(self, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
return self.source.filters.add(*args, **kwargs)
|
2017-09-08 15:01:12 +02:00
|
|
|
except AttributeError:
|
|
|
|
raise AttributeError('Environment has no data source')
|
|
|
|
|
2017-09-08 18:39:36 +02:00
|
|
|
def parse(self, *args, **kwargs):
|
|
|
|
return _parse(*args, **kwargs)
|
|
|
|
parse.__doc__ = _parse.__doc__
|
2017-11-02 12:48:37 +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:
|
|
|
|
The STIX object's creator, or
|
|
|
|
None, if the object contains no `created_by_ref` property or the
|
|
|
|
object's creator cannot be found.
|
|
|
|
|
|
|
|
"""
|
|
|
|
creator_id = obj.get('created_by_ref', '')
|
|
|
|
if creator_id:
|
|
|
|
return self.get(creator_id)
|
|
|
|
else:
|
|
|
|
return None
|