Reorganize bases, use isinstance to check version

Renamed STIXDomainObject -> _DomainObject.
Renamed STIXRelationshipObject -> _RelationshipObject.
master
Chris Lenk 2020-03-27 02:40:42 -04:00
parent e31634c32b
commit 01ba190525
11 changed files with 77 additions and 70 deletions

View File

@ -8,6 +8,7 @@ import uuid
import simplejson as json
import six
import stix2
from stix2.canonicalization.Canonicalize import canonicalize
from .exceptions import (
@ -15,6 +16,7 @@ from .exceptions import (
ImmutableError, InvalidObjRefError, InvalidValueError,
MissingPropertiesError, MutuallyExclusivePropertiesError,
)
from .markings import _MarkingsMixin
from .markings.utils import validate
from .utils import (
NOW, PREFIX_21_REGEX, find_property_index, format_datetime, get_timestamp,
@ -160,17 +162,15 @@ class _STIXBase(Mapping):
custom_props = kwargs.pop('custom_properties', {})
if custom_props and not isinstance(custom_props, dict):
raise ValueError("'custom_properties' must be a dictionary")
if not self._allow_custom:
extra_kwargs = list(set(kwargs) - set(self._properties))
if extra_kwargs:
raise ExtraPropertiesError(cls, extra_kwargs)
else:
# because allow_custom is true, any extra kwargs are custom
extra_kwargs = list(set(kwargs) - set(self._properties))
extra_kwargs = list(set(kwargs) - set(self._properties))
if extra_kwargs and not self._allow_custom:
raise ExtraPropertiesError(cls, extra_kwargs)
# because allow_custom is true, any extra kwargs are custom
if custom_props or extra_kwargs:
self._allow_custom = True
if self._spec_version == "2.1":
if isinstance(self, stix2.v21._STIXBase21):
all_custom_prop_names = extra_kwargs
all_custom_prop_names.extend(list(custom_props.keys()))
for prop_name in all_custom_prop_names:
@ -321,6 +321,14 @@ class _STIXBase(Mapping):
return json.dumps(self, cls=STIXJSONEncoder, **kwargs)
class _DomainObject(_STIXBase, _MarkingsMixin):
pass
class _RelationshipObject(_STIXBase, _MarkingsMixin):
pass
class _Observable(_STIXBase):
def __init__(self, **kwargs):

View File

@ -7,9 +7,8 @@ import re
import stix2
from .base import _Observable, _STIXBase
from .base import _Observable
from .exceptions import ParseError
from .markings import _MarkingsMixin
from .utils import (
EXT_21_REGEX, PREFIX_21_REGEX, TYPE_21_REGEX, TYPE_REGEX, _get_dict,
)
@ -17,14 +16,6 @@ from .utils import (
STIX2_OBJ_MAPS = {}
class STIXDomainObject(_STIXBase, _MarkingsMixin):
pass
class STIXRelationshipObject(_STIXBase, _MarkingsMixin):
pass
def parse(data, allow_custom=False, version=None):
"""Convert a string, dict or file-like object into a STIX object.

View File

@ -639,7 +639,7 @@ class STIXObjectProperty(Property):
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')
if any(x in ('_DomainObject', '_RelationshipObject', '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

View File

@ -14,6 +14,9 @@
# flake8: noqa
from .base import (
_DomainObject, _Extension, _Observable, _RelationshipObject, _STIXBase20,
)
from .bundle import Bundle
from .common import (
TLP_AMBER, TLP_GREEN, TLP_RED, TLP_WHITE, CustomMarking, ExternalReference,

View File

@ -1,11 +1,12 @@
"""Base classes for STIX 2.0 type definitions."""
from ..base import _Extension, _Observable, _STIXBase
from ..core import STIXDomainObject, STIXRelationshipObject
from ..base import (
_DomainObject, _Extension, _Observable, _RelationshipObject, _STIXBase,
)
class _STIXBase20(_STIXBase):
_spec_version = "2.0"
pass
class _Observable(_Observable, _STIXBase20):
@ -16,9 +17,9 @@ class _Extension(_Extension, _STIXBase20):
pass
class STIXDomainObject(STIXDomainObject, _STIXBase20):
class _DomainObject(_DomainObject, _STIXBase20):
pass
class STIXRelationshipObject(STIXRelationshipObject, _STIXBase20):
class _RelationshipObject(_RelationshipObject, _STIXBase20):
pass

View File

@ -13,11 +13,11 @@ from ..properties import (
TimestampProperty, TypeProperty,
)
from ..utils import NOW
from .base import STIXDomainObject
from .base import _DomainObject
from .common import ExternalReference, GranularMarking, KillChainPhase
class AttackPattern(STIXDomainObject):
class AttackPattern(_DomainObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714302>`__.
"""
@ -40,7 +40,7 @@ class AttackPattern(STIXDomainObject):
])
class Campaign(STIXDomainObject):
class Campaign(_DomainObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714305>`__.
"""
@ -66,7 +66,7 @@ class Campaign(STIXDomainObject):
])
class CourseOfAction(STIXDomainObject):
class CourseOfAction(_DomainObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714308>`__.
"""
@ -88,7 +88,7 @@ class CourseOfAction(STIXDomainObject):
])
class Identity(STIXDomainObject):
class Identity(_DomainObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714311>`__.
"""
@ -113,7 +113,7 @@ class Identity(STIXDomainObject):
])
class Indicator(STIXDomainObject):
class Indicator(_DomainObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714314>`__.
"""
@ -144,7 +144,7 @@ class Indicator(STIXDomainObject):
raise InvalidValueError(self.__class__, 'pattern', str(errors[0]))
class IntrusionSet(STIXDomainObject):
class IntrusionSet(_DomainObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714317>`__.
"""
@ -173,7 +173,7 @@ class IntrusionSet(STIXDomainObject):
])
class Malware(STIXDomainObject):
class Malware(_DomainObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714320>`__.
"""
@ -196,7 +196,7 @@ class Malware(STIXDomainObject):
])
class ObservedData(STIXDomainObject):
class ObservedData(_DomainObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714323>`__.
"""
@ -226,7 +226,7 @@ class ObservedData(STIXDomainObject):
super(ObservedData, self).__init__(*args, **kwargs)
class Report(STIXDomainObject):
class Report(_DomainObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714326>`__.
"""
@ -250,7 +250,7 @@ class Report(STIXDomainObject):
])
class ThreatActor(STIXDomainObject):
class ThreatActor(_DomainObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714329>`__.
"""
@ -280,7 +280,7 @@ class ThreatActor(STIXDomainObject):
])
class Tool(STIXDomainObject):
class Tool(_DomainObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714332>`__.
"""
@ -304,7 +304,7 @@ class Tool(STIXDomainObject):
])
class Vulnerability(STIXDomainObject):
class Vulnerability(_DomainObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714335>`__.
"""
@ -374,5 +374,5 @@ def CustomObject(type='x-custom-type', properties=None):
],
sorted([x for x in properties if x[0].startswith('x_')], key=lambda x: x[0]),
]))
return _custom_object_builder(cls, type, _properties, '2.0', STIXDomainObject)
return _custom_object_builder(cls, type, _properties, '2.0', _DomainObject)
return wrapper

View File

@ -7,11 +7,11 @@ from ..properties import (
ReferenceProperty, StringProperty, TimestampProperty, TypeProperty,
)
from ..utils import NOW
from .base import STIXRelationshipObject
from .base import _RelationshipObject
from .common import ExternalReference, GranularMarking
class Relationship(STIXRelationshipObject):
class Relationship(_RelationshipObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714340>`__.
"""
@ -52,7 +52,7 @@ class Relationship(STIXRelationshipObject):
super(Relationship, self).__init__(**kwargs)
class Sighting(STIXRelationshipObject):
class Sighting(_RelationshipObject):
"""For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714343>`__.
"""

View File

@ -14,6 +14,9 @@
# flake8: noqa
from .base import (
_DomainObject, _Extension, _Observable, _RelationshipObject, _STIXBase21,
)
from .bundle import Bundle
from .common import (
TLP_AMBER, TLP_GREEN, TLP_RED, TLP_WHITE, CustomMarking, ExternalReference,

View File

@ -1,11 +1,12 @@
"""Base classes for STIX 2.1 type definitions."""
from ..base import _Extension, _Observable, _STIXBase
from ..core import STIXDomainObject, STIXRelationshipObject
from ..base import (
_DomainObject, _Extension, _Observable, _RelationshipObject, _STIXBase,
)
class _STIXBase21(_STIXBase):
_spec_version = "2.1"
pass
class _Observable(_Observable, _STIXBase21):
@ -16,9 +17,9 @@ class _Extension(_Extension, _STIXBase21):
pass
class STIXDomainObject(STIXDomainObject, _STIXBase21):
class _DomainObject(_DomainObject, _STIXBase21):
pass
class STIXRelationshipObject(STIXRelationshipObject, _STIXBase21):
class _RelationshipObject(_RelationshipObject, _STIXBase21):
pass

View File

@ -17,11 +17,11 @@ from ..properties import (
StringProperty, TimestampProperty, TypeProperty,
)
from ..utils import NOW
from .base import STIXDomainObject
from .base import _DomainObject
from .common import ExternalReference, GranularMarking, KillChainPhase
class AttackPattern(STIXDomainObject):
class AttackPattern(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -49,7 +49,7 @@ class AttackPattern(STIXDomainObject):
])
class Campaign(STIXDomainObject):
class Campaign(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -89,7 +89,7 @@ class Campaign(STIXDomainObject):
raise ValueError(msg.format(self))
class CourseOfAction(STIXDomainObject):
class CourseOfAction(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -115,7 +115,7 @@ class CourseOfAction(STIXDomainObject):
])
class Grouping(STIXDomainObject):
class Grouping(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -143,7 +143,7 @@ class Grouping(STIXDomainObject):
])
class Identity(STIXDomainObject):
class Identity(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -173,7 +173,7 @@ class Identity(STIXDomainObject):
])
class Indicator(STIXDomainObject):
class Indicator(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -210,7 +210,7 @@ class Indicator(STIXDomainObject):
if kwargs.get('pattern') and kwargs.get('pattern_type') == 'stix' and not kwargs.get('pattern_version'):
kwargs['pattern_version'] = '2.1'
super(STIXDomainObject, self).__init__(*args, **kwargs)
super(_DomainObject, self).__init__(*args, **kwargs)
def _check_object_constraints(self):
super(Indicator, self)._check_object_constraints()
@ -233,7 +233,7 @@ class Indicator(STIXDomainObject):
raise InvalidValueError(self.__class__, 'pattern', str(errors[0]))
class Infrastructure(STIXDomainObject):
class Infrastructure(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -274,7 +274,7 @@ class Infrastructure(STIXDomainObject):
raise ValueError(msg.format(self))
class IntrusionSet(STIXDomainObject):
class IntrusionSet(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -317,7 +317,7 @@ class IntrusionSet(STIXDomainObject):
raise ValueError(msg.format(self))
class Location(STIXDomainObject):
class Location(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -425,7 +425,7 @@ class Location(STIXDomainObject):
return final_url
class Malware(STIXDomainObject):
class Malware(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -478,7 +478,7 @@ class Malware(STIXDomainObject):
)
class MalwareAnalysis(STIXDomainObject):
class MalwareAnalysis(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -523,7 +523,7 @@ class MalwareAnalysis(STIXDomainObject):
self._check_at_least_one_property(["result", "analysis_sco_refs"])
class Note(STIXDomainObject):
class Note(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -551,7 +551,7 @@ class Note(STIXDomainObject):
])
class ObservedData(STIXDomainObject):
class ObservedData(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -607,7 +607,7 @@ class ObservedData(STIXDomainObject):
)
class Opinion(STIXDomainObject):
class Opinion(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -645,7 +645,7 @@ class Opinion(STIXDomainObject):
])
class Report(STIXDomainObject):
class Report(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -674,7 +674,7 @@ class Report(STIXDomainObject):
])
class ThreatActor(STIXDomainObject):
class ThreatActor(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -721,7 +721,7 @@ class ThreatActor(STIXDomainObject):
raise ValueError(msg.format(self))
class Tool(STIXDomainObject):
class Tool(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -751,7 +751,7 @@ class Tool(STIXDomainObject):
])
class Vulnerability(STIXDomainObject):
class Vulnerability(_DomainObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -828,6 +828,6 @@ def CustomObject(type='x-custom-type', properties=None):
],
sorted([x for x in properties if x[0].startswith('x_')], key=lambda x: x[0]),
]))
return _custom_object_builder(cls, type, _properties, '2.1', STIXDomainObject)
return _custom_object_builder(cls, type, _properties, '2.1', _DomainObject)
return wrapper

View File

@ -7,11 +7,11 @@ from ..properties import (
ReferenceProperty, StringProperty, TimestampProperty, TypeProperty,
)
from ..utils import NOW
from .base import STIXRelationshipObject
from .base import _RelationshipObject
from .common import ExternalReference, GranularMarking
class Relationship(STIXRelationshipObject):
class Relationship(_RelationshipObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.
@ -68,7 +68,7 @@ class Relationship(STIXRelationshipObject):
raise ValueError(msg.format(self))
class Sighting(STIXRelationshipObject):
class Sighting(_RelationshipObject):
# TODO: Add link
"""For more detailed information on this object's properties, see
`the STIX 2.1 specification <link here>`__.