Skip to content

Commit f366698

Browse files
authored
Merge pull request #2413 from kamil-certat/entry_point_enum
Discover bots based on the entry points
2 parents df26d61 + 3d31341 commit f366698

File tree

19 files changed

+228
-16
lines changed

19 files changed

+228
-16
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*.profile
1212
.vscode/
1313
.profile
14-
intelmq.egg-info
14+
*.egg-info
1515
build
1616
dist
1717
*.old

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
### Core
2424
- `intelmq.lib.message`: For invalid message keys, add a hint on the failure to the exception: not allowed by configuration or not matching regular expression (PR#2398 by Sebastian Wagner).
2525
- `intelmq.lib.exceptions.InvalidKey`: Add optional parameter `additional_text` (PR#2398 by Sebastian Wagner).
26+
- Change the way we discover bots to allow easy extending based on the entry point name. (PR#2413 by Kamil Mankowski)
2627
- `intelmq.lib.mixins`: Add a new class, `StompMixin` (defined in a new submodule: `stomp`),
2728
which provides certain common STOMP-bot-specific operations, factored out from
2829
`intelmq.bots.collectors.stomp.collector` and `intelmq.bots.outputs.stomp.output`
@@ -68,6 +69,7 @@
6869

6970
### Documentation
7071
- Add a readthedocs configuration file to fix the build fail (PR#2403 by Sebastian Wagner).
72+
- Add a guide of developing extensions packages (PR#2413 by Kamil Mankowski)
7173
- Update/fix/improve the stuff related to the STOMP bots and integration with the *n6*'s
7274
Stream API (PR#2408 by Jan Kaliszewski).
7375
- Complete documentation overhaul. Change to markdown format. Uses the mkdocs-material (PR#2419 by Filip Pokorný).

contrib/example-extension-package/mybots/__init__.py

Whitespace-only changes.

contrib/example-extension-package/mybots/bots/__init__.py

Whitespace-only changes.

contrib/example-extension-package/mybots/bots/collectors/__init__.py

Whitespace-only changes.

contrib/example-extension-package/mybots/bots/collectors/custom/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
SPDX-FileCopyrightText: 2023 CERT.at GmbH <https://cert.at/>
3+
SPDX-License-Identifier: AGPL-3.0-or-later
4+
"""
5+
6+
# Use your package as usual
7+
from mybots.lib import common
8+
9+
from intelmq.lib.bot import CollectorBot
10+
11+
12+
class ExampleAdditionalCollectorBot(CollectorBot):
13+
"""
14+
This is an example bot provided by an extension package
15+
"""
16+
17+
def process(self):
18+
report = self.new_report()
19+
if self.raw: # noqa: Set as parameter
20+
report['raw'] = common.return_value('example')
21+
self.send_message(report)
22+
23+
24+
BOT = ExampleAdditionalCollectorBot

contrib/example-extension-package/mybots/lib/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
3+
SPDX-FileCopyrightText: 2023 CERT.at GmbH <https://cert.at/>
4+
SPDX-License-Identifier: AGPL-3.0-or-later
5+
"""
6+
7+
8+
def return_value(value):
9+
return value
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""Example IntelMQ extension package
2+
3+
SPDX-FileCopyrightText: 2023 CERT.at GmbH <https://cert.at/>
4+
SPDX-License-Identifier: AGPL-3.0-or-later
5+
"""
6+
7+
from pathlib import Path
8+
from setuptools import find_packages, setup
9+
10+
11+
# Instead of the bot-autodiscovery below, you can also just manually declare entrypoints
12+
# (regardless of packaging solution, even in pyproject.toml etc.), e.g.:
13+
#
14+
# 'intelmq.bots.collectors.custom.collector = mybots.bots.collectors.custom.collector:BOT.run'
15+
#
16+
# Important is:
17+
# - entry point has to start with `intelmq.bots.{type}` (type: collectors, experts, parsers, outputs)
18+
# - target has to end with `:BOT.run`
19+
# - entry points have to be in `console_scripts` group
20+
21+
22+
BOTS = []
23+
24+
base_path = Path(__file__).parent / 'mybots/bots'
25+
botfiles = [botfile for botfile in Path(base_path).glob('**/*.py') if botfile.is_file() and not botfile.name.startswith('_')]
26+
for file in botfiles:
27+
file = Path(str(file).replace(str(base_path), 'intelmq/bots'))
28+
entry_point = '.'.join(file.with_suffix('').parts)
29+
file = Path(str(file).replace('intelmq/bots', 'mybots/bots'))
30+
module = '.'.join(file.with_suffix('').parts)
31+
BOTS.append('{0} = {1}:BOT.run'.format(entry_point, module))
32+
33+
setup(
34+
name='intelmq-example-extension',
35+
version='1.0.0', # noqa: F821
36+
maintainer='Your Name',
37+
maintainer_email='you@example.com',
38+
packages=find_packages(),
39+
license='AGPLv3',
40+
description='This is an example package to demonstrate how ones can extend IntelMQ.',
41+
entry_points={
42+
'console_scripts': BOTS
43+
},
44+
)

0 commit comments

Comments
 (0)