Skip to content

Commit

Permalink
Fix urlunparse for salt file urls
Browse files Browse the repository at this point in the history
Previously, the `salt.utils.url.create()` method naively assumed the
compiled URL provided by `urllib.parse.urlunparse()` always starts with
the literal string `file:///`, that is, the begininning of the URL
string starting with the `scheme` and including an empty `authority`
section.  Beginning with
[python-3.12](python/cpython@0edfc66),
however, generating a URL with `urlunparse()` results in the syntax
`file:` for the portion of the URL including the `scheme` and
`authority` sections.

Since `urlunparse()` accepts the arbitrary scheme `salt`, use it instead
and then normalize the rendered scheme to `salt://` as SaltStack
expects.
  • Loading branch information
jfindlay committed Oct 7, 2024
1 parent 58c89a8 commit 6beaae3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions salt/utils/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ def create(path, saltenv=None):
path = salt.utils.data.decode(path)

query = f"saltenv={saltenv}" if saltenv else ""
url = salt.utils.data.decode(urlunparse(("file", "", path, "", query, "")))
return "salt://{}".format(url[len("file:///") :])
url = salt.utils.data.decode(urlunparse(("salt", "", path, "", query, "")))
# urllib may return `salt:` or `salt:///`, but salt wants `salt://`, so normalize here
if url.startswith("salt:{path}"):
return f"salt://{url.split('salt:')[1]}"
elif url.startswith("salt://{path}"):
return url
elif url.startswith("salt:///{path}"):
return f"salt://{url.split('salt:///')[1]}"
raise ValueError(f"Cannot interpret {url!r} as a 'salt://' URL")


def is_escaped(url):
Expand Down
File renamed without changes.

0 comments on commit 6beaae3

Please sign in to comment.