From ecb35becbab0c0f3571dae8cf93fa2db1e232733 Mon Sep 17 00:00:00 2001 From: Mahalakshmithirumurthy2301 Date: Thu, 28 Nov 2024 16:03:44 +0530 Subject: [PATCH 1/7] S-115548 : zos Create Overthere sshHostZosUnix composite connection --- .../ssh/ZosSshConnectionProvider.java | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/main/java/com/xebialabs/overthere/ssh/ZosSshConnectionProvider.java diff --git a/src/main/java/com/xebialabs/overthere/ssh/ZosSshConnectionProvider.java b/src/main/java/com/xebialabs/overthere/ssh/ZosSshConnectionProvider.java new file mode 100644 index 00000000..955ea8ed --- /dev/null +++ b/src/main/java/com/xebialabs/overthere/ssh/ZosSshConnectionProvider.java @@ -0,0 +1,125 @@ +package com.xebialabs.overthere.ssh; + +import java.io.IOException; +import static com.xebialabs.overthere.ConnectionOptions.*; +import static com.xebialabs.overthere.ConnectionOptions.OPERATING_SYSTEM; +import static com.xebialabs.overthere.OperatingSystemFamily.UNIX; +import static com.xebialabs.overthere.ssh.SshConnectionBuilder.CONNECTION_TYPE; +import static com.xebialabs.overthere.ssh.SshConnectionType.SCP; +import static com.xebialabs.overthere.ssh.SshConnectionType.SFTP; + +import com.xebialabs.overthere.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ZosSshConnectionProvider { + public static void main(String[] args) { + ConnectionOptions options = new ConnectionOptions(); + options.set(ADDRESS, "172.18.61.12"); + options.set(USERNAME, "ubuntu"); + options.set(PASSWORD, "devopsqe@123"); + options.set(OPERATING_SYSTEM, UNIX); + options.set(CONNECTION_TYPE, SFTP); + + ZosConnection zosConnection = new ZosConnection(options); + System.out.println(zosConnection.getOverthereForScp()); + System.out.println(zosConnection.getOverthereForSftp()); + zosConnection.checkConnection(); + zosConnection.checkConnection(SshConnectionType.SFTP); + } +} + +class ZosConnection { + private static final Logger logger = LoggerFactory.getLogger(ZosConnection.class); + private SshConnection overthereSshScp; + private SshConnection overthereSshSftp; + + public ZosConnection(ConnectionOptions targetOptions) { + try { + ConnectionOptions scpOptions = new ConnectionOptions(targetOptions); + scpOptions.set(CONNECTION_TYPE, SCP); + this.overthereSshScp = (SshConnection) Overthere.getConnection("ssh", scpOptions); + } catch (Exception e) { + this.overthereSshScp = null; + } + + try { + ConnectionOptions sftpOptions = new ConnectionOptions(targetOptions); + sftpOptions.set(CONNECTION_TYPE, SFTP); + this.overthereSshSftp = (SshConnection) Overthere.getConnection("ssh", sftpOptions); + } catch (Exception e) { + this.overthereSshSftp = null; + } + } + + public SshConnection getOverthereForScp() { + return overthereSshScp; + } + + public SshConnection getOverthereForSftp() { + return overthereSshSftp; + } + + public SshConnection getOverthere(SshConnectionType connectionType) { + if (connectionType == SFTP) { + return overthereSshSftp; + } else if (connectionType == SCP) { + return overthereSshScp; + } else { + throw new IllegalArgumentException("Unsupported connection type: " + connectionType); + } + } + + public void checkConnection(SshConnectionType connectionType){ + if (connectionType == SFTP) { + if (overthereSshSftp == null) { + throw new IllegalArgumentException("No SFTP connection available"); + } + echoTmpDirContents(overthereSshSftp); + } else if (connectionType == SCP) { + if (overthereSshScp == null) { + throw new IllegalArgumentException("No SCP connection available"); + } + echoTmpDirContents(overthereSshScp); + } else { + throw new IllegalArgumentException("Unsupported connection type: " + connectionType); + } + } + + public void checkConnection(){ + if (overthereSshSftp != null) echoTmpDirContents(overthereSshSftp); + if (overthereSshScp != null) echoTmpDirContents(overthereSshScp); + if (overthereSshSftp == null && overthereSshScp == null) { + throw new IllegalArgumentException("No connection available"); + } + } + + public void close() throws IOException { + if (overthereSshSftp != null) { + overthereSshSftp.close(); + } + if (overthereSshScp != null) { + overthereSshScp.close(); + } + } + + private void echoTmpDirContents(SshConnection connection) { + System.out.println("Listing the contents of the temporary directory using "+ connection.protocolAndConnectionType +" connection on host " + connection.host); + CmdLine cmdLine = new CmdLine(); + if (connection.getHostOperatingSystem() == OperatingSystemFamily.WINDOWS) { + cmdLine.addArgument("cmd"); + cmdLine.addArgument("/c"); + cmdLine.addArgument("dir"); + } else { + cmdLine.addArgument("ls"); + } + + String tempDir = connection.getHostOperatingSystem().getDefaultTemporaryDirectoryPath(); + cmdLine.addArgument(tempDir); + int i = connection.execute(cmdLine); + System.out.println("Successfully executed commands on " + connection.host + "."); + if (i != 0) { + System.out.println("Failed to execute command for connection type "+ connection.protocolAndConnectionType +" on host" + connection.host +". Return code was ["+ i +"]. Please check the logs."); + } + } +} \ No newline at end of file From ed01def523101d6bdead52e490afbcafd628aec6 Mon Sep 17 00:00:00 2001 From: Mahalakshmithirumurthy2301 Date: Thu, 28 Nov 2024 19:13:49 +0530 Subject: [PATCH 2/7] Optimization --- .../ssh/ZosSshConnectionProvider.java | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/xebialabs/overthere/ssh/ZosSshConnectionProvider.java b/src/main/java/com/xebialabs/overthere/ssh/ZosSshConnectionProvider.java index 955ea8ed..0310e4ce 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/ZosSshConnectionProvider.java +++ b/src/main/java/com/xebialabs/overthere/ssh/ZosSshConnectionProvider.java @@ -1,6 +1,8 @@ package com.xebialabs.overthere.ssh; import java.io.IOException; +import java.util.Objects; + import static com.xebialabs.overthere.ConnectionOptions.*; import static com.xebialabs.overthere.ConnectionOptions.OPERATING_SYSTEM; import static com.xebialabs.overthere.OperatingSystemFamily.UNIX; @@ -19,7 +21,6 @@ public static void main(String[] args) { options.set(USERNAME, "ubuntu"); options.set(PASSWORD, "devopsqe@123"); options.set(OPERATING_SYSTEM, UNIX); - options.set(CONNECTION_TYPE, SFTP); ZosConnection zosConnection = new ZosConnection(options); System.out.println(zosConnection.getOverthereForScp()); @@ -61,9 +62,9 @@ public SshConnection getOverthereForSftp() { } public SshConnection getOverthere(SshConnectionType connectionType) { - if (connectionType == SFTP) { + if (connectionType.equals(SFTP)) { return overthereSshSftp; - } else if (connectionType == SCP) { + } else if (connectionType.equals(SCP)) { return overthereSshScp; } else { throw new IllegalArgumentException("Unsupported connection type: " + connectionType); @@ -71,13 +72,13 @@ public SshConnection getOverthere(SshConnectionType connectionType) { } public void checkConnection(SshConnectionType connectionType){ - if (connectionType == SFTP) { - if (overthereSshSftp == null) { + if (connectionType.equals(SFTP)) { + if (Objects.isNull(overthereSshSftp)) { throw new IllegalArgumentException("No SFTP connection available"); } echoTmpDirContents(overthereSshSftp); - } else if (connectionType == SCP) { - if (overthereSshScp == null) { + } else if (connectionType.equals(SCP)) { + if (Objects.isNull(overthereSshScp)) { throw new IllegalArgumentException("No SCP connection available"); } echoTmpDirContents(overthereSshScp); @@ -87,18 +88,18 @@ public void checkConnection(SshConnectionType connectionType){ } public void checkConnection(){ - if (overthereSshSftp != null) echoTmpDirContents(overthereSshSftp); - if (overthereSshScp != null) echoTmpDirContents(overthereSshScp); - if (overthereSshSftp == null && overthereSshScp == null) { + if (Objects.nonNull(overthereSshSftp)) echoTmpDirContents(overthereSshSftp); + if (Objects.nonNull(overthereSshScp)) echoTmpDirContents(overthereSshScp); + if (Objects.isNull(overthereSshSftp) && Objects.isNull(overthereSshScp)) { throw new IllegalArgumentException("No connection available"); } } public void close() throws IOException { - if (overthereSshSftp != null) { + if (Objects.nonNull(overthereSshSftp)) { overthereSshSftp.close(); } - if (overthereSshScp != null) { + if (Objects.nonNull(overthereSshScp)) { overthereSshScp.close(); } } @@ -106,20 +107,15 @@ public void close() throws IOException { private void echoTmpDirContents(SshConnection connection) { System.out.println("Listing the contents of the temporary directory using "+ connection.protocolAndConnectionType +" connection on host " + connection.host); CmdLine cmdLine = new CmdLine(); - if (connection.getHostOperatingSystem() == OperatingSystemFamily.WINDOWS) { - cmdLine.addArgument("cmd"); - cmdLine.addArgument("/c"); - cmdLine.addArgument("dir"); - } else { - cmdLine.addArgument("ls"); - } - + cmdLine.addArgument("ls"); String tempDir = connection.getHostOperatingSystem().getDefaultTemporaryDirectoryPath(); cmdLine.addArgument(tempDir); - int i = connection.execute(cmdLine); - System.out.println("Successfully executed commands on " + connection.host + "."); - if (i != 0) { - System.out.println("Failed to execute command for connection type "+ connection.protocolAndConnectionType +" on host" + connection.host +". Return code was ["+ i +"]. Please check the logs."); + int returnCode = connection.execute(cmdLine); + if (returnCode != 0) { + System.out.println("Failed to execute command for connection type "+ connection.protocolAndConnectionType +" on host" + connection.host +". Return code was ["+ returnCode +"]. Please check the logs."); + } + else { + System.out.println("Successfully executed commands on " + connection.host + "."); } } } \ No newline at end of file From d8c3999e6fbbe63f7d497041929f456238c5c076 Mon Sep 17 00:00:00 2001 From: Mahalakshmithirumurthy2301 Date: Mon, 9 Dec 2024 15:18:09 +0530 Subject: [PATCH 3/7] Removed checkConnection method --- .../overthere/ssh/ZosConnection.java | 55 ++++++++ .../ssh/ZosSshConnectionProvider.java | 121 ------------------ 2 files changed, 55 insertions(+), 121 deletions(-) create mode 100644 src/main/java/com/xebialabs/overthere/ssh/ZosConnection.java delete mode 100644 src/main/java/com/xebialabs/overthere/ssh/ZosSshConnectionProvider.java diff --git a/src/main/java/com/xebialabs/overthere/ssh/ZosConnection.java b/src/main/java/com/xebialabs/overthere/ssh/ZosConnection.java new file mode 100644 index 00000000..11110292 --- /dev/null +++ b/src/main/java/com/xebialabs/overthere/ssh/ZosConnection.java @@ -0,0 +1,55 @@ +package com.xebialabs.overthere.ssh; + +import static com.xebialabs.overthere.ssh.SshConnectionBuilder.CONNECTION_TYPE; +import static com.xebialabs.overthere.ssh.SshConnectionType.SCP; +import static com.xebialabs.overthere.ssh.SshConnectionType.SFTP; + +import com.xebialabs.overthere.ConnectionOptions; +import com.xebialabs.overthere.Overthere; + +public class ZosConnection { + private SshConnection overthereSshScp; + private SshConnection overthereSshSftp; + + public ZosConnection(ConnectionOptions options) { + initializeConnections(options); + } + + private void initializeConnections(ConnectionOptions targetOptions) { + try { + ConnectionOptions scpOptions = new ConnectionOptions(targetOptions); + scpOptions.set(CONNECTION_TYPE, SCP); + this.overthereSshScp = (SshConnection) Overthere.getConnection("ssh", scpOptions); + } catch (Exception e) { + this.overthereSshScp = null; + } + + try { + ConnectionOptions sftpOptions = new ConnectionOptions(targetOptions); + sftpOptions.set(CONNECTION_TYPE, SFTP); + this.overthereSshSftp = (SshConnection) Overthere.getConnection("ssh", sftpOptions); + } catch (Exception e) { + this.overthereSshSftp = null; + } + } + + public SshConnection getConnectionForScp() { return overthereSshScp; } + + public SshConnection getConnectionForSftp() { + return overthereSshSftp; + } + + public SshConnection getConnection(SshConnectionType connectionType) { + if (connectionType.equals(SFTP)) { + return overthereSshSftp; + } else if (connectionType.equals(SCP)) { + return overthereSshScp; + } else { + throw new IllegalArgumentException("Unsupported connection type: " + connectionType); + } + } + + public ZosConnection getConnection() { + return this; + } +} \ No newline at end of file diff --git a/src/main/java/com/xebialabs/overthere/ssh/ZosSshConnectionProvider.java b/src/main/java/com/xebialabs/overthere/ssh/ZosSshConnectionProvider.java deleted file mode 100644 index 0310e4ce..00000000 --- a/src/main/java/com/xebialabs/overthere/ssh/ZosSshConnectionProvider.java +++ /dev/null @@ -1,121 +0,0 @@ -package com.xebialabs.overthere.ssh; - -import java.io.IOException; -import java.util.Objects; - -import static com.xebialabs.overthere.ConnectionOptions.*; -import static com.xebialabs.overthere.ConnectionOptions.OPERATING_SYSTEM; -import static com.xebialabs.overthere.OperatingSystemFamily.UNIX; -import static com.xebialabs.overthere.ssh.SshConnectionBuilder.CONNECTION_TYPE; -import static com.xebialabs.overthere.ssh.SshConnectionType.SCP; -import static com.xebialabs.overthere.ssh.SshConnectionType.SFTP; - -import com.xebialabs.overthere.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ZosSshConnectionProvider { - public static void main(String[] args) { - ConnectionOptions options = new ConnectionOptions(); - options.set(ADDRESS, "172.18.61.12"); - options.set(USERNAME, "ubuntu"); - options.set(PASSWORD, "devopsqe@123"); - options.set(OPERATING_SYSTEM, UNIX); - - ZosConnection zosConnection = new ZosConnection(options); - System.out.println(zosConnection.getOverthereForScp()); - System.out.println(zosConnection.getOverthereForSftp()); - zosConnection.checkConnection(); - zosConnection.checkConnection(SshConnectionType.SFTP); - } -} - -class ZosConnection { - private static final Logger logger = LoggerFactory.getLogger(ZosConnection.class); - private SshConnection overthereSshScp; - private SshConnection overthereSshSftp; - - public ZosConnection(ConnectionOptions targetOptions) { - try { - ConnectionOptions scpOptions = new ConnectionOptions(targetOptions); - scpOptions.set(CONNECTION_TYPE, SCP); - this.overthereSshScp = (SshConnection) Overthere.getConnection("ssh", scpOptions); - } catch (Exception e) { - this.overthereSshScp = null; - } - - try { - ConnectionOptions sftpOptions = new ConnectionOptions(targetOptions); - sftpOptions.set(CONNECTION_TYPE, SFTP); - this.overthereSshSftp = (SshConnection) Overthere.getConnection("ssh", sftpOptions); - } catch (Exception e) { - this.overthereSshSftp = null; - } - } - - public SshConnection getOverthereForScp() { - return overthereSshScp; - } - - public SshConnection getOverthereForSftp() { - return overthereSshSftp; - } - - public SshConnection getOverthere(SshConnectionType connectionType) { - if (connectionType.equals(SFTP)) { - return overthereSshSftp; - } else if (connectionType.equals(SCP)) { - return overthereSshScp; - } else { - throw new IllegalArgumentException("Unsupported connection type: " + connectionType); - } - } - - public void checkConnection(SshConnectionType connectionType){ - if (connectionType.equals(SFTP)) { - if (Objects.isNull(overthereSshSftp)) { - throw new IllegalArgumentException("No SFTP connection available"); - } - echoTmpDirContents(overthereSshSftp); - } else if (connectionType.equals(SCP)) { - if (Objects.isNull(overthereSshScp)) { - throw new IllegalArgumentException("No SCP connection available"); - } - echoTmpDirContents(overthereSshScp); - } else { - throw new IllegalArgumentException("Unsupported connection type: " + connectionType); - } - } - - public void checkConnection(){ - if (Objects.nonNull(overthereSshSftp)) echoTmpDirContents(overthereSshSftp); - if (Objects.nonNull(overthereSshScp)) echoTmpDirContents(overthereSshScp); - if (Objects.isNull(overthereSshSftp) && Objects.isNull(overthereSshScp)) { - throw new IllegalArgumentException("No connection available"); - } - } - - public void close() throws IOException { - if (Objects.nonNull(overthereSshSftp)) { - overthereSshSftp.close(); - } - if (Objects.nonNull(overthereSshScp)) { - overthereSshScp.close(); - } - } - - private void echoTmpDirContents(SshConnection connection) { - System.out.println("Listing the contents of the temporary directory using "+ connection.protocolAndConnectionType +" connection on host " + connection.host); - CmdLine cmdLine = new CmdLine(); - cmdLine.addArgument("ls"); - String tempDir = connection.getHostOperatingSystem().getDefaultTemporaryDirectoryPath(); - cmdLine.addArgument(tempDir); - int returnCode = connection.execute(cmdLine); - if (returnCode != 0) { - System.out.println("Failed to execute command for connection type "+ connection.protocolAndConnectionType +" on host" + connection.host +". Return code was ["+ returnCode +"]. Please check the logs."); - } - else { - System.out.println("Successfully executed commands on " + connection.host + "."); - } - } -} \ No newline at end of file From 3b975e4145ebbafd40976dbe3a222b36c17a4df7 Mon Sep 17 00:00:00 2001 From: Mahalakshmithirumurthy2301 Date: Tue, 10 Dec 2024 11:24:21 +0530 Subject: [PATCH 4/7] Test class for ZOS Connection --- .../overthere/ssh/ZosConnectionTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/java/com/xebialabs/overthere/ssh/ZosConnectionTest.java diff --git a/src/test/java/com/xebialabs/overthere/ssh/ZosConnectionTest.java b/src/test/java/com/xebialabs/overthere/ssh/ZosConnectionTest.java new file mode 100644 index 00000000..d5c71bdd --- /dev/null +++ b/src/test/java/com/xebialabs/overthere/ssh/ZosConnectionTest.java @@ -0,0 +1,74 @@ +package com.xebialabs.overthere.ssh; + +import com.xebialabs.overthere.ConnectionOptions; + +import org.mockito.Mockito; + +import static com.xebialabs.overthere.ConnectionOptions.*; +import static com.xebialabs.overthere.OperatingSystemFamily.UNIX; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class ZosConnectionTest { + + private SshConnection mockScpConnection; + private SshConnection mockSftpConnection; + private ZosConnection zosConnection; + + @BeforeClass + public void setUp() { + ConnectionOptions options = new ConnectionOptions(); + options.set(PORT, 22); + options.set(OPERATING_SYSTEM, UNIX); + options.set(ADDRESS, "host"); + options.set(USERNAME, "username"); + options.set(PASSWORD, "password"); + mockScpConnection = mock(SshConnection.class); + mockSftpConnection = mock(SshConnection.class); + + zosConnection = Mockito.spy(new ZosConnection(options)); + doReturn(mockScpConnection).when(zosConnection).getConnectionForScp(); + doReturn(mockSftpConnection).when(zosConnection).getConnectionForSftp(); + + doReturn(mockScpConnection).when(zosConnection).getConnection(SshConnectionType.SCP); + doReturn(mockSftpConnection).when(zosConnection).getConnection(SshConnectionType.SFTP); + } + + @Test + public void testGetConnectionForScp() { + assertThat(mockScpConnection, equalTo(zosConnection.getConnectionForScp())); + } + + @Test + public void testGetConnectionForSftp() { + assertThat(mockSftpConnection, equalTo(zosConnection.getConnectionForSftp())); + } + + @Test + public void testGetConnection_Scp() { + assertThat(zosConnection.getConnection(SshConnectionType.SCP), equalTo(mockScpConnection)); + } + + @Test + public void testGetConnection_Sftp() { + assertThat(zosConnection.getConnection(SshConnectionType.SFTP), equalTo(mockSftpConnection)); + } + + @Test(expectedExceptions = NullPointerException.class) + public void testGetConnection_NullPointer() { + zosConnection.getConnection(null); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testGetConnection_Unsupported() { + zosConnection.getConnection(SshConnectionType.SFTP_CYGWIN); + } + + +} \ No newline at end of file From 3dd373bd2c35919b25e2e54554498cf7fa8b105b Mon Sep 17 00:00:00 2001 From: Arul Prasad Date: Thu, 12 Dec 2024 19:44:33 +0530 Subject: [PATCH 5/7] code refactor with variable names and return type made as interface --- .../overthere/ssh/ZosConnection.java | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/xebialabs/overthere/ssh/ZosConnection.java b/src/main/java/com/xebialabs/overthere/ssh/ZosConnection.java index 11110292..f9394782 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/ZosConnection.java +++ b/src/main/java/com/xebialabs/overthere/ssh/ZosConnection.java @@ -6,10 +6,17 @@ import com.xebialabs.overthere.ConnectionOptions; import com.xebialabs.overthere.Overthere; +import com.xebialabs.overthere.OverthereConnection; +import com.xebialabs.overthere.OverthereConnector; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ZosConnection { - private SshConnection overthereSshScp; - private SshConnection overthereSshSftp; + private static final Logger logger = LoggerFactory.getLogger(OverthereConnector.class); + + private SshConnection sshScpZosConnection; + private SshConnection sshSftpZosConnection; + public ZosConnection(ConnectionOptions options) { initializeConnections(options); @@ -17,33 +24,33 @@ public ZosConnection(ConnectionOptions options) { private void initializeConnections(ConnectionOptions targetOptions) { try { - ConnectionOptions scpOptions = new ConnectionOptions(targetOptions); - scpOptions.set(CONNECTION_TYPE, SCP); - this.overthereSshScp = (SshConnection) Overthere.getConnection("ssh", scpOptions); + targetOptions.set(CONNECTION_TYPE, SCP); + this.sshScpZosConnection = (SshConnection) Overthere.getConnection("ssh", targetOptions); } catch (Exception e) { - this.overthereSshScp = null; + logger.warn("OverThere:zosConnection:SCP Failed", e); + this.sshScpZosConnection = null; } try { - ConnectionOptions sftpOptions = new ConnectionOptions(targetOptions); - sftpOptions.set(CONNECTION_TYPE, SFTP); - this.overthereSshSftp = (SshConnection) Overthere.getConnection("ssh", sftpOptions); + targetOptions.set(CONNECTION_TYPE, SFTP); + this.sshSftpZosConnection = (SshConnection) Overthere.getConnection("ssh", targetOptions); } catch (Exception e) { - this.overthereSshSftp = null; + logger.warn("OverThere:zosConnection:SFTP Failed", e); + this.sshSftpZosConnection = null; } } - public SshConnection getConnectionForScp() { return overthereSshScp; } + public OverthereConnection getConnectionForScp() { return sshScpZosConnection; } - public SshConnection getConnectionForSftp() { - return overthereSshSftp; + public OverthereConnection getConnectionForSftp() { + return sshSftpZosConnection; } - public SshConnection getConnection(SshConnectionType connectionType) { + public OverthereConnection getConnection(SshConnectionType connectionType) { if (connectionType.equals(SFTP)) { - return overthereSshSftp; + return sshSftpZosConnection; } else if (connectionType.equals(SCP)) { - return overthereSshScp; + return sshScpZosConnection; } else { throw new IllegalArgumentException("Unsupported connection type: " + connectionType); } From b1c6a4b4f920e54d85c60231260b06a2268b0132 Mon Sep 17 00:00:00 2001 From: Mahalakshmithirumurthy2301 Date: Tue, 17 Dec 2024 10:32:32 +0530 Subject: [PATCH 6/7] Created a new SshZosConnectionType ZOS_UNIX_SSH and mapped it in SshConnectionBuilder --- .../overthere/ssh/SshConnectionBuilder.java | 3 ++ .../overthere/ssh/SshConnectionType.java | 4 ++ .../ssh/SshZOSConnectionBuilder.java | 50 +++++++++++++++++++ .../overthere/ssh/SshZosConnectionType.java | 13 +++++ 4 files changed, 70 insertions(+) create mode 100644 src/main/java/com/xebialabs/overthere/ssh/SshZOSConnectionBuilder.java create mode 100644 src/main/java/com/xebialabs/overthere/ssh/SshZosConnectionType.java diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshConnectionBuilder.java b/src/main/java/com/xebialabs/overthere/ssh/SshConnectionBuilder.java index 871d9955..64b66d57 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshConnectionBuilder.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshConnectionBuilder.java @@ -531,6 +531,9 @@ public SshConnectionBuilder(String type, ConnectionOptions options, AddressPortM case INTERACTIVE_SUDO: connection = new SshInteractiveSudoConnection(type, options, mapper); break; + case ZOS_UNIX_SSH: + connection = new SshZOSConnectionBuilder(type, options, mapper).getSshConnection(); + break; default: throw new IllegalArgumentException("Unknown SSH connection type " + sshConnectionType); } diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshConnectionType.java b/src/main/java/com/xebialabs/overthere/ssh/SshConnectionType.java index 97c570ca..c965ad4b 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshConnectionType.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshConnectionType.java @@ -69,4 +69,8 @@ public enum SshConnectionType { */ INTERACTIVE_SUDO, + /** + * ZOS SSH connection which is a composite class that has both SCP and SFTP connection to transfer files, to a ZOS host + */ + ZOS_UNIX_SSH, } diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshZOSConnectionBuilder.java b/src/main/java/com/xebialabs/overthere/ssh/SshZOSConnectionBuilder.java new file mode 100644 index 00000000..bdd0c241 --- /dev/null +++ b/src/main/java/com/xebialabs/overthere/ssh/SshZOSConnectionBuilder.java @@ -0,0 +1,50 @@ +package com.xebialabs.overthere.ssh; + + +import com.xebialabs.overthere.ConnectionOptions; +import com.xebialabs.overthere.OverthereConnection; +import com.xebialabs.overthere.spi.AddressPortMapper; +import com.xebialabs.overthere.spi.OverthereConnectionBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static com.xebialabs.overthere.ssh.SshConnectionBuilder.CONNECTION_TYPE; + +public class SshZOSConnectionBuilder implements OverthereConnectionBuilder { + + public static final String ZOS_CONNECTION_TYPE = "zosConnectionType"; + private static final Logger log = LoggerFactory.getLogger(SshZOSConnectionBuilder.class); + + private SshConnection connection; + + public SshZOSConnectionBuilder(String type, ConnectionOptions options, AddressPortMapper mapper) { + SshZosConnectionType zosSshConnectionType = options.getEnum(ZOS_CONNECTION_TYPE, SshZosConnectionType.class); + if (zosSshConnectionType == SshZosConnectionType.SFTP) { + log.debug("Creating SFTP connection inside ZOSConnectionBuilder"); + options.set(CONNECTION_TYPE, SshConnectionType.SFTP); + connection = new SshSftpUnixConnection(type, options, mapper); + } else if (zosSshConnectionType == SshZosConnectionType.SCP) { + log.debug("Creating SCP connection inside ZOSConnectionBuilder"); + options.set(CONNECTION_TYPE, SshConnectionType.SCP); + connection = new SshScpConnection(type, options, mapper); + } else { + throw new IllegalArgumentException("Unknown SSH connection type " + zosSshConnectionType); + } + } + + SshConnection getSshConnection() { + return connection; + } + + @Override + public OverthereConnection connect() { + connection.connect(); + return connection; + } + + @Override + public String toString() { + return connection.toString(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshZosConnectionType.java b/src/main/java/com/xebialabs/overthere/ssh/SshZosConnectionType.java new file mode 100644 index 00000000..0fe14ac9 --- /dev/null +++ b/src/main/java/com/xebialabs/overthere/ssh/SshZosConnectionType.java @@ -0,0 +1,13 @@ +package com.xebialabs.overthere.ssh; + +public enum SshZosConnectionType { + /** + * An SSH connection that uses SFTP to transfer files, to a Unix host. + */ + SFTP, + + /** + * An SSH connection that uses SCP to transfer files, to a Unix host. + */ + SCP, +} From bd8e10842d0c090eff08cae11a734b510bd0a392 Mon Sep 17 00:00:00 2001 From: Mahalakshmithirumurthy2301 Date: Fri, 3 Jan 2025 10:53:20 +0530 Subject: [PATCH 7/7] S-116516 - Add source and target machines encoding while ZOSSSHUNIX check connection --- .../overthere/local/LocalConnection.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/xebialabs/overthere/local/LocalConnection.java b/src/main/java/com/xebialabs/overthere/local/LocalConnection.java index 23770b5b..b324d347 100644 --- a/src/main/java/com/xebialabs/overthere/local/LocalConnection.java +++ b/src/main/java/com/xebialabs/overthere/local/LocalConnection.java @@ -174,10 +174,21 @@ private boolean isWindows(final OperatingSystemFamily os) { private CmdLine getCmdForWindows(final CmdLine cmd) { String command = cmd.toCommandLine(WINDOWS, false); CmdLine c = CmdLine.build("cmd", "/s", "/c"); - if (containsAnySpecialChars(command)) { - return c.addArgument("\"" + command + "\""); + if (command.startsWith("powershell")) { + for(CmdLineArgument arg : cmd.getArguments()) { + if(containsAnySpecialChars(arg.toString(WINDOWS, false))) + c.addArgument("\"" + arg.toString(WINDOWS, false) + "\""); + else + c.add(arg); + } + return c; + } + else { + if (containsAnySpecialChars(command)) { + return c.addArgument("\"" + command + "\""); + } + return c.add(cmd.getArguments()); } - return c.add(cmd.getArguments()); } private static final Logger logger = LoggerFactory.getLogger(LocalConnection.class);