Convert REF_PROPERTY to ReferenceProperty
parent
ef332a328b
commit
19146c8327
|
@ -3,17 +3,9 @@
|
|||
import re
|
||||
|
||||
from .base import _STIXBase
|
||||
from .properties import Property, BooleanProperty
|
||||
from .properties import Property, BooleanProperty, ReferenceProperty
|
||||
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}"
|
||||
"-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
|
||||
|
||||
REF_PROPERTY = {
|
||||
'validate': (lambda x, val: re.match(ref_regex, val)),
|
||||
'error_msg': "{type} {field} values must consist of a valid STIX type name and a valid UUID, separated by '--'."
|
||||
}
|
||||
|
||||
COMMON_PROPERTIES = {
|
||||
# 'type' and 'id' should be defined on each individual type
|
||||
'created': {
|
||||
|
@ -24,7 +16,7 @@ COMMON_PROPERTIES = {
|
|||
},
|
||||
'external_references': {},
|
||||
'revoked': BooleanProperty(),
|
||||
'created_by_ref': REF_PROPERTY
|
||||
'created_by_ref': ReferenceProperty(),
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import re
|
||||
import uuid
|
||||
|
||||
|
||||
|
@ -117,3 +118,16 @@ class BooleanProperty(Property):
|
|||
if not isinstance(value, bool):
|
||||
raise ValueError("must be a boolean value.")
|
||||
return value
|
||||
|
||||
|
||||
REF_REGEX = re.compile("^[a-z][a-z-]+[a-z]--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}"
|
||||
"-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
|
||||
|
||||
|
||||
class ReferenceProperty(Property):
|
||||
# TODO: support references that must be to a specific object type
|
||||
|
||||
def validate(self, value):
|
||||
if not REF_REGEX.match(value):
|
||||
raise ValueError("must match <object-type>--<guid>.")
|
||||
return value
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import pytest
|
||||
|
||||
from stix2.properties import Property, BooleanProperty, IDProperty, TypeProperty
|
||||
from stix2.properties import (Property, BooleanProperty, IDProperty,
|
||||
ReferenceProperty, TypeProperty)
|
||||
|
||||
|
||||
def test_property():
|
||||
|
@ -76,3 +77,11 @@ def test_boolean_property():
|
|||
print(invalid)
|
||||
with pytest.raises(ValueError):
|
||||
bool_prop.validate(invalid)
|
||||
|
||||
|
||||
def test_reference_property():
|
||||
ref_prop = ReferenceProperty()
|
||||
|
||||
assert ref_prop.validate("my-type--3a331bfe-0566-55e1-a4a0-9a2cd355a300")
|
||||
with pytest.raises(ValueError):
|
||||
ref_prop.validate("foo")
|
||||
|
|
|
@ -179,7 +179,7 @@ def test_indicator_required_field_pattern():
|
|||
def test_indicator_created_ref_invalid_format():
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
indicator = stix2.Indicator(created_by_ref='myprefix--12345678', **INDICATOR_KWARGS)
|
||||
assert str(excinfo.value) == "Indicator created_by_ref values must consist of a valid STIX type name and a valid UUID, separated by '--'."
|
||||
assert str(excinfo.value) == "Invalid value for Indicator 'created_by_ref': must match <object-type>--<guid>."
|
||||
|
||||
|
||||
def test_indicator_revoked_invalid():
|
||||
|
|
Loading…
Reference in New Issue