Create ObjectFactory class

currently only supports created_by_ref
stix2.1
clenk 2017-07-12 11:36:15 -04:00
parent 6f680be8a6
commit 0c47936ee0
4 changed files with 37 additions and 0 deletions

View File

@ -4,6 +4,7 @@
from . import exceptions
from .bundle import Bundle
from .environment import ObjectFactory
from .observables import (URL, AlternateDataStream, ArchiveExt, Artifact,
AutonomousSystem, Directory, DomainName,
EmailAddress, EmailMessage, EmailMIMEComponent, File,

14
stix2/environment.py Normal file
View File

@ -0,0 +1,14 @@
class ObjectFactory(object):
def __init__(self, created_by=None):
self.created_by = created_by
def create(self, cls=None, **kwargs):
if cls is None:
raise ValueError('No STIX object class provided')
if self.created_by is not None:
kwargs['created_by_ref'] = self.created_by
return cls(**kwargs)

View File

@ -20,6 +20,12 @@ TOOL_ID = "tool--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f"
SIGHTING_ID = "sighting--bfbc19db-ec35-4e45-beed-f8bde2a772fb"
VULNERABILITY_ID = "vulnerability--0c7b5b88-8ff7-4a4d-aa9d-feb398cd0061"
# Minimum required args for an Identity instance
IDENTITY_KWARGS = dict(
name="John Smith",
identity_class="individual",
)
# Minimum required args for an Indicator instance
INDICATOR_KWARGS = dict(
labels=['malicious-activity'],

View File

@ -0,0 +1,16 @@
import stix2
from .constants import IDENTITY_ID, IDENTITY_KWARGS, INDICATOR_KWARGS
def test_object_factory_created_by_ref_str():
factory = stix2.ObjectFactory(created_by=IDENTITY_ID)
ind = factory.create(stix2.Indicator, **INDICATOR_KWARGS)
assert ind.created_by_ref == IDENTITY_ID
def test_object_factory_created_by_ref_obj():
id_obj = stix2.Identity(id=IDENTITY_ID, **IDENTITY_KWARGS)
factory = stix2.ObjectFactory(created_by=id_obj)
ind = factory.create(stix2.Indicator, **INDICATOR_KWARGS)
assert ind.created_by_ref == IDENTITY_ID