Skip to content

Commit

Permalink
ENH: lib/bot: fix behavior for unconfigured bots
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Sebastian Wagner committed Aug 30, 2021
1 parent eaaaeb2 commit e278acf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ CHANGELOG
- Added an ExpertBot class - it should be used by all expert bots as a parent class
- Introduced a module for IntelMQ related datatypes `intelmq.lib.datatypes` which for now only contains an Enum listing the four bot types
- Added a `bottype` attribute to CollectorBot, ParserBot, ExpertBot, OutputBot
- `intelmq.lib.bot`:
- Enhance behaviour if an unconfigured bot is started (PR#2054 by Sebastian Wagner).

### Development

Expand Down
23 changes: 18 additions & 5 deletions intelmq/lib/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
from intelmq.lib.datatypes import *

__all__ = ['Bot', 'CollectorBot', 'ParserBot', 'OutputBot', 'ExpertBot']
ALLOWED_SYSTEM_PARAMETERS = {'enabled', 'run_mode', 'group', 'description', 'module', 'name'}
# 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
IGNORED_SYSTEM_PARAMETERS = {'groupname', 'bot_id', 'parameters'}


class Bot(object):
Expand All @@ -53,6 +56,8 @@ class Bot(object):
# Bot is capable of SIGHUP delaying
_sighup_delay: bool = True
# From the runtime configuration
enabled: bool = True
run_mode: str = "continuous"
description: Optional[str] = None
group: Optional[str] = None
module = None
Expand Down Expand Up @@ -758,16 +763,21 @@ def __load_runtime_configuration(self):

if self.__bot_id in config:
params = config[self.__bot_id]
self.run_mode = params.get('run_mode', 'continuous')
self.name = params.get('name')
self.description = params.get('description')
self.group = params.get('group')
self.module = params.get('module')
for key, value in params.items():
if key in ALLOWED_SYSTEM_PARAMETERS and value:
self.__log_configuration_parameter("system", key, value)
setattr(self, key, value)
elif key not in IGNORED_SYSTEM_PARAMETERS:
self.logger.warning('Ignoring disallowed system parameter %r.',
key)
for option, value in params.get('parameters', {}).items():
setattr(self, option, value)
self.__log_configuration_parameter("runtime", option, value)
if option.startswith('logging_'):
reinitialize_logging = True
else:
self.logger.warning('Bot ID %r not found in runtime configuration - could not load any parameters.',
self.__bot_id)

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

if option.startswith('logging_'):
reinitialize_logging = True

if reinitialize_logging:
self.logger.handlers = [] # remove all existing handlers
self.__init_logger()
Expand Down

0 comments on commit e278acf

Please sign in to comment.