From e3e4f2ebc2e813ac3cd0bffee4ab9ba5c0dd4150 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 28 Apr 2023 10:43:28 -0400 Subject: [PATCH 1/2] ssh: add -d option to scp when copying directories The -d option of scp, as for the -t option, is an undocumented one, and is used for specifying that we are working in directory mode. In effect, this flag ensures that the target is to be a directory, and implies that scp will check that the target directory exists before attempting to copy the contents of the source. Adding this flag to the scp sink makes the communicator behave correctly in relation to the scp command as it would be used by clients, rejecting copies to a non-existent destination directory, rather than mistakenly creating it, partially (or completely) copying the source's contents, and potentially succeeding where it should not. --- sdk-internals/communicator/ssh/communicator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk-internals/communicator/ssh/communicator.go b/sdk-internals/communicator/ssh/communicator.go index ed46dd89d..24878a87e 100644 --- a/sdk-internals/communicator/ssh/communicator.go +++ b/sdk-internals/communicator/ssh/communicator.go @@ -701,7 +701,7 @@ func (c *comm) scpUploadDirSession(dst string, src string, excl []string) error } } - return c.scpSession("scp -rvt "+dst, scpFunc) + return c.scpSession("scp -rdvt "+dst, scpFunc) } func (c *comm) scpDownloadSession(path string, output io.Writer) error { From 5d34ea0ebf53c341445cdd9bea9ac6a3cca7da3a Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 28 Apr 2023 14:27:01 -0400 Subject: [PATCH 2/2] ssh: add -d option to scp file upload As with the directory upload, not specifying the -d option in scp when uploading a file may result in uploading succeeding even when the destination directory (if suffixed by a `/') succeeds, and makes the resulting file upload take the name of the destination directory. To avoid this problem, we add the option so that if the destination is explicitely a directory, this does not succeed anymore. --- sdk-internals/communicator/ssh/communicator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk-internals/communicator/ssh/communicator.go b/sdk-internals/communicator/ssh/communicator.go index 24878a87e..4ce9938ff 100644 --- a/sdk-internals/communicator/ssh/communicator.go +++ b/sdk-internals/communicator/ssh/communicator.go @@ -668,7 +668,7 @@ func (c *comm) scpUploadSession(path string, input io.Reader, fi *os.FileInfo) e return scpUploadFile(target_file, input, w, stdoutR, fi) } - return c.scpSession("scp -vt "+target_dir, scpFunc) + return c.scpSession("scp -vdt "+target_dir, scpFunc) } func (c *comm) scpUploadDirSession(dst string, src string, excl []string) error {