Skip to content

Commit

Permalink
FIX: prevent jinja expert bot crash on logrotate
Browse files Browse the repository at this point in the history
  • Loading branch information
monoidic committed Nov 1, 2023
1 parent d624988 commit 784662a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions intelmq/bots/experts/jinja/expert.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,23 @@ 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):
if not Template:
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:
Expand All @@ -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)
Expand Down

0 comments on commit 784662a

Please sign in to comment.