Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update dict key limits per 2.1 spec #198

Merged
merged 6 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stix2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def default(self, obj):
elif isinstance(obj, _STIXBase):
return dict(obj)
else:
return super(STIXJSONEncoder, self).default(obj)
return super(STIXJSONIncludeOptionalDefaultsEncoder, self).default(obj)


def get_required_properties(properties):
Expand Down
8 changes: 4 additions & 4 deletions stix2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import stix2

from . import exceptions
from .exceptions import ParseError
from .utils import _get_dict

STIX2_OBJ_MAPS = {}
Expand Down Expand Up @@ -74,7 +74,7 @@ def dict_to_stix2(stix_dict, allow_custom=False, version=None):

"""
if 'type' not in stix_dict:
raise exceptions.ParseError("Can't parse object with no 'type' property: %s" % str(stix_dict))
raise ParseError("Can't parse object with no 'type' property: %s" % str(stix_dict))

if "spec_version" in stix_dict:
# For STIX 2.0, applies to bundles only.
Expand All @@ -87,7 +87,7 @@ def dict_to_stix2(stix_dict, allow_custom=False, version=None):
else:
v = 'v' + stix2.DEFAULT_VERSION.replace('.', '')
else:
v = 'v20'
v = 'v' + stix2.DEFAULT_VERSION.replace('.', '')

OBJ_MAP = STIX2_OBJ_MAPS[v]

Expand All @@ -98,7 +98,7 @@ def dict_to_stix2(stix_dict, allow_custom=False, version=None):
# flag allows for unknown custom objects too, but will not
# be parsed into STIX object, returned as is
return stix_dict
raise exceptions.ParseError("Can't parse unknown object type '%s'! For custom types, use the CustomObject decorator." % stix_dict['type'])
raise ParseError("Can't parse unknown object type '%s'! For custom types, use the CustomObject decorator." % stix_dict['type'])

return obj_class(allow_custom=allow_custom, **stix_dict)

Expand Down
5 changes: 2 additions & 3 deletions stix2/test/test_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import pytest

import stix2
import stix2.v20.sdo
import stix2.v21.bundle
import stix2.v21.sdo

EXPECTED_BUNDLE = """{
"type": "bundle",
Expand Down Expand Up @@ -179,8 +179,7 @@ def test_parse_bundle(version):

assert bundle.type == "bundle"
assert bundle.id.startswith("bundle--")
# TODO: update this to a STIX 2.1 indicator
assert type(bundle.objects[0]) is stix2.v20.sdo.Indicator
assert type(bundle.objects[0]) is stix2.v21.sdo.Indicator
assert bundle.objects[0].type == 'indicator'
assert bundle.objects[1].type == 'malware'
assert bundle.objects[2].type == 'relationship'
Expand Down
5 changes: 2 additions & 3 deletions stix2/test/test_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import stix2
import stix2.base
import stix2.v20.sdo
import stix2.v21.sdo

from .constants import FAKE_TIME, MARKING_DEFINITION_ID

Expand Down Expand Up @@ -95,8 +95,7 @@ def test_identity_custom_property_allowed():
def test_parse_identity_custom_property(data):
with pytest.raises(stix2.exceptions.ExtraPropertiesError) as excinfo:
identity = stix2.parse(data)
# TODO: update to create and check a STIX 2.1 Identity object
assert excinfo.value.cls == stix2.v20.sdo.Identity
assert excinfo.value.cls == stix2.v21.sdo.Identity
assert excinfo.value.properties == ['foo']
assert "Unexpected properties for" in str(excinfo.value)

Expand Down
17 changes: 9 additions & 8 deletions stix2/test/test_language_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import datetime as dt

import pytest
import pytz

import stix2
Expand All @@ -21,7 +20,7 @@
"description": "More information about bank attack"
}"""

TEST_LANGUAGE_CONTENT = """{
TEST_LANGUAGE_CONTENT = u"""{
"type": "language-content",
"id": "language-content--b86bd89f-98bb-4fa9-8cb2-9ad421da981d",
"created": "2017-02-08T21:31:22.007Z",
Expand All @@ -30,18 +29,17 @@
"object_modified": "2017-02-08T21:31:22.007Z",
"contents": {
"de": {
"name": "Bank Angriff 1",
"description": "Weitere Informationen über Banküberfall"
"description": "Weitere Informationen über Banküberfall",
"name": "Bank Angriff 1"
},
"fr": {
"name": "Attaque Bank 1",
"description": "Plus d'informations sur la crise bancaire"
"description": "Plus d'informations sur la crise bancaire",
"name": "Attaque Bank 1"
}
}
}"""


@pytest.mark.xfail(reason="Dictionary keys are too short")
def test_language_content_campaign():
now = dt.datetime(2017, 2, 8, 21, 31, 22, microsecond=7000, tzinfo=pytz.utc)

Expand All @@ -66,5 +64,8 @@ def test_language_content_campaign():

camp = stix2.parse(TEST_CAMPAIGN)

assert str(lc) in TEST_LANGUAGE_CONTENT
# In order to provide the same representation, we need to disable escaping
# in json.dumps(). https://docs.python.org/3/library/json.html#json.dumps
# or https://docs.python.org/2/library/json.html#json.dumps
assert lc.serialize(pretty=True, ensure_ascii=False) == TEST_LANGUAGE_CONTENT
assert lc.modified == camp.modified
13 changes: 7 additions & 6 deletions stix2/test/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from stix2 import CustomObject, EmailMIMEComponent, ExtensionsProperty, TCPExt
from stix2.exceptions import AtLeastOnePropertyError, DictionaryKeyError
from stix2.properties import (BinaryProperty, BooleanProperty,
DictionaryProperty, EmbeddedObjectProperty,
EnumProperty, FloatProperty, HashesProperty,
HexProperty, IDProperty, IntegerProperty,
ListProperty, Property, ReferenceProperty,
StringProperty, TimestampProperty, TypeProperty)
from stix2.v20.properties import (BinaryProperty, BooleanProperty,
DictionaryProperty, EmbeddedObjectProperty,
EnumProperty, FloatProperty, HashesProperty,
HexProperty, IDProperty, IntegerProperty,
ListProperty, Property, ReferenceProperty,
StringProperty, TimestampProperty,
TypeProperty)

from .constants import FAKE_TIME

Expand Down
10 changes: 5 additions & 5 deletions stix2/v20/bundle.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from collections import OrderedDict

from stix2 import parse
from stix2.base import _STIXBase
from stix2.properties import (IDProperty, ListProperty, Property,
StringProperty, TypeProperty)
from stix2.utils import _get_dict, get_class_hierarchy_names
from ..base import _STIXBase
from ..core import parse
from ..utils import _get_dict, get_class_hierarchy_names
from .properties import (IDProperty, ListProperty, Property, StringProperty,
TypeProperty)


class STIXObjectProperty(Property):
Expand Down
6 changes: 3 additions & 3 deletions stix2/v20/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from ..base import _STIXBase
from ..markings import _MarkingsMixin
from ..properties import (HashesProperty, IDProperty, ListProperty, Property,
ReferenceProperty, SelectorProperty, StringProperty,
TimestampProperty, TypeProperty)
from ..utils import NOW, _get_dict
from .properties import (HashesProperty, IDProperty, ListProperty, Property,
ReferenceProperty, SelectorProperty, StringProperty,
TimestampProperty, TypeProperty)


class ExternalReference(_STIXBase):
Expand Down
10 changes: 5 additions & 5 deletions stix2/v20/observables.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from ..base import _Extension, _Observable, _STIXBase
from ..exceptions import (AtLeastOnePropertyError, CustomContentError,
DependentPropertiesError, ParseError)
from ..properties import (BinaryProperty, BooleanProperty, DictionaryProperty,
EmbeddedObjectProperty, EnumProperty, FloatProperty,
HashesProperty, HexProperty, IntegerProperty,
ListProperty, ObjectReferenceProperty, Property,
StringProperty, TimestampProperty, TypeProperty)
from ..utils import TYPE_REGEX, _get_dict
from .properties import (BinaryProperty, BooleanProperty, DictionaryProperty,
EmbeddedObjectProperty, EnumProperty, FloatProperty,
HashesProperty, HexProperty, IntegerProperty,
ListProperty, ObjectReferenceProperty, Property,
StringProperty, TimestampProperty, TypeProperty)


class ObservableProperty(Property):
Expand Down
6 changes: 3 additions & 3 deletions stix2/properties.py → stix2/v20/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from six import string_types, text_type
from stix2patterns.validator import run_validator

from .base import _STIXBase
from .exceptions import DictionaryKeyError
from .utils import _get_dict, parse_into_datetime
from ..base import _STIXBase
from ..exceptions import DictionaryKeyError
from ..utils import _get_dict, parse_into_datetime


class Property(object):
Expand Down
11 changes: 5 additions & 6 deletions stix2/v20/sdo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
from collections import OrderedDict
import re

import stix2

from ..base import _STIXBase
from ..core import _register_type
from ..markings import _MarkingsMixin
from ..properties import (BooleanProperty, IDProperty, IntegerProperty,
ListProperty, PatternProperty, ReferenceProperty,
StringProperty, TimestampProperty, TypeProperty)
from ..utils import NOW, TYPE_REGEX
from .common import ExternalReference, GranularMarking, KillChainPhase
from .observables import ObservableProperty
from .properties import (BooleanProperty, IDProperty, IntegerProperty,
ListProperty, PatternProperty, ReferenceProperty,
StringProperty, TimestampProperty, TypeProperty)


class STIXDomainObject(_STIXBase, _MarkingsMixin):
Expand Down Expand Up @@ -409,7 +408,7 @@ def __init__(self, **kwargs):
return
raise e

stix2._register_type(_Custom, version="2.0")
_register_type(_Custom, version="2.0")
return _Custom

return custom_builder
6 changes: 3 additions & 3 deletions stix2/v20/sro.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

from ..base import _STIXBase
from ..markings import _MarkingsMixin
from ..properties import (BooleanProperty, IDProperty, IntegerProperty,
ListProperty, ReferenceProperty, StringProperty,
TimestampProperty, TypeProperty)
from ..utils import NOW
from .common import ExternalReference, GranularMarking
from .properties import (BooleanProperty, IDProperty, IntegerProperty,
ListProperty, ReferenceProperty, StringProperty,
TimestampProperty, TypeProperty)


class STIXRelationshipObject(_STIXBase, _MarkingsMixin):
Expand Down
8 changes: 4 additions & 4 deletions stix2/v21/bundle.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from collections import OrderedDict

from stix2 import parse
from stix2.base import _STIXBase
from stix2.properties import IDProperty, ListProperty, Property, TypeProperty
from stix2.utils import _get_dict, get_class_hierarchy_names
from ..base import _STIXBase
from ..core import parse
from ..utils import _get_dict, get_class_hierarchy_names
from .properties import IDProperty, ListProperty, Property, TypeProperty


class STIXObjectProperty(Property):
Expand Down
8 changes: 4 additions & 4 deletions stix2/v21/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

from ..base import _STIXBase
from ..markings import _MarkingsMixin
from ..properties import (BooleanProperty, DictionaryProperty, HashesProperty,
IDProperty, ListProperty, Property,
ReferenceProperty, SelectorProperty, StringProperty,
TimestampProperty, TypeProperty)
from ..utils import NOW, _get_dict
from .properties import (BooleanProperty, DictionaryProperty, HashesProperty,
IDProperty, ListProperty, Property, ReferenceProperty,
SelectorProperty, StringProperty, TimestampProperty,
TypeProperty)


class ExternalReference(_STIXBase):
Expand Down
10 changes: 5 additions & 5 deletions stix2/v21/observables.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from ..base import _Extension, _Observable, _STIXBase
from ..exceptions import (AtLeastOnePropertyError, CustomContentError,
DependentPropertiesError, ParseError)
from ..properties import (BinaryProperty, BooleanProperty, DictionaryProperty,
EmbeddedObjectProperty, EnumProperty, FloatProperty,
HashesProperty, HexProperty, IntegerProperty,
ListProperty, ObjectReferenceProperty, Property,
StringProperty, TimestampProperty, TypeProperty)
from ..utils import TYPE_REGEX, _get_dict
from .properties import (BinaryProperty, BooleanProperty, DictionaryProperty,
EmbeddedObjectProperty, EnumProperty, FloatProperty,
HashesProperty, HexProperty, IntegerProperty,
ListProperty, ObjectReferenceProperty, Property,
StringProperty, TimestampProperty, TypeProperty)


class ObservableProperty(Property):
Expand Down
Loading