Skip to content

Commit

Permalink
BUG: enforce rule on harmonzation key names
Browse files Browse the repository at this point in the history
fixes #1104
  • Loading branch information
Sebastian Wagner committed Dec 19, 2017
1 parent 6422b72 commit 057b62c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ CHANGELOG
- warnings of bots are catched by the logger (#1074)
- Bots stop when redis gives the error "OOM command not allowed when used memory > 'maxmemory'.".

### Harmonization
- Rule for harmonization keys is enforced (#1104)

### Bots
#### Collectors
- bots.collectors.mail.collector_mail_attach: Support attachment file parsing for imbox versions newer than 0.9.5
Expand Down
2 changes: 1 addition & 1 deletion docs/Data-Harmonization.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Every event **MUST** contain a timestamp field.

## Rules for keys

The keys can be grouped together in sub-fields, e.g. `source.ip` or `source.geolocation.latitude`. Thus, keys must match `[a-z_.]`.
The keys can be grouped together in sub-fields, e.g. `source.ip` or `source.geolocation.latitude`. Thus, keys must match `^[a-z_](.[a-z0-9_]+)*$`.


<a name="sections"></a>
Expand Down
4 changes: 4 additions & 0 deletions intelmq/lib/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def __init__(self, message=(), auto=False, harmonization=None):
expected=VALID_MESSSAGE_TYPES,
docs=HARMONIZATION_CONF_FILE)

for harm_key in self.harmonization_config.keys():
if not re.match('^[a-z_](.[a-z_0-9]+)*$', harm_key) and harm_key != '__type':
raise exceptions.InvalidKey("Harmonization key %r is invalid." % harm_key)

super(Message, self).__init__()
if isinstance(message, dict):
iterable = message.items()
Expand Down
7 changes: 7 additions & 0 deletions intelmq/tests/lib/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,13 @@ def test_message_update(self):
with self.assertRaises(exceptions.InvalidValue):
event.update({'source.asn': 'AS1'})

def test_invalid_harm_key(self):
""" Test if error is raised when using an invalid key. """
with self.assertRaises(exceptions.InvalidKey):
message.Event(harmonization={'event': {'foo..bar': {}}})
with self.assertRaises(exceptions.InvalidKey):
message.Event(harmonization={'event': {'foo.bar.': {}}})


if __name__ == '__main__': # pragma: no cover # pragma: no cover
unittest.main()

0 comments on commit 057b62c

Please sign in to comment.