Skip to content

Fix relative paths in ParamikoSSHClient.put() and putfo() - #2261

Open
abedalawieh wants to merge 1 commit into
apache:trunkfrom
abedalawieh:fix/sftp-cwd-between-uploads
Open

abedalawieh wants to merge 1 commit into
apache:trunkfrom
abedalawieh:fix/sftp-cwd-between-uploads

Conversation

@abedalawieh

Copy link
Copy Markdown

Fix relative paths in ParamikoSSHClient.put() and putfo()

Description

Since #1483, ParamikoSSHClient reuses one SFTP client for every put() / putfo() call. Both methods start a relative path with sftp.chdir(".") ("start from a home directory"), but on a reused client "." is whatever directory the previous call changed into, so relative paths were resolved against the wrong directory. deploy_node() runs all deployment steps over one client, so this shows up with ordinary deployments.

Checked against a real sshd (paramiko 5.0.0), trunk vs this PR:

Call trunk this PR
MultiStepDeployment([FileDeployment(src, "/etc/myapp/app.cfg"), SSHKeyDeployment(key)]) key written to /etc/myapp/.ssh/authorized_keys ~/.ssh/authorized_keys
put("sub1/a.txt") returns /root/sub1/sub1/a.txt /root/sub1/a.txt
putfo("b.txt") (after the above) lands in /root/sub1/b.txt /root/b.txt
putfo("sub2/d.txt"), FileDeployment(src, "conf/app.cfg") FileNotFoundError uploaded under ~
ScriptDeployment(script, name="scripts/run.sh") exit 127, /root/scripts/scripts/scripts/run.sh: No such file runs, exit 0

Changes:

  • put() / putfo(): for a relative path, call sftp.chdir(None) (paramiko's reset to the server's default directory) before sftp.chdir("."), and read the base directory before descending into the path's directories, so the returned path no longer repeats them.
  • putfo(): upload to the file name, since it has already changed into the target directory (it uploaded to the full relative path before, which only worked without a directory).
  • ScriptDeployment.run(): for a relative name, use the full path that put() returns (as BaseSSHClient.put() documents) instead of joining dirname(file_path) and name again. For names without a directory the result is the same; this also avoids the os.path.join that closed PR fix linux command deployment when running libcloud on windows #1602 was about.

Tests: test_put_and_putfo_relative_path_in_subdirectory uses a small fake SFTP client that tracks the working directory like paramiko does, test_script_deployment_relative_path_in_subdirectory covers ScriptDeployment, and the existing relative-path tests now also check the reset. The new tests fail on trunk and pass with this change; all tests in libcloud/test/compute pass.

This, #2259 and #2260 all add an entry at the top of the Compute section of CHANGES.rst, so the later ones will need a trivial rebase.

Status

  • done, ready for review

Checklist (tick everything that applies)

  • Code linting (black, isort, flake8 on the changed files)
  • Documentation
  • Tests
  • ICLA (required for bigger changes)

Since the SFTP client is reused between calls, put() and putfo()
starting a relative path with sftp.chdir(".") resolved it against the
directory the previous call changed to rather than the home directory.
For example, SSHKeyDeployment after a FileDeployment to
/etc/app/app.cfg wrote /etc/app/.ssh/authorized_keys.

Relative paths with a directory were also broken: putfo() changed into
the directory and then uploaded to the full relative path, which failed,
and both methods returned the directory twice in the file's path.
ScriptDeployment then joined the directory onto that path again, so a
script with such a name couldn't be run.

Reset the SFTP working directory before a relative upload, take the
base directory before descending into the path's directories, upload to
the file name in putfo(), and let ScriptDeployment use the full path
that put() returns.
@abedalawieh
abedalawieh force-pushed the fix/sftp-cwd-between-uploads branch from 7eca0ac to 6733f1c Compare December 6, 2026 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant