Skip to content

Commit 3e1b63b

Browse files
committed
Update SSH connector to properly quote filenames.
1 parent 97d92b2 commit 3e1b63b

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

pyinfra/api/connectors/ssh.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import pyinfra
2727

2828
from pyinfra import logger
29+
from pyinfra.api.command import QuoteString, StringCommand
2930
from pyinfra.api.exceptions import ConnectError, PyinfraError
3031
from pyinfra.api.util import get_file_io, memoize
3132

@@ -420,15 +421,15 @@ def put_file(
420421
_put_file(host, filename_or_io, temp_file)
421422

422423
# Execute run_shell_command w/sudo and/or su_user
423-
command = 'mv {0} {1}'.format(temp_file, remote_filename)
424+
command = StringCommand('mv', temp_file, QuoteString(remote_filename))
424425

425426
# Move it to the su_user if present
426427
if su_user:
427-
command = '{0} && chown {1} {2}'.format(command, su_user, remote_filename)
428+
command = StringCommand(command, '&&', 'chown', su_user, QuoteString(remote_filename))
428429

429430
# Otherwise any sudo_user
430431
elif sudo_user:
431-
command = '{0} && chown {1} {2}'.format(command, sudo_user, remote_filename)
432+
command = StringCommand(command, '&&', 'chown', sudo_user, QuoteString(remote_filename))
432433

433434
status, _, stderr = run_shell_command(
434435
state, host, command,

tests/test_connectors/test_ssh.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ def test_put_file_sudo(self, fake_sftp_client, fake_ssh_client):
498498
fake_open = mock_open(read_data='test!')
499499
with patch('pyinfra.api.util.open', fake_open, create=True):
500500
status = host.put_file(
501-
'not-a-file', 'not-another-file',
501+
'not-a-file', 'not another file',
502502
print_output=True,
503503
sudo=True,
504504
sudo_user='ubuntu',
@@ -508,12 +508,13 @@ def test_put_file_sudo(self, fake_sftp_client, fake_ssh_client):
508508

509509
fake_ssh_client().exec_command.assert_called_with((
510510
"sudo -H -n -u ubuntu sh -c 'mv "
511-
'/tmp/pyinfra-43db9984686317089fefcf2e38de527e4cb44487 '
512-
"not-another-file && chown ubuntu not-another-file'"
511+
'/tmp/pyinfra-de01e82cb691e8a31369da3c7c8f17341c44ac24 '
512+
"\'\"\'\"\'not another file\'\"\'\"\' && chown ubuntu "
513+
"\'\"\'\"\'not another file\'\"\'\"\'\'"
513514
), get_pty=False)
514515

515516
fake_sftp_client.from_transport().putfo.assert_called_with(
516-
fake_open(), '/tmp/pyinfra-43db9984686317089fefcf2e38de527e4cb44487',
517+
fake_open(), '/tmp/pyinfra-de01e82cb691e8a31369da3c7c8f17341c44ac24',
517518
)
518519

519520
@patch('pyinfra.api.connectors.ssh.SSHClient')

0 commit comments

Comments
 (0)