Skip to content

Commit

Permalink
initial add of sftp support to scp file copier
Browse files Browse the repository at this point in the history
  • Loading branch information
jsboak committed Feb 22, 2024
1 parent 4b9283e commit 222dc1d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/plugin/sshjplugin/SSHJBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/plugin/sshjplugin/SSHJFileCopierPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand All @@ -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";

Expand All @@ -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";
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/plugin/sshjplugin/model/SSHJBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/plugin/sshjplugin/model/SSHJConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ static enum AuthenticationType {

int getConnectTimeout();

boolean useSftp();

int getKeepAliveInterval();

int getKeepAliveMaxAlive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
34 changes: 31 additions & 3 deletions src/main/java/com/plugin/sshjplugin/model/SSHJScp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +18,10 @@ public class SSHJScp extends SSHJBase {
private List<File> fileSets;
private String localFile;

private SFTPClient sftp;

private boolean useSftp;

public void setTodir(final String aToUri) {
this.toDir = aToUri;
}
Expand All @@ -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<>();
Expand All @@ -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();
}
Expand Down

0 comments on commit 222dc1d

Please sign in to comment.