d0e1a8d380
OBS-URL: https://build.opensuse.org/package/show/systemsmanagement:saltstack/salt?expand=0&rev=190
114 lines
4.2 KiB
Diff
114 lines
4.2 KiB
Diff
From 4664c4b31fdef9ef5b3987b9d993ccba2675d18f Mon Sep 17 00:00:00 2001
|
|
From: Alberto Planas <aplanas@suse.com>
|
|
Date: Tue, 31 Aug 2021 11:20:49 +0200
|
|
Subject: [PATCH] templates: move the globals up to the Environment
|
|
(Jinja2 3.0.0) (#418)
|
|
|
|
* jinja: fix TemplateNotFound missing name
|
|
|
|
The TemplateNotFound exception requires a parameter, name, that is
|
|
missing in one of the calls.
|
|
|
|
File "/usr/lib/python3.8/site-packages/salt/utils/jinja.py", line 158, in get_source
|
|
raise TemplateNotFound
|
|
TypeError: __init__() missing 1 required positional argument: 'name'
|
|
|
|
This patch add the missing parameter in the raise call.
|
|
|
|
Signed-off-by: Alberto Planas <aplanas@suse.com>
|
|
|
|
* templates: move the globals up to the Environment
|
|
|
|
When creating a Jinja2 environment, we populate the globals in the
|
|
Template object that we generate from the environment. This cause a
|
|
problem when there is a {% include "./file.sls" %} in the template, as
|
|
cannot find in the environment globals information like the "tpldir",
|
|
for example, making the relative path to be unresolved.
|
|
|
|
Seems that in Jinja2 2.X this behaviour is not present, so attaching the
|
|
globals to the Template will make the include to work, but since Jinja2
|
|
3.0.0 this is not the case. Maybe related with the re-architecture from
|
|
https://github.com/pallets/jinja/issues/295
|
|
|
|
This patch populate the globals in the Environment level, making this
|
|
and other variables reachable by the Jinja templates.
|
|
|
|
Fix #55159
|
|
|
|
Signed-off-by: Alberto Planas <aplanas@suse.com>
|
|
---
|
|
changelog/55159.fixed | 1 +
|
|
salt/utils/jinja.py | 2 +-
|
|
salt/utils/templates.py | 3 ++-
|
|
tests/unit/utils/test_jinja.py | 16 ++++++++++++++++
|
|
4 files changed, 20 insertions(+), 2 deletions(-)
|
|
create mode 100644 changelog/55159.fixed
|
|
|
|
diff --git a/changelog/55159.fixed b/changelog/55159.fixed
|
|
new file mode 100644
|
|
index 0000000000..6ee1a78366
|
|
--- /dev/null
|
|
+++ b/changelog/55159.fixed
|
|
@@ -0,0 +1 @@
|
|
+Jinja renderer resolves wrong relative paths when importing subdirectories
|
|
diff --git a/salt/utils/jinja.py b/salt/utils/jinja.py
|
|
index 68a8646474..3f27c2c7e1 100644
|
|
--- a/salt/utils/jinja.py
|
|
+++ b/salt/utils/jinja.py
|
|
@@ -148,7 +148,7 @@ class SaltCacheLoader(BaseLoader):
|
|
'Relative path "%s" cannot be resolved without an environment',
|
|
template,
|
|
)
|
|
- raise TemplateNotFound
|
|
+ raise TemplateNotFound(template)
|
|
base_path = environment.globals["tpldir"]
|
|
_template = os.path.normpath("/".join((base_path, _template)))
|
|
if _template.split("/", 1)[0] == "..":
|
|
diff --git a/salt/utils/templates.py b/salt/utils/templates.py
|
|
index 0f705cfa37..4c59553bc8 100644
|
|
--- a/salt/utils/templates.py
|
|
+++ b/salt/utils/templates.py
|
|
@@ -494,9 +494,10 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None):
|
|
SLS_ENCODING,
|
|
)
|
|
decoded_context[key] = salt.utils.data.decode(value)
|
|
+
|
|
+ jinja_env.globals.update(decoded_context)
|
|
try:
|
|
template = jinja_env.from_string(tmplstr)
|
|
- template.globals.update(decoded_context)
|
|
output = template.render(**decoded_context)
|
|
except jinja2.exceptions.UndefinedError as exc:
|
|
trace = traceback.extract_tb(sys.exc_info()[2])
|
|
diff --git a/tests/unit/utils/test_jinja.py b/tests/unit/utils/test_jinja.py
|
|
index fb67e17fa2..e23caf11ae 100644
|
|
--- a/tests/unit/utils/test_jinja.py
|
|
+++ b/tests/unit/utils/test_jinja.py
|
|
@@ -619,6 +619,22 @@ class TestGetTemplate(TestCase):
|
|
dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
|
|
)
|
|
|
|
+ def test_relative_include(self):
|
|
+ template = "{% include './hello_import' %}"
|
|
+ expected = "Hey world !a b !"
|
|
+ filename = os.path.join(self.template_dir, "hello_import")
|
|
+ with salt.utils.files.fopen(filename) as fp_:
|
|
+ out = render_jinja_tmpl(
|
|
+ template,
|
|
+ dict(
|
|
+ opts=self.local_opts,
|
|
+ saltenv="test",
|
|
+ salt=self.local_salt,
|
|
+ tpldir=self.template_dir,
|
|
+ ),
|
|
+ )
|
|
+ self.assertEqual(out, expected)
|
|
+
|
|
|
|
class TestJinjaDefaultOptions(TestCase):
|
|
@classmethod
|
|
--
|
|
2.33.0
|
|
|
|
|