diff options
author | Ganesh Chandrasekaran <ganesh.c@samsung.com> | 2018-07-20 16:32:46 +0900 |
---|---|---|
committer | Patrick Brady <pb071s@att.com> | 2018-07-26 21:47:39 +0000 |
commit | 14175c4605745183a09dcd0fdc72c0d4e8a05b20 (patch) | |
tree | 0fa5e27c0207cc3da30f4acf3af9bc1020bff4aa /appc-adapters/appc-ssh-adapter | |
parent | fdf7e648663e5770a431e9ba8088be6c3bbf1f5a (diff) |
API to support getConnection with SSH KeyFile
Issue-ID: APPC-1097
Change-Id: Ic9baa3e43be55bc20aa974898c164cc8a590a78a
Signed-off-by: Ganesh Chandrasekaran <ganesh.c@samsung.com>
Diffstat (limited to 'appc-adapters/appc-ssh-adapter')
7 files changed, 101 insertions, 72 deletions
diff --git a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-api/src/main/java/org/onap/appc/adapter/ssh/SshAdapter.java b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-api/src/main/java/org/onap/appc/adapter/ssh/SshAdapter.java index bf3f7f64e..91dc0ad49 100644 --- a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-api/src/main/java/org/onap/appc/adapter/ssh/SshAdapter.java +++ b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-api/src/main/java/org/onap/appc/adapter/ssh/SshAdapter.java @@ -28,14 +28,24 @@ package org.onap.appc.adapter.ssh; */ public interface SshAdapter { - /** - * Creates instance of SshConnection. - * - * @param host remote host to open SSH connection to - * @param port remote SSH port - * @param username SSH connection user name - * @param password SSH connection password - * @return instance of SshConnection - */ - SshConnection getConnection(String host, int port, String username, String password); + /** + * Creates instance of SshConnection. + * + * @param host remote host to open SSH connection to + * @param port remote SSH port + * @param username SSH connection user name + * @param password SSH connection password + * @return instance of SshConnection + */ + SshConnection getConnection(String host, int port, String username, String password); + + /** + * Creates instance of SshConnection. + * + * @param host remote host to open SSH connection to + * @param port remote SSH port + * @param keyFile SSH connection key file location + * @return instance of SshConnection + */ + SshConnection getConnection(String host, int port, String keyFile); } diff --git a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-api/src/main/java/org/onap/appc/adapter/ssh/SshConnection.java b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-api/src/main/java/org/onap/appc/adapter/ssh/SshConnection.java index cd80a2e35..db67f00cd 100644 --- a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-api/src/main/java/org/onap/appc/adapter/ssh/SshConnection.java +++ b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-api/src/main/java/org/onap/appc/adapter/ssh/SshConnection.java @@ -30,45 +30,45 @@ import java.io.OutputStream; */ public interface SshConnection { - /** - * Connect to SSH server. - */ - void connect(); + /** + * Connect to SSH server. + */ + void connect(); - /** - * Connect to SSH Server using a retry mechanism - */ - void connectWithRetry(); + /** + * Connect to SSH Server using a retry mechanism + */ + void connectWithRetry(); - /** - * Disconnect from SSH server. - */ - void disconnect(); + /** + * Disconnect from SSH server. + */ + void disconnect(); - /** - * Exec remote command over SSH. Return command execution status. - * Command output is written to out or err stream. - * - * @param cmd command to execute - * @param out content of sysout will go to this stream - * @param err content of syserr will go to this stream - * @return command execution status - */ - int execCommand(String cmd, OutputStream out, OutputStream err); + /** + * Exec remote command over SSH. Return command execution status. + * Command output is written to out or err stream. + * + * @param cmd command to execute + * @param out content of sysout will go to this stream + * @param err content of syserr will go to this stream + * @return command execution status + */ + int execCommand(String cmd, OutputStream out, OutputStream err); - /** - * Exec remote command over SSH with pseudo-tty. Return command execution status. - * Command output is written to out stream only as pseudo-tty writes to one stream only. - * - * @param cmd command to execute - * @param out content of sysout will go to this stream - * @return command execution status - */ - int execCommandWithPty(String cmd, OutputStream out); + /** + * Exec remote command over SSH with pseudo-tty. Return command execution status. + * Command output is written to out stream only as pseudo-tty writes to one stream only. + * + * @param cmd command to execute + * @param out content of sysout will go to this stream + * @return command execution status + */ + int execCommandWithPty(String cmd, OutputStream out); - /** - * Set the command execution timeout - * @param timeout time in milliseconds + /** + * Set the command execution timeout + * @param timeout time in milliseconds */ - void setExecTimeout(long timeout); + void setExecTimeout(long timeout); } diff --git a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshAdapterSshd.java b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshAdapterSshd.java index 209aa1f9a..2b5b6806a 100644 --- a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshAdapterSshd.java +++ b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshAdapterSshd.java @@ -28,8 +28,13 @@ import org.onap.appc.adapter.ssh.SshConnection; public class SshAdapterSshd implements SshAdapter { - @Override - public SshConnection getConnection(String host, int port, String username, String password) { - return new SshConnectionSshd(host, port, username, password); - } + @Override + public SshConnection getConnection(String host, int port, String username, String password) { + return new SshConnectionSshd(host, port, username, password); + } + + @Override + public SshConnection getConnection(String host, int port, String keyFile) { + return new SshConnectionSshd(host, port, keyFile); + } } diff --git a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshConnectionSshd.java b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshConnectionSshd.java index d54fe4355..299ad7755 100644 --- a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshConnectionSshd.java +++ b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshConnectionSshd.java @@ -91,8 +91,7 @@ class SshConnectionSshd implements SshConnection { sshClient.connect(EncryptionTool.getInstance().decrypt(username), host, port).verify().getSession(); if (password != null) { clientSession.addPasswordIdentity(EncryptionTool.getInstance().decrypt(password)); - } - if (keyFile != null) { + } else if (keyFile != null) { KeyPairProvider keyPairProvider = new FileKeyPairProvider( new File(keyFile).toPath() ); diff --git a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-tests/src/main/java/org/onap/appc/adapter/ssh/SshAdapterMock.java b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-tests/src/main/java/org/onap/appc/adapter/ssh/SshAdapterMock.java index 2ef49b832..55d77821a 100644 --- a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-tests/src/main/java/org/onap/appc/adapter/ssh/SshAdapterMock.java +++ b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-tests/src/main/java/org/onap/appc/adapter/ssh/SshAdapterMock.java @@ -36,7 +36,17 @@ public class SshAdapterMock implements SshAdapter { @Override public SshConnection getConnection(String host, int port, String username, String password) { - SshConnectionMock sshConnectionMock = new SshConnectionMock(host, port, username, password); + SshConnectionMock sshConnectionMock = new SshConnectionMock(host, port, username, password, null); + sshConnectionMock.setReturnStatus(returnStatus); + sshConnectionMock.setReturnStdout(returnStdout); + sshConnectionMock.setReturnStderr(returnStderr); + connectionMocks.add(sshConnectionMock); + return sshConnectionMock; + } + + @Override + public SshConnection getConnection(String host, int port, String keyFile) { + SshConnectionMock sshConnectionMock = new SshConnectionMock(host, port, null, null, keyFile); sshConnectionMock.setReturnStatus(returnStatus); sshConnectionMock.setReturnStdout(returnStdout); sshConnectionMock.setReturnStderr(returnStderr); diff --git a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-tests/src/main/java/org/onap/appc/adapter/ssh/SshConnectionMock.java b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-tests/src/main/java/org/onap/appc/adapter/ssh/SshConnectionMock.java index e62274b60..b61fc136f 100644 --- a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-tests/src/main/java/org/onap/appc/adapter/ssh/SshConnectionMock.java +++ b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-tests/src/main/java/org/onap/appc/adapter/ssh/SshConnectionMock.java @@ -34,10 +34,11 @@ public class SshConnectionMock implements SshConnection { private static final int DEF_SUCCESS_STATUS = 0; - private String host; - private int port; - private String username; - private String password; + private final String host; + private final int port; + private final String username; + private final String password; + private final String keyFile; private long timeout; private int returnStatus = DEF_SUCCESS_STATUS; @@ -48,11 +49,12 @@ public class SshConnectionMock implements SshConnection { private int disconnectCallCount = 0; private List<String> executedCommands = new ArrayList<>(); - public SshConnectionMock(String host, int port, String username, String password) { + public SshConnectionMock(String host, int port, String username, String password, String keyFile) { this.host = host; this.port = port; this.username = username; this.password = password; + this.keyFile = keyFile; } @Override @@ -124,6 +126,10 @@ public class SshConnectionMock implements SshConnection { return password; } + public String getKeyFile() { + return keyFile; + } + public int getConnectCallCount() { return connectCallCount; } diff --git a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-tests/src/test/java/org/onap/appc/adapter/ssh/TestSshConnectionMock.java b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-tests/src/test/java/org/onap/appc/adapter/ssh/TestSshConnectionMock.java index 182e0d7c7..750a3bafa 100644 --- a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-tests/src/test/java/org/onap/appc/adapter/ssh/TestSshConnectionMock.java +++ b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-tests/src/test/java/org/onap/appc/adapter/ssh/TestSshConnectionMock.java @@ -29,49 +29,48 @@ public class TestSshConnectionMock { private SshConnectionMock sshConnectionMock; @Before public void setUp() { - sshConnectionMock=new SshConnectionMock("localhost", 8080, "myUser", "myPassword"); + sshConnectionMock=new SshConnectionMock("localhost", 8080, "myUser", "myPassword", "sampleKeyFile"); } @Test public void testGetHost() { - assertNotNull(sshConnectionMock.getHost()); - assertEquals(sshConnectionMock.getHost(), "localhost"); + assertEquals("localhost", sshConnectionMock.getHost()); } @Test public void testGetPort() { - assertNotNull(sshConnectionMock.getPort()); - assertEquals(sshConnectionMock.getPort(), 8080); + assertEquals(8080, sshConnectionMock.getPort()); } @Test public void testGetUsername() { - assertNotNull(sshConnectionMock.getUsername()); - assertEquals(sshConnectionMock.getUsername(), "myUser"); + assertEquals("myUser", sshConnectionMock.getUsername()); } @Test public void testGetPassword() { - assertNotNull(sshConnectionMock.getPassword()); - assertEquals(sshConnectionMock.getPassword(), "myPassword"); + assertEquals("myPassword", sshConnectionMock.getPassword()); } + + @Test + public void testKeyFile() { + assertEquals("sampleKeyFile", sshConnectionMock.getKeyFile()); + } + @Test public void testGetReturnStderr() { sshConnectionMock.setReturnStderr("returnStderr"); - assertNotNull(sshConnectionMock.getReturnStderr()); - assertEquals(sshConnectionMock.getReturnStderr(), "returnStderr"); + assertEquals("returnStderr", sshConnectionMock.getReturnStderr()); } @Test public void testGetReturnStdout() { sshConnectionMock.setReturnStdout("returnStdout"); - assertNotNull(sshConnectionMock.getReturnStdout()); - assertEquals(sshConnectionMock.getReturnStdout(), "returnStdout"); + assertEquals("returnStdout", sshConnectionMock.getReturnStdout()); } @Test public void testGetReturnStatus() { sshConnectionMock.setReturnStatus(200); - assertNotNull(sshConnectionMock.getReturnStatus()); - assertEquals(sshConnectionMock.getReturnStatus(), 200); + assertEquals(200, sshConnectionMock.getReturnStatus()); } @Test public void testGetExecutedCommands() { |