diff --git a/sftp-connector-demo/config/variables.yaml b/sftp-connector-demo/config/variables.yaml index 6ff84af..ea1d9c0 100644 --- a/sftp-connector-demo/config/variables.yaml +++ b/sftp-connector-demo/config/variables.yaml @@ -5,4 +5,27 @@ # please add a 'variables.yaml' in the sub directory '_'. # Variables: - #myVariable: value \ No newline at end of file + com.axonivy.connector.sftp.server: + dummy: + # The host name to the SFTP server + host: 'localhost' + + # The port number to the SFTP server + port: 22 + + # The username to the SFTP server + username: 'usr' + + # Auth type to the SFPT server + # [enum: password, ssh] + auth: 'ssh' + + # The password to the SFTP server + # [password] + password: '' + + # The path of ssh key file to SFTP server + sshkeyFilePath: 'C:\NonInstall\RebexTinySftpServer-Binaries-Latest\sshkeyBK\rsa4096new' + + # The ssh key passphrase + sshPassphraseSecret: '123456' \ No newline at end of file diff --git a/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/BaseTest.java b/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/BaseTest.java new file mode 100644 index 0000000..0d93917 --- /dev/null +++ b/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/BaseTest.java @@ -0,0 +1,34 @@ +package com.axonivy.connector.sftp.test; + +import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; +import ch.ivyteam.ivy.environment.Ivy; +import ch.ivyteam.ivy.environment.IvyTest; + +@IvyTest +public class BaseTest { + protected static final String TEST_SFTP_SERVER_NAME = "dummy"; + protected static final String TEST_SFTP_SSH_SERVER_NAME = "dummy_ssh"; + + protected static final BpmProcess TEST_HELPER_PROCESS = BpmProcess.path("Sftp/SftpHelper"); + protected static final BpmProcess TEST_UPLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpUploadFile"); + protected static final BpmProcess TEST_DOWNLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpDownloadFile"); + + protected static final String PREFIX = "com.axonivy.connector.sftp.server"; + protected static final String TEST_FILE_NAME = "market_market_connector_sftp.pdf"; + protected static final long TEST_FILE_SIZE = 207569L; + + protected static void setVarForSFTPName(String sftpServerName, String username, String auth, String password, String sshKeyFilePath, String sshpassphrase) { + setVar(sftpServerName, "host", "localhost"); + setVar(sftpServerName, "username", username); + setVar(sftpServerName, "port", "22"); + setVar(sftpServerName, "auth", auth); + setVar(sftpServerName, "password", password); + setVar(sftpServerName, "sshkeyFilePath", sshKeyFilePath); + setVar(sftpServerName, "sshPassphraseSecret", sshpassphrase); + } + + private static void setVar(String sftpServerName, String var, String value) { + Ivy.var().set(String.format("%s.%s.%s", PREFIX, sftpServerName, var), value); + } + +} diff --git a/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpMultiConnectionTest.java b/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpMultiConnectionTest.java index 26f3b19..45cfdb0 100644 --- a/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpMultiConnectionTest.java +++ b/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpMultiConnectionTest.java @@ -3,8 +3,6 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -13,38 +11,29 @@ import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; -import ch.ivyteam.ivy.environment.Ivy; /** * This SftpMultiConnectionTest creates 2 sFTP connections */ @IvyProcessTest(enableWebServer = true) -public class SftpMultiConnectionTest { +public class SftpMultiConnectionTest extends BaseTest { - private static final String PREFIX = "com.axonivy.connector.sftp.server."; - private static final String SFTP_NAME = "dummy"; private static final String SFTP_SSH_NAME = "dummy_ssh"; - private static final String SFTP_SSH_NAME_VAR = SFTP_SSH_NAME + "."; - + @BeforeEach public void preInit() throws Exception { - // set-up variables for sftp connector with SSH key pair - Ivy.var().set(PREFIX+SFTP_SSH_NAME_VAR+"host", "localhost"); - Ivy.var().set(PREFIX+SFTP_SSH_NAME_VAR+"username", "usr2ssh"); - Ivy.var().set(PREFIX+SFTP_SSH_NAME_VAR+"auth", "ssh"); - Ivy.var().set(PREFIX+SFTP_SSH_NAME_VAR+"password", ""); - String keyString = Files.readString(Paths.get(SftpProcessSSHTest.class.getResource("sftptest").toURI())); - Ivy.var().set(PREFIX+SFTP_SSH_NAME_VAR+"secret.sshkey", keyString); - Ivy.var().set(PREFIX+SFTP_SSH_NAME_VAR+"secret.sshpassphrase", "123456"); + setVarForSFTPName(TEST_SFTP_SERVER_NAME, "usr", "password", "pwd", "", ""); + String keyPath = SftpProcessSSHTest.class.getResource("sftptest").getPath(); + setVarForSFTPName(TEST_SFTP_SSH_SERVER_NAME, "usr2ssh", "ssh", "", keyPath, "123456"); } @Test public void callOpenConnection(BpmClient bpmClient) throws IOException { SftpClientService sftpClient = new SftpClientService(SFTP_NAME); SftpClientService sftpSSHClient = new SftpClientService(SFTP_SSH_NAME); - + assertThat(sftpClient).isNotNull(); assertThat(sftpSSHClient).isNotNull(); sftpClient.close(); diff --git a/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpProcessSSHTest.java b/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpProcessSSHTest.java index c38256f..eb8a4b8 100644 --- a/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpProcessSSHTest.java +++ b/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpProcessSSHTest.java @@ -21,7 +21,6 @@ import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.engine.client.sub.SubProcessCallResult; import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; -import ch.ivyteam.ivy.environment.Ivy; import ch.ivyteam.ivy.scripting.objects.File; @@ -39,28 +38,12 @@ *

