Merge pull request #143 from emmanvg/issue-134

Update TAXIICollectionSink.add() to use serialize()
stix2.0
Greg Back 2018-03-20 08:52:53 -05:00 committed by GitHub
commit 33cfc4bb27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 6 deletions

View File

@ -191,7 +191,7 @@ class _STIXBase(collections.Mapping):
**kwargs: The arguments for a json.dumps() call.
Returns:
dict: The serialized JSON object.
str: The serialized JSON object.
Note:
The argument ``pretty=True`` will output the STIX object following

View File

@ -67,27 +67,31 @@ class TAXIICollectionSink(DataSink):
"""
if isinstance(stix_data, _STIXBase):
# adding python STIX object
bundle = dict(Bundle(stix_data, allow_custom=self.allow_custom))
if stix_data["type"] == "bundle":
bundle = stix_data.serialize(encoding="utf-8")
else:
bundle = Bundle(stix_data, allow_custom=self.allow_custom).serialize(encoding="utf-8")
elif isinstance(stix_data, dict):
# adding python dict (of either Bundle or STIX obj)
if stix_data["type"] == "bundle":
bundle = stix_data
bundle = parse(stix_data, allow_custom=self.allow_custom, version=version).serialize(encoding="utf-8")
else:
bundle = dict(Bundle(stix_data, allow_custom=self.allow_custom))
bundle = Bundle(stix_data, allow_custom=self.allow_custom).serialize(encoding="utf-8")
elif isinstance(stix_data, list):
# adding list of something - recurse on each
for obj in stix_data:
self.add(obj, version=version)
return
elif isinstance(stix_data, str):
# adding json encoded string of STIX content
stix_data = parse(stix_data, allow_custom=self.allow_custom, version=version)
if stix_data["type"] == "bundle":
bundle = dict(stix_data)
bundle = stix_data.serialize(encoding="utf-8")
else:
bundle = dict(Bundle(stix_data, allow_custom=self.allow_custom))
bundle = Bundle(stix_data, allow_custom=self.allow_custom).serialize(encoding="utf-8")
else:
raise TypeError("stix_data must be as STIX object(or list of),json formatted STIX (or list of), or a json formatted STIX bundle")