Skip to content

Commit

Permalink
Merge pull request #2420 from monoidic/jinja-template-type
Browse files Browse the repository at this point in the history
FIX: prevent jinja expert bot crash on logrotate
  • Loading branch information
sebix committed Nov 14, 2023
2 parents d693561 + b2d71de commit df26d61
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions intelmq/bots/experts/jinja/expert.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,33 @@ 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:///"):
continue

templatefile = pathlib.Path(template[7:])
if not (templatefile.exists() and os.access(templatefile, os.R_OK)):
raise ValueError(f"Jinja Template {templatefile} does not exist or is not readable.")
self.fields[field] = templatefile.read_text()

for field, template in self.fields.items():
try:
self.fields[field] = Template(template)
self._templates[field] = Template(template)
except TemplateError as msg:
raise ValueError(f"Error parsing Jinja Template for '{field}': {msg}")

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 df26d61

Please sign in to comment.