Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templates: move the globals up to the Environment (Jinja2 3.0.0) #60811

Merged
merged 2 commits into from
Sep 1, 2021

Conversation

aplanas
Copy link
Contributor

@aplanas aplanas commented Aug 30, 2021

What does this PR do?

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
pallets/jinja#295

This patch populate the globals in the Environment level, making this
and other variables reachable by the Jinja templates.

Also fix a wrong call to TemplateNotFound

What issues does this PR fix or reference?

Fixes: #55159

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

Commits signed with GPG?

Yes

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 <[email protected]>
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
pallets/jinja#295

This patch populate the globals in the Environment level, making this
and other variables reachable by the Jinja templates.

Fix saltstack#55159

Signed-off-by: Alberto Planas <[email protected]>
@garethgreenaway
Copy link
Contributor

garethgreenaway commented Aug 31, 2021

@aplanas Thanks for the fix, this also resolves another issue that was reported related to the changes that went into Jinja2 3.0.x. We'll want to add some additional tests for this scenario and move these existing tests over to Pytest. If you don't have time to migrate these tests, a member of the Salt core team can help out.

salt/utils/templates.py Show resolved Hide resolved
@aplanas aplanas requested a review from waynew September 1, 2021 15:01
@garethgreenaway garethgreenaway merged commit 7275ecf into saltstack:master Sep 1, 2021
@baby-gnu
Copy link

baby-gnu commented Sep 2, 2021

Is it planned to backport this fix or is there a strong dependency between Jinja2 3.0 and salt?

i.e. will it be required to upgrade salt version when a distribution starts using newer Jinja2?

@aplanas
Copy link
Contributor Author

aplanas commented Sep 2, 2021

In my test the patch is safe to backport. I tested master with the patch against Jinja2 2.10.X, 2.11.X and 3.0.1. More tests are welcome (but note that the CI was green, and none is using 3.0.1)

@myii
Copy link
Contributor

myii commented Sep 2, 2021

I've done a before and after comparison using the Formula team's pre-salted images. We've been downgrading Jinja2 until now, so all our instances have been working. There are a few issues with installing on Gentoo with the salt-bootstrap so we'll ignore Gentoo 3000.9 failures below.

Before:

  • All instances are passing.

After:

  • The following instances are now failing (where git or stable is shown in the table):
Distro master tiamat 3003.2 3003.1 3002.6 3001.7 3000.9
Arch Linux git stable git git
Fedora 33 git git
Fedora 34 git git
Gentoo (latest) git stable git git
Gentoo (systemd) git stable git git
openSUSE Leap 15.2 git git git
openSUSE Leap 15.3 git git git
openSUSE Tumbleweed git stable git
  • Notes:
    • Showing git or stable to clarify the Salt installation method that was used with the bootstrap script.
    • stable on openSUSE for 3002.X is actually 3002.2.
    • I wouldn't be surprised to find that stable for 3003.2 on both Arch Linux and Gentoo will also fail, once they are actually released (not available at the time of writing this comment).
    • Also had to downgrade Jinja2 on our FreeBSD Vagrant boxes -- will report back these findings when I get a chance to work with those.

saltstack-formulas-github pushed a commit to netmanagers/salt-image-builder that referenced this pull request Sep 2, 2021
Fix has been released:

* saltstack/salt#60811

Maintain Jinja2 downgrades where necessary, as outlined here:

* saltstack/salt#60811 (comment)
@garethgreenaway
Copy link
Contributor

We're not planning to back port any fixes at this time unless it's considered a CVE issue, which this one is not. This fix will be available in the Silicon release.

@damon-atkins
Copy link
Contributor

damon-atkins commented Sep 3, 2021

I assume old OS may not have access to the new Jinja2. Might consider running test case against both versions, to ensure salt works with both? In house code or other software already installed may not support newer version, if salt supplies a new version and it overrides the existing version.

@baby-gnu
Copy link

baby-gnu commented Sep 3, 2021

We're not planning to back port any fixes at this time unless it's considered a CVE issue, which this one is not. This fix will be available in the Silicon release.

This seems a reasonable policy, I think some special notes should be in the documentation for users to easily spot possible troubles, something like this release of salt does not work with Jinja2 >= 3.0.

Or maybe in the dependencies where the Jinja2 should have < 3.0 mentioned.

Regards.

dwoz pushed a commit to dwoz/salt that referenced this pull request Sep 12, 2021
…ltstack#60811)

* 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 <[email protected]>

* 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
pallets/jinja#295

This patch populate the globals in the Environment level, making this
and other variables reachable by the Jinja templates.

Fix saltstack#55159

Signed-off-by: Alberto Planas <[email protected]>
bdrung pushed a commit to bdrung/salt that referenced this pull request Oct 13, 2021
…ltstack#60811)

* 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 <[email protected]>

* 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
pallets/jinja#295

This patch populate the globals in the Environment level, making this
and other variables reachable by the Jinja templates.

Fix saltstack#55159
Bug-Debian: https://bugs.debian.org/996271
Origin: upstream, saltstack#60811
Signed-off-by: Alberto Planas <[email protected]>
bdrung pushed a commit to bdrung/salt that referenced this pull request Oct 14, 2021
…ltstack#60811)

* 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 <[email protected]>

* 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
pallets/jinja#295

This patch populate the globals in the Environment level, making this
and other variables reachable by the Jinja templates.

Fix saltstack#55159
Bug-Debian: https://bugs.debian.org/996271
Origin: upstream, saltstack#60811
Signed-off-by: Alberto Planas <[email protected]>
s0undt3ch pushed a commit to saltstack/salt-bootstrap that referenced this pull request Mar 15, 2022
With default version 3003.3, you run into saltstack/salt#55159, which was fixed by saltstack/salt#60811
If you want to install from git master, it fails because py2-pip is unavailable - Alpine 3.11 was the last version to include py2-pip
This PR changes the git master process to python3 and adds all requirements for a successful build
saltstack-formulas-github pushed a commit to netmanagers/salt-image-builder that referenced this pull request Apr 3, 2022
Check for problems associated with this bug:

* saltstack/salt#60188
  - Affects use of `grains`, `opts`, `pillar` & `salt` in Jinja.

Fix was first available in `3004`:

* saltstack/salt#60811

Use tests to apply the Jinja2 downgrade only where necessary
(and to ensure that it is actually working).
wbh1 pushed a commit to wbh1/salt that referenced this pull request Jul 7, 2022
…ltstack#60811)

* 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 <[email protected]>

* 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
pallets/jinja#295

This patch populate the globals in the Environment level, making this
and other variables reachable by the Jinja templates.

Fix saltstack#55159

Signed-off-by: Alberto Planas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Silicon v3004.0 Release code name
Projects
None yet
8 participants