2018-11-28 22:51:00 +01:00
|
|
|
"""STIX 2.1 Bundle Representation."""
|
|
|
|
|
2018-06-14 02:09:07 +02:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
from ..properties import (
|
|
|
|
IDProperty, ListProperty, STIXObjectProperty, TypeProperty,
|
|
|
|
)
|
2020-03-22 03:22:36 +01:00
|
|
|
from .base import _STIXBase21
|
2018-06-14 02:09:07 +02:00
|
|
|
|
|
|
|
|
2020-03-22 03:22:36 +01:00
|
|
|
class Bundle(_STIXBase21):
|
2018-06-14 02:09:07 +02:00
|
|
|
"""For more detailed information on this object's properties, see
|
2020-03-25 16:36:29 +01:00
|
|
|
`the STIX 2.1 specification <https://docs.oasis-open.org/cti/stix/v2.1/cs01/stix-v2.1-cs01.html#_nuwp4rox8c7r>`__.
|
2018-06-14 02:09:07 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
_type = 'bundle'
|
2018-06-30 00:38:04 +02:00
|
|
|
_properties = OrderedDict([
|
2020-04-02 03:52:04 +02:00
|
|
|
('type', TypeProperty(_type, spec_version='2.1')),
|
2019-06-14 23:58:51 +02:00
|
|
|
('id', IDProperty(_type, spec_version='2.1')),
|
2018-07-10 21:02:55 +02:00
|
|
|
('objects', ListProperty(STIXObjectProperty(spec_version='2.1'))),
|
2018-06-14 02:09:07 +02:00
|
|
|
])
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# Add any positional arguments to the 'objects' kwarg.
|
|
|
|
if args:
|
|
|
|
if isinstance(args[0], list):
|
|
|
|
kwargs['objects'] = args[0] + list(args[1:]) + kwargs.get('objects', [])
|
|
|
|
else:
|
|
|
|
kwargs['objects'] = list(args) + kwargs.get('objects', [])
|
|
|
|
|
2019-12-17 17:57:55 +01:00
|
|
|
self._allow_custom = kwargs.get('allow_custom', False)
|
2018-06-14 02:09:07 +02:00
|
|
|
self._properties['objects'].contained.allow_custom = kwargs.get('allow_custom', False)
|
|
|
|
|
|
|
|
super(Bundle, self).__init__(**kwargs)
|
2019-05-14 19:48:54 +02:00
|
|
|
|
|
|
|
def get_obj(self, obj_uuid):
|
2019-05-20 22:29:01 +02:00
|
|
|
if "objects" in self._inner:
|
2019-06-27 17:19:05 +02:00
|
|
|
found_objs = [elem for elem in self.objects if elem['id'] == obj_uuid]
|
2019-05-20 22:29:01 +02:00
|
|
|
if found_objs == []:
|
|
|
|
raise KeyError("'%s' does not match the id property of any of the bundle's objects" % obj_uuid)
|
|
|
|
return found_objs
|
|
|
|
else:
|
|
|
|
raise KeyError("There are no objects in this empty bundle")
|
2019-05-17 21:21:35 +02:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
try:
|
|
|
|
return super(Bundle, self).__getitem__(key)
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
return self.get_obj(key)
|
|
|
|
except KeyError:
|
2019-05-22 17:05:01 +02:00
|
|
|
raise KeyError("'%s' is neither a property on the bundle nor does it match the id property of any of the bundle's objects" % key)
|