Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add ability to copy more fields from the report #2513

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
- `intelmq.bots.parsers.shadowserver._config`:
- Fetch schema before first run (PR#2482 by elsif2, fixes #2480).
- `intelmq.bots.parsers.dataplane.parser`: Use ` | ` as field delimiter, fix parsing of AS names including `|` (PR#2488 by DigitalTrustCenter).
- all parsers: add `copy_custom_fields` parameter allowing copying additional fields from the report, e.g. `extra.file_name`.
(PR# by Kamil Mankowski).
kamil-certat marked this conversation as resolved.
Show resolved Hide resolved

#### Experts
- `intelmq.bots.experts.sieve.expert`:
Expand Down
23 changes: 23 additions & 0 deletions docs/user/bots.md
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,17 @@ tweet text is sent separately and if allowed, links to pastebin are followed and

## Parser Bots

If not set differently during parsing, all parser bots copy the following fields from the report to an event:

- `feed.accuracy`
- `feed.code`
- `feed.documentation`
- `feed.name`
- `feed.provider`
- `feed.url`
- `rtir_id`
- `time.observation`

### Common parameters

#### `default_fields`
Expand All @@ -1346,6 +1357,18 @@ defaults_fields:
protocol.transport: tcp
```

#### `copy_custom_fields`

(optional, list) List of additional fields to be copy from the report (only applied if parsing the
event doesn't set the value).

Example usage:

```yaml
copy_custom_fields:
- extra.file_name
```

---

### Abuse.ch Feodo Tracker <div id="intelmq.bots.parsers.abusech.parser_feodotracker" />
Expand Down
8 changes: 8 additions & 0 deletions intelmq/lib/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@ class ParserBot(Bot):
_default_message_type = 'Report'

default_fields: Optional[dict] = {}
copy_custom_fields: Optional[list] = []

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -1245,6 +1246,13 @@ def process(self):
for key, value in self.default_fields.items():
event.add(key, value, overwrite=False)

if self.copy_custom_fields:
for key in self.copy_custom_fields:
if key not in report:
continue
for event in events:
event.add(key, report.get(key), overwrite=False)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When this code is in intelmq.lib.bot.ParserBot.process, it will not be active in all parsers as the above documentation suggests.

Adding it to intelmq.lib.message.Report.__init__ (and passing the parameter in intelmq.lib.bot.Bot.new_event will cover all cases AFAIK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, you mean some parsers do not use the process? Then they do not use the default_fields, right?

Copy link
Member

@sebix sebix Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process can be overwritten

Copy link
Contributor Author

@kamil-certat kamil-certat Jul 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, it could be, but I didn't know it's a broad practice - I've looked at the source code, and you're right, a lot of parsers rely on overriding the whole process. I'll move the code to the new_event, and I think we should consider moving support of default_fields there as well as this is something I'd expect all parsers to support (but it's a separated thing).

except Exception:
self.logger.exception('Failed to parse line.')
self.__failed.append((traceback.format_exc(), self._current_line))
Expand Down
13 changes: 13 additions & 0 deletions intelmq/tests/lib/test_parser_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,19 @@ def test_bad_default_fields_parameter_2(self):
self.assertAnyLoglineEqual(message="Invalid value of key 'source.port' in default_fields parameter.",
levelname="ERROR")

def test_copy_custom_fields_from_report(self):
"""Allow copying custom fields from the report message to support more context from reports"""
report = {**EXAMPLE_SHORT, "extra.file_name": "file.txt", "extra.field2": "value2"}
self.input_message = report

self.run_bot(parameters={"copy_custom_fields":
["extra.file_name", "extra.not_exists"]})

output_message = EXAMPLE_EVENT.copy()
output_message["extra.file_name"] = "file.txt"
self.assertMessageEqual(0, output_message)


def test_missing_raw(self):
""" Test DummyParserBot with missing raw. """
self.input_message = EXAMPLE_EMPTY_REPORT
Expand Down
Loading