From 222dc1da48c9b510c74463f643ebd91b6a8cef47 Mon Sep 17 00:00:00 2001 From: Jake Cohen Date: Thu, 22 Feb 2024 15:23:25 -0800 Subject: [PATCH] initial add of sftp support to scp file copier --- .../com/plugin/sshjplugin/SSHJBuilder.java | 3 +- .../sshjplugin/SSHJFileCopierPlugin.java | 4 +++ .../sshjplugin/SSHJNodeExecutorPlugin.java | 9 +++++ .../com/plugin/sshjplugin/model/SSHJBase.java | 2 ++ .../sshjplugin/model/SSHJConnection.java | 2 ++ .../model/SSHJConnectionParameters.java | 5 +++ .../com/plugin/sshjplugin/model/SSHJScp.java | 34 +++++++++++++++++-- 7 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/plugin/sshjplugin/SSHJBuilder.java b/src/main/java/com/plugin/sshjplugin/SSHJBuilder.java index 37cec0b..6295459 100644 --- a/src/main/java/com/plugin/sshjplugin/SSHJBuilder.java +++ b/src/main/java/com/plugin/sshjplugin/SSHJBuilder.java @@ -79,7 +79,7 @@ static void buildRecursiveScp(final SSHJScp scp, //Set the local and remote file paths //scp.setLocalFile(sourceFolder.getAbsolutePath()); scp.addFile(sourceFolder); - + scp.setUseSftp(sshConnection.useSftp()); scp.setTodir(remotePath); } @@ -101,6 +101,7 @@ static void buildScp(final SSHJScp scp, configureSSHBase(nodeentry, sshjConnection, scp, logger); + scp.setUseSftp(sshjConnection.useSftp()); //Set the local and remote file paths scp.setLocalFile(sourceFile.getAbsolutePath()); scp.setRemoteTofile(remotepath); diff --git a/src/main/java/com/plugin/sshjplugin/SSHJFileCopierPlugin.java b/src/main/java/com/plugin/sshjplugin/SSHJFileCopierPlugin.java index 73c23d0..cc6e138 100644 --- a/src/main/java/com/plugin/sshjplugin/SSHJFileCopierPlugin.java +++ b/src/main/java/com/plugin/sshjplugin/SSHJFileCopierPlugin.java @@ -52,6 +52,8 @@ public class SSHJFileCopierPlugin extends BaseFileCopier implements MultiFileCop .property(SSHJNodeExecutorPlugin.SSH_KEEP_ALIVE_INTERVAL) .property(SSHJNodeExecutorPlugin.SSH_KEEP_ALIVE_MAX_ALIVE_COUNT) .property(SSHJNodeExecutorPlugin.SSH_RETRY_COUNTER) + .property(SSHJNodeExecutorPlugin.USE_SFTP) + .mapping(SSHJNodeExecutorPlugin.CONFIG_KEYPATH, SSHJNodeExecutorPlugin.PROJ_PROP_SSH_KEYPATH) .mapping(SSHJNodeExecutorPlugin.CONFIG_AUTHENTICATION, SSHJNodeExecutorPlugin.PROJ_PROP_SSH_AUTHENTICATION) @@ -69,6 +71,8 @@ public class SSHJFileCopierPlugin extends BaseFileCopier implements MultiFileCop .frameworkMapping(SSHJNodeExecutorPlugin.CONFIG_KEEP_ALIVE_MAX_ALIVE_COUNT, SSHJNodeExecutorPlugin.FWK_PROP_SSH_KEEP_ALIVE_MAX_ALIVE_COUNT) .mapping(SSHJNodeExecutorPlugin.CONFIG_RETRY_ENABLE, SSHJNodeExecutorPlugin.PROJ_PROP_RETRY_ENABLE) .frameworkMapping(SSHJNodeExecutorPlugin.CONFIG_RETRY_ENABLE, SSHJNodeExecutorPlugin.FWK_PROP_RETRY_ENABLE) + .mapping(SSHJNodeExecutorPlugin.CONFIG_USE_SFTP, SSHJNodeExecutorPlugin.PROJ_PROP_USE_SFTP) + .frameworkMapping(SSHJNodeExecutorPlugin.CONFIG_USE_SFTP, SSHJNodeExecutorPlugin.FWK_PROP_USE_SFTP) .mapping(SSHJNodeExecutorPlugin.CONFIG_RETRY_COUNTER, SSHJNodeExecutorPlugin.PROJ_PROP_RETRY_COUNTER) .frameworkMapping(SSHJNodeExecutorPlugin.CONFIG_RETRY_COUNTER, SSHJNodeExecutorPlugin.FWK_PROP_RETRY_COUNTER) .build(); diff --git a/src/main/java/com/plugin/sshjplugin/SSHJNodeExecutorPlugin.java b/src/main/java/com/plugin/sshjplugin/SSHJNodeExecutorPlugin.java index e0fd3b8..1f1499a 100644 --- a/src/main/java/com/plugin/sshjplugin/SSHJNodeExecutorPlugin.java +++ b/src/main/java/com/plugin/sshjplugin/SSHJNodeExecutorPlugin.java @@ -50,6 +50,7 @@ public class SSHJNodeExecutorPlugin implements NodeExecutor, ProxySecretBundleCr public static final String CONFIG_RETRY_ENABLE = "retryEnable"; public static final String CONFIG_RETRY_COUNTER = "retryCounter"; + public static final String CONFIG_USE_SFTP = "useSftp"; public static final String PROJ_PROP_PREFIX = "project."; public static final String FWK_PROP_PREFIX = "framework."; @@ -74,6 +75,7 @@ public class SSHJNodeExecutorPlugin implements NodeExecutor, ProxySecretBundleCr public static final String NODE_ATTR_RETRY_COUNTER = "retry-counter"; public static final String NODE_ATTR_RETRY_ENABLE = "retry-enable"; + public static final String NODE_ATTR_USE_SFTP = "use-sftp"; public static final String PROJECT_SSH_USER = PROJ_PROP_PREFIX + "ssh.user"; @@ -100,6 +102,9 @@ public class SSHJNodeExecutorPlugin implements NodeExecutor, ProxySecretBundleCr public static final String FWK_PROP_RETRY_ENABLE = FWK_PROP_PREFIX + NODE_ATTR_RETRY_ENABLE; public static final String PROJ_PROP_RETRY_ENABLE = PROJ_PROP_PREFIX + NODE_ATTR_RETRY_ENABLE; + public static final String FWK_PROP_USE_SFTP = FWK_PROP_PREFIX + NODE_ATTR_USE_SFTP; + public static final String PROJ_PROP_USE_SFTP = PROJ_PROP_PREFIX + NODE_ATTR_USE_SFTP; + public static final String SUDO_OPT_PREFIX = "sudo-"; public static final String DEFAULT_SUDO_PROMPT_PATTERN = "[sudo] password for"; @@ -173,6 +178,10 @@ public class SSHJNodeExecutorPlugin implements NodeExecutor, ProxySecretBundleCr "Enable a connection retry when the connection fails", false, "false"); + static final Property USE_SFTP = PropertyUtil.bool(CONFIG_USE_SFTP, "Use SFTP", + "Use SFTP for file transfer", + false, "false"); + private SSHClient sshClient; public void setSshClient(SSHClient sshClient) { diff --git a/src/main/java/com/plugin/sshjplugin/model/SSHJBase.java b/src/main/java/com/plugin/sshjplugin/model/SSHJBase.java index 675f37d..0affe6a 100644 --- a/src/main/java/com/plugin/sshjplugin/model/SSHJBase.java +++ b/src/main/java/com/plugin/sshjplugin/model/SSHJBase.java @@ -68,6 +68,7 @@ public void connect(SSHClient ssh){ int keepAliveCount = sshjConnection.getKeepAliveMaxAlive(); boolean retry = sshjConnection.isRetryEnabled(); int retryCount = sshjConnection.getRetryCounter(); + boolean useSftp = sshjConnection.useSftp(); SSHJAuthentication authentication = new SSHJAuthentication(sshjConnection, pluginLogger); @@ -78,6 +79,7 @@ public void connect(SSHClient ssh){ pluginLogger.log(3, "["+getPluginName()+"] keepAliveMaxCount: " + keepAliveCount); pluginLogger.log(3, "["+getPluginName()+"] retry: " + retry); pluginLogger.log(3, "["+getPluginName()+"] retryCount: " + retryCount); + pluginLogger.log(3, "["+getPluginName()+"] useSftp: " + useSftp); ssh.getTransport().getConfig().setLoggerFactory(new SSHJPluginLoggerFactory(pluginLogger)); ssh.addHostKeyVerifier(new PromiscuousVerifier()); diff --git a/src/main/java/com/plugin/sshjplugin/model/SSHJConnection.java b/src/main/java/com/plugin/sshjplugin/model/SSHJConnection.java index 91acba5..c8905b4 100644 --- a/src/main/java/com/plugin/sshjplugin/model/SSHJConnection.java +++ b/src/main/java/com/plugin/sshjplugin/model/SSHJConnection.java @@ -49,6 +49,8 @@ static enum AuthenticationType { int getConnectTimeout(); + boolean useSftp(); + int getKeepAliveInterval(); int getKeepAliveMaxAlive(); diff --git a/src/main/java/com/plugin/sshjplugin/model/SSHJConnectionParameters.java b/src/main/java/com/plugin/sshjplugin/model/SSHJConnectionParameters.java index bea6372..a564279 100644 --- a/src/main/java/com/plugin/sshjplugin/model/SSHJConnectionParameters.java +++ b/src/main/java/com/plugin/sshjplugin/model/SSHJConnectionParameters.java @@ -206,6 +206,11 @@ public boolean matchesCommandPattern(final String command) { } } + @Override + public boolean useSftp() { + return propertyResolver.resolveBoolean(SSHJNodeExecutorPlugin.NODE_ATTR_USE_SFTP); + } + @Override public int getKeepAliveInterval() { diff --git a/src/main/java/com/plugin/sshjplugin/model/SSHJScp.java b/src/main/java/com/plugin/sshjplugin/model/SSHJScp.java index 654627b..77d435f 100644 --- a/src/main/java/com/plugin/sshjplugin/model/SSHJScp.java +++ b/src/main/java/com/plugin/sshjplugin/model/SSHJScp.java @@ -4,6 +4,7 @@ import com.plugin.sshjplugin.SSHJBuilder; import net.schmizz.sshj.SSHClient; import net.schmizz.sshj.xfer.FileSystemFile; +import net.schmizz.sshj.sftp.SFTPClient; import java.io.File; import java.io.IOException; @@ -17,6 +18,10 @@ public class SSHJScp extends SSHJBase { private List fileSets; private String localFile; + private SFTPClient sftp; + + private boolean useSftp; + public void setTodir(final String aToUri) { this.toDir = aToUri; } @@ -29,6 +34,10 @@ public void setRemoteTofile(String s) { this.remoteTofile = s; } + public void setUseSftp(boolean useSftp) { + this.useSftp = useSftp; + } + public void addFile(final File set) { if (fileSets == null) { fileSets = new ArrayList<>(); @@ -42,23 +51,42 @@ public SSHJScp() { public void execute(SSHClient ssh) throws Exception { + if(useSftp) { + sftp = ssh.newSFTPClient(); + } + pluginLogger.log(3, "["+this.getPluginName()+"] SSHJ File Copier"); try { if (this.localFile != null && this.fileSets == null) { if (toDir != null && remoteTofile == null) { pluginLogger.log(3, "["+getPluginName()+"] Copying file " + this.localFile + " to " + toDir); - ssh.newSCPFileTransfer().upload(new FileSystemFile(this.localFile), toDir); + + if(useSftp) { + sftp.put(new FileSystemFile(this.localFile), toDir); + } else { + ssh.newSCPFileTransfer().upload(new FileSystemFile(this.localFile), toDir); + } } else { pluginLogger.log(3, "["+getPluginName()+"] Copying file " + this.localFile + " to " + remoteTofile); - ssh.newSCPFileTransfer().upload(new FileSystemFile(this.localFile), remoteTofile); + + if(useSftp) { + sftp.put(new FileSystemFile(this.localFile), remoteTofile); + } else { + ssh.newSCPFileTransfer().upload(new FileSystemFile(this.localFile), remoteTofile); + } } } else if (this.fileSets != null && this.fileSets.size() > 0) { for(File file:this.fileSets) { pluginLogger.log(3, "["+getPluginName()+"] Copying file " + file.getAbsolutePath() + " to " + toDir); try { - ssh.newSCPFileTransfer().upload(new FileSystemFile(file), toDir); + + if(useSftp) { + sftp.put(new FileSystemFile(this.localFile), toDir); + } else { + ssh.newSCPFileTransfer().upload(new FileSystemFile(this.localFile), toDir); + } } catch (IOException e) { e.printStackTrace(); }