From cea669eeaa444d0eeab1075d22447860bffc2725 Mon Sep 17 00:00:00 2001 From: guzzijones12 Date: Tue, 25 Jan 2022 21:50:32 +0000 Subject: [PATCH 1/4] check if value is actually jinja before adding as template --- st2common/st2common/util/param.py | 33 ++++++++++++++++++------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/st2common/st2common/util/param.py b/st2common/st2common/util/param.py index 52e1f025fd..4094fb2c93 100644 --- a/st2common/st2common/util/param.py +++ b/st2common/st2common/util/param.py @@ -19,7 +19,7 @@ import six import networkx as nx -from jinja2 import meta +from jinja2 import meta, exceptions from oslo_config import cfg from st2common import log as logging from st2common.util.config_loader import get_config @@ -133,19 +133,24 @@ def _process(G, name, value): ) or jinja_utils.is_jinja_expression(complex_value_str) if is_jinja_expr: - G.add_node(name, template=value) - - template_ast = ENV.parse(value) - LOG.debug("Template ast: %s", template_ast) - # Dependencies of the node represent jinja variables used in the template - # We're connecting nodes with an edge for every depencency to traverse them - # in the right order and also make sure that we don't have missing or cyclic - # dependencies upfront. - dependencies = meta.find_undeclared_variables(template_ast) - LOG.debug("Dependencies: %s", dependencies) - if dependencies: - for dependency in dependencies: - G.add_edge(dependency, name) + try: + template_ast = ENV.parse(value) + G.add_node(name, template=value) + + LOG.debug("Template ast: %s", template_ast) + # Dependencies of the node represent jinja variables used in the template + # We're connecting nodes with an edge for every depencency to traverse them + # in the right order and also make sure that we don't have missing or cyclic + # dependencies upfront. + dependencies = meta.find_undeclared_variables(template_ast) + LOG.debug("Dependencies: %s", dependencies) + if dependencies: + for dependency in dependencies: + G.add_edge(dependency, name) + except exceptions.TemplateSyntaxError as e: + G.add_node(name, value=value) + # not jinja after all + # is_jinga_expression only checks for {{ or {{% for speed else: G.add_node(name, value=value) From 37b5259fe03298d31ebe42cd388e50179557468f Mon Sep 17 00:00:00 2001 From: guzzijones12 Date: Wed, 26 Jan 2022 19:25:48 +0000 Subject: [PATCH 2/4] add unit test for _process params.py --- st2common/st2common/util/param.py | 2 +- st2common/tests/unit/test_param_utils.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/st2common/st2common/util/param.py b/st2common/st2common/util/param.py index 4094fb2c93..90617a78d9 100644 --- a/st2common/st2common/util/param.py +++ b/st2common/st2common/util/param.py @@ -147,7 +147,7 @@ def _process(G, name, value): if dependencies: for dependency in dependencies: G.add_edge(dependency, name) - except exceptions.TemplateSyntaxError as e: + except exceptions.TemplateSyntaxError: G.add_node(name, value=value) # not jinja after all # is_jinga_expression only checks for {{ or {{% for speed diff --git a/st2common/tests/unit/test_param_utils.py b/st2common/tests/unit/test_param_utils.py index c2e5810815..592ecd802d 100644 --- a/st2common/tests/unit/test_param_utils.py +++ b/st2common/tests/unit/test_param_utils.py @@ -54,6 +54,26 @@ class ParamsUtilsTest(DbTestCase): action_system_default_db = FIXTURES["actions"]["action_system_default.yaml"] runnertype_db = FIXTURES["runners"]["testrunner1.yaml"] + def test_process_jinja_exception(self): + + action_context = {"api_user": "noob"} + config = {} + G = param_utils._create_graph(action_context, config) + name = "a1" + value = "http://someurl?value={{a" + param_utils._process(G, name, value) + self.assertEquals(G.nodes.get(name, {}).get("value"), value) + + def test_process_jinja_template(self): + + action_context = {"api_user": "noob"} + config = {} + G = param_utils._create_graph(action_context, config) + name = "a1" + value = "http://someurl?value={{a}}" + param_utils._process(G, name, value) + self.assertEquals(G.nodes.get(name, {}).get("template"), value) + def test_get_finalized_params(self): params = { "actionstr": "foo", From 0b710a18e13f3103cfef6b04ffbff35ca52774b4 Mon Sep 17 00:00:00 2001 From: guzzijones12 Date: Wed, 26 Jan 2022 19:54:21 +0000 Subject: [PATCH 3/4] edit unit test _process parameter values of a dict --- st2common/tests/unit/test_param_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/st2common/tests/unit/test_param_utils.py b/st2common/tests/unit/test_param_utils.py index 592ecd802d..77c88b1cd3 100644 --- a/st2common/tests/unit/test_param_utils.py +++ b/st2common/tests/unit/test_param_utils.py @@ -60,7 +60,7 @@ def test_process_jinja_exception(self): config = {} G = param_utils._create_graph(action_context, config) name = "a1" - value = "http://someurl?value={{a" + value = {"test": "http://someurl?value={{a"} param_utils._process(G, name, value) self.assertEquals(G.nodes.get(name, {}).get("value"), value) From 6fad929d25eee88dc7e6857a4acc35347dbdc38c Mon Sep 17 00:00:00 2001 From: guzzijones12 Date: Wed, 26 Jan 2022 20:39:45 +0000 Subject: [PATCH 4/4] update changelog; jinja parameter fix --- CHANGELOG.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 6cd8eb2f6a..856922ba04 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,6 +12,10 @@ Fixed Contributed by @blackstrip +* Fix exception thrown if action parameter contains {{ or {% and no closing jinja characters. #5556 + + contributed by @guzzijones12 + Added ~~~~~ @@ -4244,4 +4248,4 @@ v0.5.1 - November 3rd, 2014 Added ~~~~~ -* Initial public release \ No newline at end of file +* Initial public release