Merge branch 'datastores' of github.com:oasis-open/cti-python-stix2 into datastores

stix2.1
Emmanuelle Vargas-Gonzalez 2017-08-22 10:47:23 -04:00
commit f437a4df5b
7 changed files with 46 additions and 8 deletions

View File

@ -1,7 +1,7 @@
[settings] [settings]
check=1 check=1
diff=1 diff=1
known_third_party=dateutil,pytest,pytz,six,requests,taxii2_client known_third_party=dateutil,pytest,pytz,six,requests,taxii2client
known_first_party=stix2 known_first_party=stix2
not_skip=__init__.py not_skip=__init__.py
force_sort_within_sections=1 force_sort_within_sections=1

View File

@ -39,8 +39,8 @@ constructor:
from stix2 import Indicator from stix2 import Indicator
indicator = Indicator(name="File hash for malware variant", indicator = Indicator(name="File hash for malware variant",
labels=['malicious-activity'], labels=["malicious-activity"],
pattern='file:hashes.md5 = "d41d8cd98f00b204e9800998ecf8427e"') pattern="[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']")
Certain required attributes of all objects will be set automatically if Certain required attributes of all objects will be set automatically if
not provided as keyword arguments: not provided as keyword arguments:

View File

@ -52,6 +52,8 @@ setup(
'pytz', 'pytz',
'requests', 'requests',
'simplejson', 'simplejson',
'six' 'six',
'stix2-patterns',
'taxii2-client',
], ],
) )

View File

@ -7,6 +7,8 @@ import uuid
from six import string_types, text_type from six import string_types, text_type
from stix2patterns.validator import run_validator
from .base import _STIXBase from .base import _STIXBase
from .exceptions import DictionaryKeyError from .exceptions import DictionaryKeyError
from .utils import get_dict, parse_into_datetime from .utils import get_dict, parse_into_datetime
@ -370,3 +372,17 @@ class EnumProperty(StringProperty):
if value not in self.allowed: if value not in self.allowed:
raise ValueError("value '%s' is not valid for this enumeration." % value) raise ValueError("value '%s' is not valid for this enumeration." % value)
return self.string_type(value) return self.string_type(value)
class PatternProperty(StringProperty):
def __init__(self, **kwargs):
super(PatternProperty, self).__init__(**kwargs)
def clean(self, value):
str_value = super(PatternProperty, self).clean(value)
errors = run_validator(str_value)
if errors:
raise ValueError(str(errors[0]))
return self.string_type(value)

View File

@ -11,8 +11,8 @@ from .base import _STIXBase
from .common import ExternalReference, GranularMarking, KillChainPhase from .common import ExternalReference, GranularMarking, KillChainPhase
from .observables import ObservableProperty from .observables import ObservableProperty
from .properties import (BooleanProperty, IDProperty, IntegerProperty, from .properties import (BooleanProperty, IDProperty, IntegerProperty,
ListProperty, ReferenceProperty, StringProperty, ListProperty, PatternProperty, ReferenceProperty,
TimestampProperty, TypeProperty) StringProperty, TimestampProperty, TypeProperty)
from .utils import NOW from .utils import NOW
@ -117,7 +117,7 @@ class Indicator(_STIXBase):
('labels', ListProperty(StringProperty, required=True)), ('labels', ListProperty(StringProperty, required=True)),
('name', StringProperty()), ('name', StringProperty()),
('description', StringProperty()), ('description', StringProperty()),
('pattern', StringProperty(required=True)), ('pattern', PatternProperty(required=True)),
('valid_from', TimestampProperty(default=lambda: NOW)), ('valid_from', TimestampProperty(default=lambda: NOW)),
('valid_until', TimestampProperty()), ('valid_until', TimestampProperty()),
('kill_chain_phases', ListProperty(KillChainPhase)), ('kill_chain_phases', ListProperty(KillChainPhase)),

View File

@ -1,5 +1,5 @@
import pytest import pytest
from taxii2_client import Collection from taxii2client import Collection
from stix2.sources import DataSource, Filter, taxii from stix2.sources import DataSource, Filter, taxii

View File

@ -174,3 +174,23 @@ def test_parse_indicator(data):
assert idctr.valid_from == dt.datetime(1970, 1, 1, 0, 0, 1, tzinfo=pytz.utc) assert idctr.valid_from == dt.datetime(1970, 1, 1, 0, 0, 1, tzinfo=pytz.utc)
assert idctr.labels[0] == "malicious-activity" assert idctr.labels[0] == "malicious-activity"
assert idctr.pattern == "[file:hashes.MD5 = 'd41d8cd98f00b204e9800998ecf8427e']" assert idctr.pattern == "[file:hashes.MD5 = 'd41d8cd98f00b204e9800998ecf8427e']"
def test_invalid_indicator_pattern():
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
stix2.Indicator(
labels=['malicious-activity'],
pattern="file:hashes.MD5 = 'd41d8cd98f00b204e9800998ecf8427e'",
)
assert excinfo.value.cls == stix2.Indicator
assert excinfo.value.prop_name == 'pattern'
assert 'input is missing square brackets' in excinfo.value.reason
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
stix2.Indicator(
labels=['malicious-activity'],
pattern='[file:hashes.MD5 = "d41d8cd98f00b204e9800998ecf8427e"]',
)
assert excinfo.value.cls == stix2.Indicator
assert excinfo.value.prop_name == 'pattern'
assert 'mismatched input' in excinfo.value.reason