Skip to content

Commit 3006220

Browse files
committed
FIX: Handle not existing fields with manual mapping
1 parent 2b8b617 commit 3006220

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

intelmq/bots/outputs/misp/output_feed.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,13 @@ def _extract_misp_attribute_kwargs(self, message: dict, definition: dict) -> dic
212212

213213
def _custom_mapping(self, obj: "MISPObject", message: dict):
214214
for object_relation, definition in self.attribute_mapping.items():
215-
obj.add_attribute(
216-
object_relation,
217-
value=message[object_relation],
218-
**self._extract_misp_attribute_kwargs(message, definition),
219-
)
220-
# In case of manual mapping, we want to fail if it produces incorrect values
215+
if object_relation in message:
216+
obj.add_attribute(
217+
object_relation,
218+
value=message[object_relation],
219+
**self._extract_misp_attribute_kwargs(message, definition),
220+
)
221+
# In case of manual mapping, we want to fail if it produces incorrect values
221222

222223
def _generate_feed(self, message: dict = None):
223224
if message:

intelmq/tests/bots/outputs/misp/test_output_feed.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,28 @@ def test_attribute_mapping(self):
149149
assert malware_name["value"] == EXAMPLE_EVENT["malware.name"]
150150
assert malware_name["comment"] == EXAMPLE_EVENT["extra.non_ascii"]
151151

152+
def test_attribute_mapping_empty_field(self):
153+
self.run_bot(
154+
parameters={
155+
"attribute_mapping": {
156+
"source.ip": {},
157+
"source.fqdn": {}, # not exists in the message
158+
}
159+
}
160+
)
161+
162+
current_event = open(f"{self.directory.name}/.current").read()
163+
with open(current_event) as f:
164+
objects = json.load(f).get("Event", {}).get("Object", [])
165+
166+
assert len(objects) == 1
167+
attributes = objects[0].get("Attribute")
168+
assert len(attributes) == 1
169+
source_ip = next(
170+
attr for attr in attributes if attr.get("object_relation") == "source.ip"
171+
)
172+
assert source_ip["value"] == "152.166.119.2"
173+
152174
def test_event_separation(self):
153175
self.input_message = [
154176
EXAMPLE_EVENT,

0 commit comments

Comments
 (0)