Skip to content

Commit 0c92949

Browse files
author
Sebastian Wagner
committed
ENH: lib/bot: fix behavior for unconfigured bots
Add `enabled` and `run_mode` as members of the `Bot` class with default values. Log the system parameters in the same way as default and runtime parameters. Warn if disallowed system parameters are encountered. Warn if no configuration could be found in the runtime configuration file for the bot's ID. Re-initialize logging if logging parameters have been changed by environment variables.
1 parent c9f3ec2 commit 0c92949

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ CHANGELOG
2525
They now reside in `intelmq.lib.processmanager` which also contains an interface definition the processmanager implementations must adhere to.
2626
Both the processmanagers and the `intelmqctl` script were cleaned up a bit.
2727
The `LogLevel` and `ReturnType` Enums were added to `intelmq.lib.datatypes`.
28+
- `intelmq.lib.bot`:
29+
- Enhance behaviour if an unconfigured bot is started (PR#2054 by Sebastian Wagner).
2830

2931
### Development
3032

intelmq/lib/bot.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
from intelmq.lib.datatypes import *
4343

4444
__all__ = ['Bot', 'CollectorBot', 'ParserBot', 'OutputBot', 'ExpertBot']
45+
ALLOWED_SYSTEM_PARAMETERS = {'enabled', 'run_mode', 'group', 'description', 'module', 'name'}
46+
# The first two keys are only used by the IntelMQ Manager and can be ignored, the last just contains the runtime parameters and is handled separately
47+
IGNORED_SYSTEM_PARAMETERS = {'groupname', 'bot_id', 'parameters'}
4548

4649

4750
class Bot(object):
@@ -53,6 +56,8 @@ class Bot(object):
5356
# Bot is capable of SIGHUP delaying
5457
_sighup_delay: bool = True
5558
# From the runtime configuration
59+
enabled: bool = True
60+
run_mode: str = "continuous"
5661
description: Optional[str] = None
5762
group: Optional[str] = None
5863
module = None
@@ -758,16 +763,21 @@ def __load_runtime_configuration(self):
758763

759764
if self.__bot_id in config:
760765
params = config[self.__bot_id]
761-
self.run_mode = params.get('run_mode', 'continuous')
762-
self.name = params.get('name')
763-
self.description = params.get('description')
764-
self.group = params.get('group')
765-
self.module = params.get('module')
766+
for key, value in params.items():
767+
if key in ALLOWED_SYSTEM_PARAMETERS and value:
768+
self.__log_configuration_parameter("system", key, value)
769+
setattr(self, key, value)
770+
elif key not in IGNORED_SYSTEM_PARAMETERS:
771+
self.logger.warning('Ignoring disallowed system parameter %r.',
772+
key)
766773
for option, value in params.get('parameters', {}).items():
767774
setattr(self, option, value)
768775
self.__log_configuration_parameter("runtime", option, value)
769776
if option.startswith('logging_'):
770777
reinitialize_logging = True
778+
else:
779+
self.logger.warning('Bot ID %r not found in runtime configuration - could not load any parameters.',
780+
self.__bot_id)
771781

772782
intelmq_environment = [elem for elem in os.environ if elem.startswith('INTELMQ_')]
773783
for elem in intelmq_environment:
@@ -784,6 +794,9 @@ def __load_runtime_configuration(self):
784794
setattr(self, option, value)
785795
self.__log_configuration_parameter("environment", option, value)
786796

797+
if option.startswith('logging_'):
798+
reinitialize_logging = True
799+
787800
if reinitialize_logging:
788801
self.logger.handlers = [] # remove all existing handlers
789802
self.__init_logger()

0 commit comments

Comments
 (0)