Add functions to set ObjectFactory default values
parent
fd6d9f74e9
commit
61733ad899
|
@ -30,19 +30,43 @@ class ObjectFactory(object):
|
|||
|
||||
self._defaults = {}
|
||||
if created_by_ref:
|
||||
self._defaults['created_by_ref'] = created_by_ref
|
||||
self.set_default_creator(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
|
||||
self.set_default_created(created)
|
||||
if external_references:
|
||||
self._defaults['external_references'] = external_references
|
||||
self.set_default_external_refs(external_references)
|
||||
if object_marking_refs:
|
||||
self._defaults['object_marking_refs'] = object_marking_refs
|
||||
self.set_default_object_marking_refs(object_marking_refs)
|
||||
self._list_append = list_append
|
||||
self._list_properties = ['external_references', 'object_marking_refs']
|
||||
|
||||
def set_default_creator(self, creator=None):
|
||||
"""Set default value for the `created_by_ref` property.
|
||||
|
||||
"""
|
||||
self._defaults['created_by_ref'] = creator
|
||||
|
||||
def set_default_created(self, created=None):
|
||||
"""Set default value for the `created` property.
|
||||
|
||||
"""
|
||||
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
|
||||
|
||||
def set_default_external_refs(self, external_references=None):
|
||||
"""Set default external references.
|
||||
|
||||
"""
|
||||
self._defaults['external_references'] = external_references
|
||||
|
||||
def set_default_object_marking_refs(self, object_marking_refs=None):
|
||||
"""Set default object markings.
|
||||
|
||||
"""
|
||||
self._defaults['object_marking_refs'] = object_marking_refs
|
||||
|
||||
def create(self, cls, **kwargs):
|
||||
"""Create a STIX object using object factory defaults.
|
||||
|
||||
|
@ -115,6 +139,22 @@ class Environment(DataStoreMixin):
|
|||
return self.factory.create(*args, **kwargs)
|
||||
create.__doc__ = ObjectFactory.create.__doc__
|
||||
|
||||
def set_default_creator(self, *args, **kwargs):
|
||||
return self.factory.set_default_creator(*args, **kwargs)
|
||||
set_default_creator.__doc__ = ObjectFactory.set_default_creator.__doc__
|
||||
|
||||
def set_default_created(self, *args, **kwargs):
|
||||
return self.factory.set_default_created(*args, **kwargs)
|
||||
set_default_created.__doc__ = ObjectFactory.set_default_created.__doc__
|
||||
|
||||
def set_default_external_refs(self, *args, **kwargs):
|
||||
return self.factory.set_default_external_refs(*args, **kwargs)
|
||||
set_default_external_refs.__doc__ = ObjectFactory.set_default_external_refs.__doc__
|
||||
|
||||
def set_default_object_marking_refs(self, *args, **kwargs):
|
||||
return self.factory.set_default_object_marking_refs(*args, **kwargs)
|
||||
set_default_object_marking_refs.__doc__ = ObjectFactory.set_default_object_marking_refs.__doc__
|
||||
|
||||
def add_filters(self, *args, **kwargs):
|
||||
try:
|
||||
return self.source.filters.update(*args, **kwargs)
|
||||
|
|
|
@ -47,7 +47,7 @@ def test_object_factory_created():
|
|||
assert ind.modified == FAKE_TIME
|
||||
|
||||
|
||||
def test_object_factory_external_resource():
|
||||
def test_object_factory_external_reference():
|
||||
ext_ref = stix2.ExternalReference(source_name="ACME Threat Intel",
|
||||
description="Threat report")
|
||||
factory = stix2.ObjectFactory(external_references=ext_ref)
|
||||
|
|
|
@ -7,7 +7,10 @@ from stix2.workbench import (AttackPattern, Campaign, CourseOfAction, Identity,
|
|||
add_data_source, all_versions, attack_patterns,
|
||||
campaigns, courses_of_action, create, get,
|
||||
identities, indicators, intrusion_sets, malware,
|
||||
observed_data, query, reports, threat_actors,
|
||||
observed_data, query, reports,
|
||||
set_default_created, set_default_creator,
|
||||
set_default_external_refs,
|
||||
set_default_object_marking_refs, threat_actors,
|
||||
tools, vulnerabilities)
|
||||
|
||||
from .constants import (ATTACK_PATTERN_ID, ATTACK_PATTERN_KWARGS, CAMPAIGN_ID,
|
||||
|
@ -200,3 +203,41 @@ def test_additional_filters_list():
|
|||
resp = tools([stix2.Filter('created_by_ref', '=', 'identity--c78cb6e5-0c4b-4611-8297-d1b8b55e40b5'),
|
||||
stix2.Filter('name', '=', 'Windows Credential Editor')])
|
||||
assert len(resp) == 1
|
||||
|
||||
|
||||
def test_default_creator():
|
||||
set_default_creator(IDENTITY_ID)
|
||||
campaign = Campaign(**CAMPAIGN_KWARGS)
|
||||
|
||||
assert 'created_by_ref' not in CAMPAIGN_KWARGS
|
||||
assert campaign.created_by_ref == IDENTITY_ID
|
||||
|
||||
|
||||
def test_default_created_timestamp():
|
||||
timestamp = "2018-03-19T01:02:03.000Z"
|
||||
set_default_created(timestamp)
|
||||
campaign = Campaign(**CAMPAIGN_KWARGS)
|
||||
|
||||
assert 'created' not in CAMPAIGN_KWARGS
|
||||
assert stix2.utils.format_datetime(campaign.created) == timestamp
|
||||
assert stix2.utils.format_datetime(campaign.modified) == timestamp
|
||||
|
||||
|
||||
def test_default_external_refs():
|
||||
ext_ref = stix2.ExternalReference(source_name="ACME Threat Intel",
|
||||
description="Threat report")
|
||||
set_default_external_refs(ext_ref)
|
||||
campaign = Campaign(**CAMPAIGN_KWARGS)
|
||||
|
||||
assert campaign.external_references[0].source_name == "ACME Threat Intel"
|
||||
assert campaign.external_references[0].description == "Threat report"
|
||||
|
||||
|
||||
def test_default_object_marking_refs():
|
||||
stmt_marking = stix2.StatementMarking("Copyright 2016, Example Corp")
|
||||
mark_def = stix2.MarkingDefinition(definition_type="statement",
|
||||
definition=stmt_marking)
|
||||
set_default_object_marking_refs(mark_def)
|
||||
campaign = Campaign(**CAMPAIGN_KWARGS)
|
||||
|
||||
assert campaign.object_marking_refs[0] == mark_def.id
|
||||
|
|
|
@ -20,6 +20,10 @@ from .environment import Environment
|
|||
_environ = Environment(store=MemoryStore())
|
||||
|
||||
create = _environ.create
|
||||
set_default_creator = _environ.set_default_creator
|
||||
set_default_created = _environ.set_default_created
|
||||
set_default_external_refs = _environ.set_default_external_refs
|
||||
set_default_object_marking_refs = _environ.set_default_object_marking_refs
|
||||
get = _environ.get
|
||||
all_versions = _environ.all_versions
|
||||
query = _environ.query
|
||||
|
|
Loading…
Reference in New Issue