From 3e85eef03e15e18250ac491c5a7925aac8ea7e0a Mon Sep 17 00:00:00 2001 From: Jason Ertel Date: Thu, 29 Apr 2021 16:15:42 -0400 Subject: [PATCH] Prevent deepcopy of Jinja Template object --- elastalert/test_rule.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/elastalert/test_rule.py b/elastalert/test_rule.py index af1eaa49..d019eadd 100644 --- a/elastalert/test_rule.py +++ b/elastalert/test_rule.py @@ -417,7 +417,18 @@ def run_rule_test(self): with open(args.json, 'r') as data_file: self.data = json.loads(data_file.read()) else: - hits = self.test_file(copy.deepcopy(rule_yaml), args) + # Temporarily remove the jinja_template, if it exists, to avoid deepcopy issues + template = rule_yaml.get("jinja_template") + rule_yaml["jinja_template"] = None + + # Copy the rule object without the template in it + copied_rule = copy.deepcopy(rule_yaml) + + # Set the template back onto the original rule object and the newly copied object + rule_yaml["jinja_template"] = template + copied_rule["jinja_template"] = template + + hits = self.test_file(copied_rule, args) if hits and args.formatted_output: self.formatted_output['results'] = json.loads(json.dumps(hits)) if hits and args.save: