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

fix(utils/url): handle Windows relative path backslash when caching file #61829

Merged
merged 1 commit into from
Apr 11, 2022

Conversation

myii
Copy link
Contributor

@myii myii commented Mar 22, 2022

What does this PR do?

Jinja imports with relative paths was introduced in #47490.

Unfortunately, relative imports have never been working on Windows.
That's due to backslashes being unhandled by the cache_file function:

Instead of getting:

  • salt://packages/map.jinja

We're ending up with:

  • salt://packages\map.jinja

Thus, the file isn't cached from the master.

There's actually a FIXME note about it:

And the replacement is actually done for setting tpldir:

This commit ensures the replacement is also done when caching files.

CC: @twangboy.

What issues does this PR fix or reference?

Fix #61040.

Merge requirements satisfied?

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

Commits signed with GPG?

Yes

@myii myii requested a review from a team as a code owner March 22, 2022 13:04
@myii myii requested review from waynew and removed request for a team March 22, 2022 13:04
@github-actions

This comment was marked as outdated.

@waynew
Copy link
Contributor

waynew commented Mar 25, 2022

🙌 Awesome find/fix!

We discussed this a bit on Slack, and I think we've come to the conclusion that this is the right fix in the wrong place 🙃

Specifically, my thinking is that salt:// is a URI (and arguably URL, and certainly URN as well), and since RFC 2396 doesn't allow the backslash, and even file:// uses forward slashes for Windows paths... I'm confident that salt:// URI/URLs should always use / from within a Salt perspective. I'm OK if backslashes accidentally work because Python or whatever, but our documentation should (if it doesn't already) state that salt:// URI/URLs should only use forward slashes, regardless of the salt master or salt minion operating system.

Naturally, the Salt master & minion should translate salt:// URLs to the appropriate underlying file paths to match their OSes preference, but I'm pretty sure that's already happening.

Soooo all of that being said, I'm totally on board with this fix, but since the documentation says that we're creating a salt:// url (

join `path` and `saltenv` into a 'salt://' URL.
) then we should make sure that nothing comes out of that function wearing \ - / only.

@myii myii force-pushed the fix/bug-61040 branch 2 times, most recently from b009244 to e2dd512 Compare March 26, 2022 06:24
@myii
Copy link
Contributor Author

myii commented Mar 26, 2022

Thanks for the advice, @waynew. I made the adjustments and added a couple of tests. These are the failures before the proposed fix is applied:

======================================================================
FAIL: test_create_url_with_backslash_in_path (tests.unit.utils.test_url.UrlTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../salt/tests/unit/utils/test_url.py", line 81, in test_create_url_with_backslash_in_path
    self.assertEqual(salt.utils.url.create(src_path), url)
AssertionError: 'salt://? interesting\\&path.filetype' != 'salt://? interesting/&path.filetype'
- salt://? interesting\&path.filetype
?                     ^
+ salt://? interesting/&path.filetype
?                     ^


======================================================================
FAIL: test_create_url_saltenv_with_backslash_in_path (tests.unit.utils.test_url.UrlTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../salt/tests/unit/utils/test_url.py", line 95, in test_create_url_saltenv_with_backslash_in_path
    self.assertEqual(salt.utils.url.create(src_path, saltenv), url)
AssertionError: 'salt://? interesting\\&path.filetype?saltenv=raumklang' != 'salt://? interesting/&path.filetype?saltenv=raumklang'
- salt://? interesting\&path.filetype?saltenv=raumklang
?                     ^
+ salt://? interesting/&path.filetype?saltenv=raumklang
?                     ^

Copy link
Contributor

@waynew waynew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://jenkins.saltproject.io/job/pr-windows-2019-x64-py3-pytest/job/PR-61829/3/testReport/tests.pytests.unit.utils.jinja/test_salt_cache_loader/test_relative_import/ Mentioned in chat, but looks like there's a test that was requesting the broken behavior 🙃

Should just be a matter of changing the \ to / in that test, though I'd double check to make sure that it really is an incorrect test 🤔

@myii myii changed the title fix(jinja): handle Windows relative path backslash when caching file fix(utils/url): handle Windows relative path backslash when caching file Mar 28, 2022
Fix saltstack#61040.

Jinja imports with relative paths was introduced in saltstack#47490.

Unfortunately, relative imports have never been working on Windows.
That's due to backslashes being unhandled by the `cache_file` function:

* https://github.com/saltstack/salt/blob/7275ecf073197faed26f3a571c25fcd59bfbfd60/salt/utils/jinja.py#L114

Instead of getting:

* `salt://packages/map.jinja`

We're ending up with:

* `salt://packages\map.jinja`

Thus, the file isn't cached from the master.

There's actually a `FIXME` note about it:

* https://github.com/saltstack/salt/blob/7275ecf073197faed26f3a571c25fcd59bfbfd60/salt/utils/jinja.py#L136

And the replacement is actually done for setting `tpldir`:

* https://github.com/saltstack/salt/blob/7275ecf073197faed26f3a571c25fcd59bfbfd60/salt/utils/jinja.py#L165

This commit ensures the replacement is also done when caching files.
@waynew
Copy link
Contributor

waynew commented Apr 8, 2022

Heeeey! Checks all passing! 👍 must've just been some flaky infrastructure :suspect:

@Ch3LL Ch3LL merged commit fe68301 into saltstack:master Apr 11, 2022
@myii myii deleted the fix/bug-61040 branch April 11, 2022 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Phosphorus v3005.0 Release code name and version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Relative path not working on master-minion setup with windows minion
4 participants