Fix ssh adapter compilation issues. 21/56521/3
authorDavid Crosson <david.crosson@orange.com>
Mon, 16 Jul 2018 15:52:06 +0000 (17:52 +0200)
committerTakamune Cho <tc012c@att.com>
Mon, 23 Jul 2018 21:04:52 +0000 (21:04 +0000)
Apache sshd library updated to 2.0.0

Issue-ID: APPC-835
Change-Id: Idfbc396291c3d05a47e4dceb45d06bc8084c054d
Signed-off-by: David Crosson <david.crosson@orange.com>
appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/pom.xml
appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshConnectionSshd.java
appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshAdapterTest.java
pom.xml

index 2c3f10e..854e2d4 100644 (file)
             <artifactId>sshd-core</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sshd</groupId>
+            <artifactId>sshd-sftp</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sshd</groupId>
+            <artifactId>sshd-scp</artifactId>
+            <scope>provided</scope>
+        </dependency>
         <dependency>
             <groupId>com.att.eelf</groupId>
             <artifactId>eelf-core</artifactId>
index 10d3019..d54fe43 100644 (file)
 
 package org.onap.appc.adapter.ssh.sshd;
 
+import org.apache.sshd.client.channel.ClientChannelEvent;
 import org.onap.appc.adapter.ssh.Constants;
 import org.onap.appc.adapter.ssh.SshConnection;
 import org.onap.appc.adapter.ssh.SshException;
 import org.onap.appc.encryption.EncryptionTool;
 import org.onap.appc.configuration.Configuration;
 import org.onap.appc.configuration.ConfigurationFactory;
-import org.apache.sshd.ClientChannel;
-import org.apache.sshd.ClientSession;
-import org.apache.sshd.SshClient;
+import org.apache.sshd.client.session.ClientSession;
+import org.apache.sshd.client.SshClient;
 import org.apache.sshd.client.channel.ChannelExec;
 import org.apache.sshd.client.future.AuthFuture;
 import org.apache.sshd.client.future.OpenFuture;
-import org.apache.sshd.common.KeyPairProvider;
+import org.apache.sshd.common.keyprovider.KeyPairProvider;
 import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
 
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 
+import java.io.File;
 import java.io.OutputStream;
 import java.security.KeyPair;
+import java.util.Arrays;
 
 /**
  * Implementation of SshConnection interface based on Apache MINA SSHD library.
@@ -86,14 +88,14 @@ class SshConnectionSshd implements SshConnection {
         sshClient.start();
         try {
             clientSession =
-                sshClient.connect(EncryptionTool.getInstance().decrypt(username), host, port).await().getSession();
+                sshClient.connect(EncryptionTool.getInstance().decrypt(username), host, port).verify().getSession();
             if (password != null) {
                 clientSession.addPasswordIdentity(EncryptionTool.getInstance().decrypt(password));
             }
             if (keyFile != null) {
-                KeyPairProvider keyPairProvider = new FileKeyPairProvider(new String[] {
-                    keyFile
-                });
+                KeyPairProvider keyPairProvider = new FileKeyPairProvider(
+                        new File(keyFile).toPath()
+                );
                 KeyPair keyPair = keyPairProvider.loadKeys().iterator().next();
                 clientSession.addPublicKeyIdentity(keyPair);
             }
@@ -183,7 +185,7 @@ class SshConnectionSshd implements SshConnection {
             OpenFuture openFuture = client.open();
             int exitStatus;
             try {
-                client.waitFor(ClientChannel.CLOSED, timeout);
+                client.waitFor(Arrays.asList(ClientChannelEvent.CLOSED), timeout);
                 openFuture.verify();
                 Integer exitStatusI = client.getExitStatus();
                 if (exitStatusI == null) {
index 5fdbf54..accc6c1 100644 (file)
 
 package org.onap.appc.adapter.ssh.sshd;
 
-import org.apache.sshd.SshServer;
+import org.apache.sshd.server.SshServer;
+import org.apache.sshd.common.keyprovider.KeyPairProvider;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.util.OsUtils;
-import org.apache.sshd.server.Command;
-import org.apache.sshd.server.CommandFactory;
-import org.apache.sshd.server.PasswordAuthenticator;
-import org.apache.sshd.server.PublickeyAuthenticator;
-import org.apache.sshd.server.command.ScpCommandFactory;
+import org.apache.sshd.server.command.Command;
+import org.apache.sshd.server.auth.password.PasswordAuthenticator;
+import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;
 import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
+import org.apache.sshd.server.scp.ScpCommandFactory;
 import org.apache.sshd.server.session.ServerSession;
-import org.apache.sshd.server.sftp.SftpSubsystem;
 import org.apache.sshd.server.shell.ProcessShellFactory;
+import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
 import org.hamcrest.CoreMatchers;
 import org.junit.*;
 import org.junit.rules.ExpectedException;
 import org.onap.appc.adapter.ssh.SshAdapter;
 import org.onap.appc.adapter.ssh.SshConnection;
 import org.onap.appc.adapter.ssh.SshException;
-import org.onap.appc.adapter.ssh.sshd.SshAdapterSshd;
 
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.net.BindException;
@@ -51,6 +51,7 @@ import java.security.PublicKey;
 import java.util.Collections;
 import java.util.EnumSet;
 
+//@Ignore
 public class SshAdapterTest {
 
     private static final boolean START_SERVER = true;
@@ -162,42 +163,43 @@ public class SshAdapterTest {
 
     private void startServer() throws IOException {
         sshd = SshServer.setUpDefaultServer();
-        sshd.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystem.Factory()));
-        sshd.setCommandFactory(new ScpCommandFactory(new CommandFactory() {
+        sshd.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
+        sshd.setCommandFactory(new ScpCommandFactory() {
 
             public Command createCommand(String command) {
-                EnumSet<ProcessShellFactory.TtyOptions> ttyOptions;
-                if (OsUtils.isUNIX()) {
-                    ttyOptions = EnumSet.of(ProcessShellFactory.TtyOptions.ONlCr);
-                } else {
-                    ttyOptions = EnumSet.of(ProcessShellFactory.TtyOptions.Echo, ProcessShellFactory.TtyOptions.ICrNl, ProcessShellFactory.TtyOptions.ONlCr);
-                }
-                return new ProcessShellFactory(command.split(" "), ttyOptions).create();
+                //EnumSet<ProcessShellFactory.TtyOptions> ttyOptions;
+                //if (OsUtils.isUNIX()) {
+                //    ttyOptions = EnumSet.of(ProcessShellFactory.TtyOptions.ONlCr);
+                //} else {
+                //    ttyOptions = EnumSet.of(ProcessShellFactory.TtyOptions.Echo, ProcessShellFactory.TtyOptions.ICrNl, ProcessShellFactory.TtyOptions.ONlCr);
+                //}
+                //return new ProcessShellFactory(command.split(" "), ttyOptions).create();
+                
+                return new ProcessShellFactory(command.split(" ")).create();
             }
-        }));
+        });
         if (OsUtils.isUNIX()) {
-            sshd.setShellFactory(new ProcessShellFactory(new String[]{"/bin/sh", "-i", "-l"},
-                    EnumSet.of(ProcessShellFactory.TtyOptions.ONlCr)));
+            sshd.setShellFactory(new ProcessShellFactory(new String[]{"/bin/sh", "-i", "-l"}/*,
+                    EnumSet.of(ProcessShellFactory.TtyOptions.ONlCr)*/));
         } else {
-            sshd.setShellFactory(new ProcessShellFactory(new String[]{"cmd.exe "},
-                    EnumSet.of(ProcessShellFactory.TtyOptions.Echo, ProcessShellFactory.TtyOptions.ICrNl, ProcessShellFactory.TtyOptions.ONlCr)));
+            sshd.setShellFactory(new ProcessShellFactory(new String[]{"cmd.exe "}/*,
+                    EnumSet.of(ProcessShellFactory.TtyOptions.Echo, ProcessShellFactory.TtyOptions.ICrNl, ProcessShellFactory.TtyOptions.ONlCr)*/));
         }
 //             if(SecurityUtils.isBouncyCastleRegistered()) {
 //                     sshd.setKeyPairProvider(new PEMGeneratorHostKeyProvider(System.getProperty("java.io.tmpdir") + "/key.pem"));
 //             } else {
-        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(System.getProperty("java.io.tmpdir") + "/key.ser"));
+        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File(System.getProperty("java.io.tmpdir") + "/key.ser")));
 //             }
         sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
-
             @Override
             public boolean authenticate(String username, String password, ServerSession session) {
                 return (SSH_USERNAME.equals(username) && SSH_PASSWORD.equals(password));
             }
         });
         sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
-
+            // We're testing access using passwords, so do not authorize authentitication using public keys
             public boolean authenticate(String username, PublicKey key, ServerSession session) {
-                return true;
+                return false;
             }
         });
         sshd.getProperties().put(SshServer.WELCOME_BANNER, "Welcome to SSHD\n");
@@ -236,11 +238,11 @@ public class SshAdapterTest {
     private void stopServer() {
         try {
             if (sshd != null) {
-                sshd.stop(true);
+                sshd.stop();
                 System.out.println("SSH server stopped on port [" + sshPort + "]. [" + getClass().getName() + "#" + System.identityHashCode(this) + "]");
             }
-        } catch (InterruptedException e) {
-            System.err.println("=> Error stopping SSH server.");
+        } catch (IOException e) {
+            System.err.println("=> IO Error stopping SSH server.");
             e.printStackTrace();
         } finally {
             sshd = null;
diff --git a/pom.xml b/pom.xml
index 6235571..340a430 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -404,7 +404,17 @@ limitations under the License.
             <dependency>
                 <groupId>org.apache.sshd</groupId>
                 <artifactId>sshd-core</artifactId>
-                <version>0.12.0</version>
+                <version>2.0.0</version>
+            </dependency>
+            <dependency>
+                <groupId>org.apache.sshd</groupId>
+                <artifactId>sshd-sftp</artifactId>
+                <version>2.0.0</version>
+            </dependency>
+            <dependency>
+                <groupId>org.apache.sshd</groupId>
+                <artifactId>sshd-scp</artifactId>
+                <version>2.0.0</version>
             </dependency>
             <dependency>
                 <groupId>org.mockito</groupId>