chg: Use default category from template

Fix #477
pull/485/head
Raphaël Vinot 2019-10-18 14:44:54 +02:00
parent fef328d395
commit 880fb300ea
3 changed files with 15 additions and 1 deletions

View File

@ -1070,6 +1070,9 @@ class MISPObjectAttribute(MISPAttribute):
self.type = kwargs.pop('type', None)
if self.type is None:
self.type = self._definition.get('misp-attribute')
if 'category' not in kwargs and 'categories' in self._definition:
# Get first category in the list from the object template as default
self.category = self._definition['categories'][0]
self.disable_correlation = kwargs.pop('disable_correlation', None)
if self.disable_correlation is None:
# The correlation can be disabled by default in the object definition.
@ -1331,7 +1334,7 @@ class MISPObject(AbstractMISP):
logger.warning("The value of the attribute you're trying to add is None or empty string, skipping it. Object relation: {}".format(object_relation))
return None
if self._known_template:
if self._definition['attributes'].get(object_relation):
if object_relation in self._definition['attributes']:
attribute = MISPObjectAttribute(self._definition['attributes'][object_relation])
else:
# Woopsie, this object_relation is unknown, no sane defaults for you.

View File

@ -14,6 +14,14 @@
"to_ids": true,
"type": "filename",
"value": "bar"
},
{
"category": "Artifacts dropped",
"disable_correlation": false,
"object_relation": "pattern-in-file",
"to_ids": true,
"type": "pattern-in-file",
"value": "baz"
}
],
"description": "File object describing a file with meta-information",

View File

@ -159,6 +159,9 @@ class TestMISPEvent(unittest.TestCase):
self.mispevent.add_object(name='file', strict=True)
a = self.mispevent.objects[0].add_attribute('filename', value='bar', Tag=[{'name': 'blah'}])
del a.uuid
a = self.mispevent.objects[0].add_attribute('pattern-in-file', value='baz')
self.assertEqual(a.category, 'Artifacts dropped')
del a.uuid
self.mispevent.add_object(name='file', strict=False, default_attributes_parameters=self.mispevent.objects[0].attributes[0])
a = self.mispevent.objects[1].add_attribute('filename', value='baz')
del a.uuid