From a79df01449f00bc44fe2c78d1d8a9f6ce440625f Mon Sep 17 00:00:00 2001 From: Greg Back Date: Fri, 24 Feb 2017 13:07:54 -0600 Subject: [PATCH] Convert more fields to Property classes. --- stix2/base.py | 27 +++++++++++---------------- stix2/bundle.py | 2 +- stix2/common.py | 12 ++++++------ stix2/test/test_kill_chain_phases.py | 2 +- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/stix2/base.py b/stix2/base.py index 4d1572e..4121570 100644 --- a/stix2/base.py +++ b/stix2/base.py @@ -65,27 +65,22 @@ class _STIXBase(collections.Mapping): prop_metadata.get('default', lambda x: ''))(cls), ) 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): if prop_name not in kwargs: - kwargs[prop_name] = prop.default() + if hasattr(prop, 'default'): + kwargs[prop_name] = prop.default() # if default == NOW: # kwargs[prop_name] = self.__now - try: - kwargs[prop_name] = prop.validate(kwargs[prop_name]) - except ValueError as exc: - msg = "Invalid value for {0} '{1}': {2}" - raise ValueError(msg.format(self.__class__.__name__, - prop_name, - exc)) + + if prop_name in kwargs: + try: + kwargs[prop_name] = prop.validate(kwargs[prop_name]) + except ValueError as exc: + msg = "Invalid value for {0} '{1}': {2}" + raise ValueError(msg.format(self.__class__.__name__, + prop_name, + exc)) def __init__(self, **kwargs): cls = self.__class__ diff --git a/stix2/bundle.py b/stix2/bundle.py index 9ef71be..5785d43 100644 --- a/stix2/bundle.py +++ b/stix2/bundle.py @@ -11,7 +11,7 @@ class Bundle(_STIXBase): 'type': TypeProperty(_type), 'id': IDProperty(_type), 'spec_version': Property(fixed="2.0"), - 'objects': {}, + 'objects': Property(), } def __init__(self, *args, **kwargs): diff --git a/stix2/common.py b/stix2/common.py index be417af..b28e342 100644 --- a/stix2/common.py +++ b/stix2/common.py @@ -1,7 +1,9 @@ """STIX 2 Common Data Types and Properties""" import re + from .base import _STIXBase +from .properties import Property 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}" @@ -33,12 +35,10 @@ COMMON_PROPERTIES = { class ExternalReference(_STIXBase): _properties = { - 'source_name': { - 'required': True, - }, - 'description': {}, - 'url': {}, - 'external_id': {}, + 'source_name': Property(required=True), + 'description': Property(), + 'url': Property(), + 'external_id': Property(), } diff --git a/stix2/test/test_kill_chain_phases.py b/stix2/test/test_kill_chain_phases.py index 425c4b2..f646f0a 100644 --- a/stix2/test/test_kill_chain_phases.py +++ b/stix2/test/test_kill_chain_phases.py @@ -25,7 +25,7 @@ FOO_PRE_ATTACK = """{ }""" -def test_lockheed_martin_cyber_kill_chain(): +def test_kill_chain_example(): preattack = stix2.KillChainPhase( kill_chain_name="foo", phase_name="pre-attack",