Simplify allowing custom observables/extensions
parent
9ef5b395a8
commit
91376586d4
|
@ -49,7 +49,7 @@ class _STIXBase(collections.Mapping):
|
|||
|
||||
return all_properties
|
||||
|
||||
def _check_property(self, prop_name, prop, kwargs, allow_custom=False):
|
||||
def _check_property(self, prop_name, prop, kwargs):
|
||||
if prop_name not in kwargs:
|
||||
if hasattr(prop, 'default'):
|
||||
value = prop.default()
|
||||
|
@ -61,7 +61,7 @@ class _STIXBase(collections.Mapping):
|
|||
try:
|
||||
kwargs[prop_name] = prop.clean(kwargs[prop_name])
|
||||
except ValueError as exc:
|
||||
if allow_custom and isinstance(exc, ParseError):
|
||||
if self.__allow_custom and isinstance(exc, ParseError):
|
||||
return
|
||||
raise InvalidValueError(self.__class__, prop_name, reason=str(exc))
|
||||
|
||||
|
@ -99,6 +99,7 @@ class _STIXBase(collections.Mapping):
|
|||
|
||||
def __init__(self, allow_custom=False, **kwargs):
|
||||
cls = self.__class__
|
||||
self.__allow_custom = allow_custom
|
||||
|
||||
# Use the same timestamp for any auto-generated datetimes
|
||||
self.__now = get_timestamp()
|
||||
|
@ -127,11 +128,7 @@ class _STIXBase(collections.Mapping):
|
|||
raise MissingPropertiesError(cls, missing_kwargs)
|
||||
|
||||
for prop_name, prop_metadata in cls._properties.items():
|
||||
try:
|
||||
self._check_property(prop_name, prop_metadata, setting_kwargs, allow_custom)
|
||||
except ParseError as err:
|
||||
if not allow_custom:
|
||||
raise err
|
||||
self._check_property(prop_name, prop_metadata, setting_kwargs)
|
||||
|
||||
self._inner = setting_kwargs
|
||||
|
||||
|
@ -250,8 +247,8 @@ class _Observable(_STIXBase):
|
|||
if ref_type not in allowed_types:
|
||||
raise InvalidObjRefError(self.__class__, prop_name, "object reference '%s' is of an invalid type '%s'" % (ref, ref_type))
|
||||
|
||||
def _check_property(self, prop_name, prop, kwargs, allow_custom=False):
|
||||
super(_Observable, self)._check_property(prop_name, prop, kwargs, allow_custom)
|
||||
def _check_property(self, prop_name, prop, kwargs):
|
||||
super(_Observable, self)._check_property(prop_name, prop, kwargs)
|
||||
if prop_name not in kwargs:
|
||||
return
|
||||
|
||||
|
|
|
@ -610,30 +610,13 @@ def test_parse_observable_with_unregistered_custom_extension():
|
|||
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
stix2.parse_observable(input_str)
|
||||
assert "Can't parse Unknown extension type" in str(excinfo.value)
|
||||
assert "Can't parse unknown extension type" in str(excinfo.value)
|
||||
|
||||
parsed_ob = stix2.parse_observable(input_str, allow_custom=True)
|
||||
assert parsed_ob['extensions']['x-foobar-ext']['property1'] == 'foo'
|
||||
assert not isinstance(parsed_ob['extensions']['x-foobar-ext'], stix2.core._STIXBase)
|
||||
|
||||
|
||||
def test_parse_observable_with_unregistered_custom_extension_dict():
|
||||
input_dict = {
|
||||
"type": "domain-name",
|
||||
"value": "example.com",
|
||||
"extensions": {
|
||||
"x-foobar-ext": {
|
||||
"property1": "foo",
|
||||
"property2": 12
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
stix2.v20.observables.DomainName(**input_dict)
|
||||
assert "Can't parse unknown extension type" in str(excinfo.value)
|
||||
|
||||
|
||||
def test_register_custom_object():
|
||||
# Not the way to register custom object.
|
||||
class CustomObject2(object):
|
||||
|
|
Loading…
Reference in New Issue