From 784662ab5de9c69b1b5a2f24e4f1dddd250111b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikk=20Margus=20M=C3=B6ll?= Date: Wed, 1 Nov 2023 09:15:11 +0200 Subject: [PATCH] FIX: prevent jinja expert bot crash on logrotate --- intelmq/bots/experts/jinja/expert.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/intelmq/bots/experts/jinja/expert.py b/intelmq/bots/experts/jinja/expert.py index 32b18d0ec..bd54fd5a8 100644 --- a/intelmq/bots/experts/jinja/expert.py +++ b/intelmq/bots/experts/jinja/expert.py @@ -27,7 +27,8 @@ class JinjaExpertBot(ExpertBot): extra.somejinjaoutput: file:///etc/intelmq/somejinjatemplate.j2 """ - fields: Dict[str, Union[str, Template]] = {} + fields: Dict[str, str] = {} + _templates: Dict[str, Union[str, Template]] = {} overwrite: bool = False def init(self): @@ -35,14 +36,14 @@ def init(self): raise MissingDependencyError("jinja2") for field, template in self.fields.items(): - if template.startswith("file:///"): - templatefile = pathlib.Path(template[7:]) - if templatefile.exists() and os.access(templatefile, os.R_OK): - self.fields[field] = templatefile.read_text() - else: - raise ValueError(f"Jinja Template {templatefile} does not exist or is not readable.") + if not template.startswith("file:///"): + raise ValueError(f"Jinja Template {templatefile} does not exist or is not readable.") - for field, template in self.fields.items(): + templatefile = pathlib.Path(template[7:]) + if templatefile.exists() and os.access(templatefile, os.R_OK): + self._templates[field] = templatefile.read_text() + + for field, template in self._templates.items(): try: self.fields[field] = Template(template) except TemplateError as msg: @@ -51,7 +52,7 @@ def init(self): def process(self): msg = self.receive_message() - for field, template in self.fields.items(): + for field, template in self._templates.items(): msg.add(field, template.render(msg=msg), overwrite=self.overwrite) self.send_message(msg)