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

[BUG] file.managed does not work correctly if file path contains a '#' #63060

Closed
3 of 9 tasks
jerrykan opened this issue Nov 14, 2022 · 1 comment
Closed
3 of 9 tasks
Assignees
Labels
Bug broken, incorrect, or confusing behavior needs-triage

Comments

@jerrykan
Copy link
Contributor

Description
The file.managed state does not work correctly if the file path contains a # character.

On the first run the file will be correctly created (if it doesn't already exist), but any subsequent run will fail to manage the file correctly.

Setup
Example state file:

# poc.sls
tmp-dir:
  file.directory:
    - name: /tmp/weird#folder/

weird-file:
  file.managed:
    - name: '/tmp/weird#folder/file'
    - contents: Original content
    - require:
      - file: tmp-dir

Please be as specific as possible and give set-up details.

  • on-prem machine
  • VM (Virtualbox, KVM, etc. please specify) - Virtualbox/vSphere
  • VM running on a cloud service, please be explicit and add details
  • container (Kubernetes, Docker, containerd, etc. please specify)
  • or a combination, please be explicit
  • jails if it is FreeBSD
  • classic packaging
  • onedir packaging
  • used bootstrap to install

Steps to Reproduce the behavior
The initial run:

# salt-call state.apply --output-diff poc
local:
----------
          ID: weird-file
    Function: file.managed
        Name: /tmp/weird#folder/file
      Result: True
     Comment: File /tmp/weird#folder/file updated
     Started: 11:47:28.625839
    Duration: 2.269 ms
     Changes:   
              ----------
              diff:
                  New file

Summary for local
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time:   7.262 ms

Running again after the weird-file contents field has been changes (ie. to contents: Updated content)

# salt-call state.apply --output-diff poc
local:

Summary for local
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time:  15.518 ms

Note no change, when the file contents should have been updated to Updated content

Doing the same thing again but this time as a test run:

# salt-call state.apply --output-diff poc test=True
[ERROR   ] Failed to diff files: Failed to read /tmp/weird: No such file or directory
local:
----------
          ID: weird-file
    Function: file.managed
        Name: /tmp/weird#folder/file
      Result: None
     Comment: The file /tmp/weird#folder/file is set to be changed
              Note: No changes made, actual changes may
              be different due to other states.
     Started: 11:50:22.090511
    Duration: 10.408 ms
     Changes:   
              ----------
              diff:
                  Failed to read /tmp/weird: No such file or directory

Summary for local
------------
Succeeded: 2 (unchanged=1, changed=1)
Failed:    0
------------
Total states run:     2
Total run time:  15.271 ms

Note that Salt is trying to diff on the incorrect file /tmp/weird instead of /tmp/weird#folder/file

Expected behavior
The file should be managed the same as any other file that doesn't contain a # in its path name.

Screenshots
N/A

Versions Report

# salt-call --versions-report
Salt Version:
          Salt: 3005.1
 
Dependency Versions:
          cffi: Not Installed
      cherrypy: Not Installed
      dateutil: 2.8.1
     docker-py: Not Installed
         gitdb: Not Installed
     gitpython: Not Installed
        Jinja2: 3.0.3
       libgit2: Not Installed
      M2Crypto: Not Installed
          Mako: Not Installed
       msgpack: 1.0.3
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: Not Installed
      pycrypto: Not Installed
  pycryptodome: 3.11.0
        pygit2: Not Installed
        Python: 3.10.6 (main, Nov  2 2022, 18:53:38) [GCC 11.3.0]
  python-gnupg: 0.4.8
        PyYAML: 5.4.1
         PyZMQ: 22.3.0
         smmap: Not Installed
       timelib: Not Installed
       Tornado: 4.5.3
           ZMQ: 4.3.4
 
System Versions:
          dist: ubuntu 22.04 Jammy Jellyfish
        locale: utf-8
       machine: x86_64
       release: 5.15.0-52-generic
        system: Linux
       version: Ubuntu 22.04 Jammy Jellyfish

Additional context
As far as I can tell the problem seems to stem from the use of URL parsing of local filenames in /usr/lib/python3/dist-packages/salt/fileclient.py(482)get_url()

  /usr/lib/python3/dist-packages/salt/modules/file.py(5832)get_diff()
-> cached_path = __salt__["cp.cache_file"](
  /usr/lib/python3/dist-packages/salt/loader/lazy.py(149)__call__()
-> return self.loader.run(run_func, *args, **kwargs)
  /usr/lib/python3/dist-packages/salt/loader/lazy.py(1228)run()
-> return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
  /usr/lib/python3/dist-packages/salt/loader/lazy.py(1243)_run_as()
-> return _func_or_method(*args, **kwargs)
  /usr/lib/python3/dist-packages/salt/modules/cp.py(554)cache_file()
-> result = _client().cache_file(
  /usr/lib/python3/dist-packages/salt/fileclient.py(186)cache_file()
-> return self.get_url(
> /usr/lib/python3/dist-packages/salt/fileclient.py(482)get_url()
-> url_path = os.path.join(url_data.netloc, url_data.path).rstrip(os.sep)
@jerrykan jerrykan added Bug broken, incorrect, or confusing behavior needs-triage labels Nov 14, 2022
@Akm0d Akm0d self-assigned this Mar 26, 2024
@Akm0d
Copy link
Contributor

Akm0d commented Apr 11, 2024

#66302

@Akm0d Akm0d closed this as completed Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug broken, incorrect, or confusing behavior needs-triage
Projects
None yet
Development

No branches or pull requests

2 participants