Convert more fields to Property classes.

stix2.1
Greg Back 2017-02-24 13:07:54 -06:00
parent 7ef6e20e9a
commit a79df01449
4 changed files with 19 additions and 24 deletions

View File

@ -65,27 +65,22 @@ class _STIXBase(collections.Mapping):
prop_metadata.get('default', lambda x: ''))(cls), prop_metadata.get('default', lambda x: ''))(cls),
) )
raise ValueError(msg) raise ValueError(msg)
elif prop_metadata.get('fixed'):
if kwargs[prop_name] != prop_metadata['fixed']:
msg = prop_metadata.get('error_msg', DEFAULT_ERROR).format(
type=class_name,
field=prop_name,
expected=prop_metadata['fixed']
)
raise ValueError(msg)
def _check_property(self, prop_name, prop, kwargs): def _check_property(self, prop_name, prop, kwargs):
if prop_name not in kwargs: if prop_name not in kwargs:
kwargs[prop_name] = prop.default() if hasattr(prop, 'default'):
kwargs[prop_name] = prop.default()
# if default == NOW: # if default == NOW:
# kwargs[prop_name] = self.__now # kwargs[prop_name] = self.__now
try:
kwargs[prop_name] = prop.validate(kwargs[prop_name]) if prop_name in kwargs:
except ValueError as exc: try:
msg = "Invalid value for {0} '{1}': {2}" kwargs[prop_name] = prop.validate(kwargs[prop_name])
raise ValueError(msg.format(self.__class__.__name__, except ValueError as exc:
prop_name, msg = "Invalid value for {0} '{1}': {2}"
exc)) raise ValueError(msg.format(self.__class__.__name__,
prop_name,
exc))
def __init__(self, **kwargs): def __init__(self, **kwargs):
cls = self.__class__ cls = self.__class__

View File

@ -11,7 +11,7 @@ class Bundle(_STIXBase):
'type': TypeProperty(_type), 'type': TypeProperty(_type),
'id': IDProperty(_type), 'id': IDProperty(_type),
'spec_version': Property(fixed="2.0"), 'spec_version': Property(fixed="2.0"),
'objects': {}, 'objects': Property(),
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):

View File

@ -1,7 +1,9 @@
"""STIX 2 Common Data Types and Properties""" """STIX 2 Common Data Types and Properties"""
import re import re
from .base import _STIXBase from .base import _STIXBase
from .properties import Property
from .utils import NOW from .utils import NOW
ref_regex = ("^[a-z][a-z-]+[a-z]--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}" ref_regex = ("^[a-z][a-z-]+[a-z]--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}"
@ -33,12 +35,10 @@ COMMON_PROPERTIES = {
class ExternalReference(_STIXBase): class ExternalReference(_STIXBase):
_properties = { _properties = {
'source_name': { 'source_name': Property(required=True),
'required': True, 'description': Property(),
}, 'url': Property(),
'description': {}, 'external_id': Property(),
'url': {},
'external_id': {},
} }

View File

@ -25,7 +25,7 @@ FOO_PRE_ATTACK = """{
}""" }"""
def test_lockheed_martin_cyber_kill_chain(): def test_kill_chain_example():
preattack = stix2.KillChainPhase( preattack = stix2.KillChainPhase(
kill_chain_name="foo", kill_chain_name="foo",
phase_name="pre-attack", phase_name="pre-attack",