Refactor Bundle

Removed redundant STIXObjectProperty, for 2.1 use validation specific to that version
stix2.1
Emmanuelle Vargas-Gonzalez 2018-07-10 15:02:55 -04:00
parent 78d77254ae
commit 5332d54383
2 changed files with 7 additions and 76 deletions

View File

@ -1,50 +1,8 @@
from collections import OrderedDict
from ..base import _STIXBase
from ..core import parse
from ..utils import _get_dict, get_class_hierarchy_names
from .properties import (IDProperty, ListProperty, Property, StringProperty,
TypeProperty)
class STIXObjectProperty(Property):
def __init__(self, allow_custom=False, *args, **kwargs):
self.allow_custom = allow_custom
super(STIXObjectProperty, self).__init__(*args, **kwargs)
def clean(self, value):
# Any STIX Object (SDO, SRO, or Marking Definition) can be added to
# a bundle with no further checks.
if any(x in ('STIXDomainObject', 'STIXRelationshipObject', 'MarkingDefinition')
for x in get_class_hierarchy_names(value)):
# A simple "is this a spec version 2.1+ object" test. For now,
# limit 2.0 bundles to 2.0 objects. It's not possible yet to
# have validation co-constraints among properties, e.g. have
# validation here depend on the value of another property
# (spec_version). So this is a hack, and not technically spec-
# compliant.
if 'spec_version' in value:
raise ValueError("Spec version 2.0 bundles don't yet support "
"containing objects of a different spec "
"version.")
return value
try:
dictified = _get_dict(value)
except ValueError:
raise ValueError("This property may only contain a dictionary or object")
if dictified == {}:
raise ValueError("This property may only contain a non-empty dictionary or object")
if 'type' in dictified and dictified['type'] == 'bundle':
raise ValueError('This property may not contain a Bundle object')
if 'spec_version' in dictified:
# See above comment regarding spec_version.
raise ValueError("Spec version 2.0 bundles don't yet support "
"containing objects of a different spec version.")
parsed_obj = parse(dictified, allow_custom=self.allow_custom)
return parsed_obj
from ..properties import (IDProperty, ListProperty, STIXObjectProperty,
StringProperty, TypeProperty)
class Bundle(_STIXBase):

View File

@ -1,48 +1,21 @@
from collections import OrderedDict
from ..base import _STIXBase
from ..core import parse
from ..utils import _get_dict, get_class_hierarchy_names
from .properties import IDProperty, ListProperty, Property, TypeProperty
class STIXObjectProperty(Property):
def __init__(self, allow_custom=False, *args, **kwargs):
self.allow_custom = allow_custom
super(STIXObjectProperty, self).__init__(*args, **kwargs)
def clean(self, value):
# Any STIX Object (SDO, SRO, or Marking Definition) can be added to
# a bundle with no further checks.
if any(x in ('STIXDomainObject', 'STIXRelationshipObject', 'MarkingDefinition')
for x in get_class_hierarchy_names(value)):
return value
try:
dictified = _get_dict(value)
except ValueError:
raise ValueError("This property may only contain a dictionary or object")
if dictified == {}:
raise ValueError("This property may only contain a non-empty dictionary or object")
if 'type' in dictified and dictified['type'] == 'bundle':
raise ValueError('This property may not contain a Bundle object')
parsed_obj = parse(dictified, allow_custom=self.allow_custom)
return parsed_obj
from ..properties import (IDProperty, ListProperty, STIXObjectProperty,
TypeProperty)
class Bundle(_STIXBase):
# TODO: Add link
"""For more detailed information on this object's properties, see
TODO: Update this to a STIX 2.1 link.
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part1-stix-core/stix-v2.0-cs01-part1-stix-core.html#_Toc496709293>`__.
`the STIX 2.1 specification <link here>`__.
"""
_type = 'bundle'
_properties = OrderedDict([
('type', TypeProperty(_type)),
('id', IDProperty(_type)),
('objects', ListProperty(STIXObjectProperty)),
('objects', ListProperty(STIXObjectProperty(spec_version='2.1'))),
])
def __init__(self, *args, **kwargs):