*/ @IvyProcessTest(enableWebServer = true) -public class SftpProcessSSHTest { - - private static final BpmProcess TEST_HELPER_PROCESS = BpmProcess.path("Sftp/SftpHelper"); - private static final BpmProcess TEST_UPLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpUploadFile"); - private static final BpmProcess TEST_DOWNLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpDownloadFile"); - - private static final String TEST_SFTP_NAME = "dummy"; - private static final String TEST_SFTP_NAME_VAR = TEST_SFTP_NAME + "."; - private static final String TEST_FILE_NAME = "market_market_connector_sftp.pdf"; - private static final long TEST_FILE_SIZE = 207569L; - - private static final String PREFIX = "com.axonivy.connector.sftp.server."; +public class SftpProcessSSHTest extends BaseTest { @BeforeAll public static void init() throws Exception { - Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"username", "usr2ssh"); - Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"auth", "ssh"); - Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"password", ""); - - String keyString = Files.readString(Paths.get(SftpProcessSSHTest.class.getResource("sftptest").toURI())); - Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"secret.sshkey", keyString); - Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"secret.sshpassphrase", "123456"); + String keyPath = SftpProcessSSHTest.class.getResource("sftptest").getPath(); + setVarForSFTPName(TEST_SFTP_SSH_SERVER_NAME, "usr2ssh", "ssh", "", keyPath, "123456"); } @Test @@ -70,7 +53,7 @@ public void callOpenConnection(BpmClient bpmClient) throws Exception { SubProcessCallResult result = bpmClient.start() .subProcess(startable) - .execute(TEST_SFTP_NAME) // Callable sub process input arguments + .execute(TEST_SFTP_SSH_SERVER_NAME) // Callable sub process input arguments .subResult(); SftpClientService sftpClient = result.param("sftpClient", SftpClientService.class); @@ -89,7 +72,7 @@ public void callUploadFile(BpmClient bpmClient) { SubProcessCallResult result = bpmClient.start() .subProcess(startable) - .execute(TEST_SFTP_NAME,fileToBeUploaded, TEST_FILE_NAME) // Callable sub process input arguments + .execute(TEST_SFTP_SSH_SERVER_NAME,fileToBeUploaded, TEST_FILE_NAME) // Callable sub process input arguments .subResult(); Boolean isSuccess = result.param("isSuccess", Boolean.class); @@ -110,7 +93,7 @@ public void callUploadIvyFile(BpmClient bpmClient) throws IOException { SubProcessCallResult result = bpmClient.start() .subProcess(startable) - .execute(TEST_SFTP_NAME, ivyFile) // Callable sub process input arguments + .execute(TEST_SFTP_SSH_SERVER_NAME, ivyFile) // Callable sub process input arguments .subResult(); Boolean isSuccess = result.param("isSuccess", Boolean.class); @@ -124,7 +107,7 @@ public void callListAllFiles(BpmClient bpmClient) { SubProcessCallResult result = bpmClient.start() .subProcess(startable) - .execute(TEST_SFTP_NAME, ".") // Callable sub process input arguments + .execute(TEST_SFTP_SSH_SERVER_NAME, ".") // Callable sub process input arguments .subResult(); List listFiles = result.param("listFiles", List.class); assertThat(listFiles.size()).isGreaterThanOrEqualTo(1); @@ -138,7 +121,7 @@ public void callDownloadFile(BpmClient bpmClient) { SubProcessCallResult result = bpmClient.start() .subProcess(startable) - .execute(TEST_SFTP_NAME, TEST_FILE_NAME) // Callable sub process input arguments + .execute(TEST_SFTP_SSH_SERVER_NAME, TEST_FILE_NAME) // Callable sub process input arguments .subResult(); java.io.File downloadedFile = result.param("toFile", java.io.File.class); assertThat(downloadedFile.length()).isEqualTo(TEST_FILE_SIZE); diff --git a/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpProcessTest.java b/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpProcessTest.java index 82b2b27..c707cab 100644 --- a/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpProcessTest.java +++ b/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpProcessTest.java @@ -7,6 +7,7 @@ import java.util.List; import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -15,7 +16,6 @@ import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.engine.client.sub.SubProcessCallResult; import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; import ch.ivyteam.ivy.scripting.objects.File; @@ -35,17 +35,13 @@ *

*/ @IvyProcessTest(enableWebServer = true) -public class SftpProcessTest { - - private static final BpmProcess TEST_HELPER_PROCESS = BpmProcess.path("Sftp/SftpHelper"); - private static final BpmProcess TEST_UPLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpUploadFile"); - private static final BpmProcess TEST_DOWNLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpDownloadFile"); - - private static final String TEST_SFTP_SERVER_NAME = "dummy"; - private static final String TEST_FILE_NAME = "market_market_connector_sftp.pdf"; - private static final long TEST_FILE_SIZE = 207569L; +public class SftpProcessTest extends BaseTest { + + @BeforeEach + public void preInit() throws Exception { + setVarForSFTPName(TEST_SFTP_SERVER_NAME, "usr", "password", "pwd", "", ""); + } - @Test @Order(1) public void callOpenConnection(BpmClient bpmClient) { diff --git a/sftp-connector/config/variables.yaml b/sftp-connector/config/variables.yaml index 39bc1d0..9d11829 100644 --- a/sftp-connector/config/variables.yaml +++ b/sftp-connector/config/variables.yaml @@ -3,13 +3,13 @@ Variables: com.axonivy.connector.sftp.server: dummy: # The host name to the SFTP server - host: 'localhost' + host: '' # The port number to the SFTP server port: 22 # The username to the SFTP server - username: 'usr' + username: '' # Auth type to the SFPT server # [enum: password, ssh] @@ -17,11 +17,10 @@ Variables: # The password to the SFTP server # [password] - password: pwd + password: '' - # The ssh key string to SFTP server - # [secret private key] - secret.sshkey: '' + # The path of ssh key file to SFTP server + sshkeyFilePath: '' # The ssh key passphrase - secret.sshpassphrase: '' + sshPassphraseSecret: '' diff --git a/sftp-connector/src/com/axonivy/connector/sftp/service/SftpClientService.java b/sftp-connector/src/com/axonivy/connector/sftp/service/SftpClientService.java index fe98451..b926ee7 100644 --- a/sftp-connector/src/com/axonivy/connector/sftp/service/SftpClientService.java +++ b/sftp-connector/src/com/axonivy/connector/sftp/service/SftpClientService.java @@ -5,6 +5,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -35,8 +37,8 @@ public class SftpClientService implements AutoCloseable { private static final String SFTP_VAR = "com.axonivy.connector.sftp.server"; private static final String HOST_VAR = "host"; private static final String PORT_VAR = "port"; - private static final String SECRET_SSHPASSPHRASE_VAR = "secret.sshpassphrase"; - private static final String SECRET_SSHKEY_VAR = "secret.sshkey"; + private static final String SECRET_SSHPASSPHRASE_VAR = "sshPassphraseSecret"; + private static final String SSHKEY_FILEPATH_VAR = "sshkeyFilePath"; private static final String AUTH_VAR = "auth"; private static final String PASSWORD_VAR = "password"; private static final String USERNAME_VAR = "username"; @@ -61,7 +63,7 @@ public SftpClientService(String sftpName) throws IOException { String username = getUsername(sftpName); String password = getVar(sftpName, PASSWORD_VAR); String auth = getVar(sftpName, AUTH_VAR); - String secretSSHkey = getVar(sftpName, SECRET_SSHKEY_VAR); + String sshKeyFilePath = getVar(sftpName, SSHKEY_FILEPATH_VAR); String secretSSHpassphrase = getVar(sftpName, SECRET_SSHPASSPHRASE_VAR); int port = 22; @@ -80,8 +82,9 @@ public SftpClientService(String sftpName) throws IOException { if (StringUtils.isEmpty(auth) || PASSWORD.name().equalsIgnoreCase(auth)) { session.setPassword(password); } else { + byte[] sshKeyBytes = Files.readAllBytes(Paths.get(sshKeyFilePath)); session.setConfig("PreferredAuthentications", "publickey"); - jsch.addIdentity(null, secretSSHkey.getBytes(), null, secretSSHpassphrase.getBytes()); + jsch.addIdentity(null, sshKeyBytes, null, secretSSHpassphrase.getBytes()); } session.setConfig("StrictHostKeyChecking", "no"); // 10 seconds session timeout