BUG-1568 Ssh Handler for netconf client Mina implementation 42/10142/9
authorMaros Marsalek <mmarsale@cisco.com>
Mon, 25 Aug 2014 08:01:47 +0000 (10:01 +0200)
committerMaros Marsalek <mmarsale@cisco.com>
Mon, 25 Aug 2014 08:01:47 +0000 (10:01 +0200)
This implementation of Ssh Handler is asynchronous and based on Apache Mina SSHD.

+ Add Mina SSHD to distribution.

Change-Id: I51f51e2eb56b5f97a87c3d068ea90f8a44adca09
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
Signed-off-by: Robert Varga <rovarga@cisco.com>
12 files changed:
features/netconf/pom.xml
features/netconf/src/main/resources/features.xml
opendaylight/commons/opendaylight/pom.xml
opendaylight/distribution/opendaylight/pom.xml
opendaylight/md-sal/sal-binding-it/src/main/java/org/opendaylight/controller/test/sal/binding/it/TestHelper.java
opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/SshClientChannelInitializer.java
opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java
opendaylight/netconf/netconf-netty-util/pom.xml
opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/AuthenticationHandler.java
opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/LoginPassword.java
opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java [new file with mode: 0644]
opendaylight/netconf/netconf-ssh/pom.xml

index 106e54a338203453fca414f06b89bce81232a3bc..51d6f6291b08adb86157c75523bc93067401b906 100644 (file)
       <groupId>org.opendaylight.controller.thirdparty</groupId>
       <artifactId>ganymed</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.sshd</groupId>
+      <artifactId>sshd-core</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.openexi</groupId>
       <artifactId>nagasena</artifactId>
index f1631a53400c2d60a1e0c922cccad5b9d3f05051..1453a5fb969a9d1e3f6b9f6bec88cebb59182d3f 100644 (file)
@@ -52,6 +52,7 @@
     <feature version='${project.version}'>odl-netconf-util</feature>
     <bundle>mvn:org.opendaylight.controller/netconf-netty-util/${project.version}</bundle>
     <bundle>mvn:org.opendaylight.controller.thirdparty/ganymed/${ganymed.version}</bundle>
+    <bundle>mvn:org.apache.sshd/sshd-core/${sshd-core.version}</bundle>
     <bundle>mvn:org.openexi/nagasena/${exi.nagasena.version}</bundle>
     <bundle>mvn:io.netty/netty-codec/${netty.version}</bundle>
     <bundle>mvn:io.netty/netty-handler/${netty.version}</bundle>
@@ -69,4 +70,4 @@
     <bundle>mvn:org.opendaylight.controller/netconf-monitoring/${project.version}</bundle>
   </feature>
 
-</features>
\ No newline at end of file
+</features>
index 2f1cd8b925ce38719702cc707906cb05df042fd4..873ad7a9b7e3f4572ca3b7809a901ac716cc391f 100644 (file)
     <yang-ext.version>2013.09.07.4-SNAPSHOT</yang-ext.version>
     <yang-jmx-generator.version>1.0.0-SNAPSHOT</yang-jmx-generator.version>
     <yangtools.version>0.6.2-SNAPSHOT</yangtools.version>
+      <sshd-core.version>0.12.0</sshd-core.version>
   </properties>
 
   <dependencyManagement>
         <artifactId>netconf-netty-util</artifactId>
         <version>${netconf.version}</version>
       </dependency>
+      <dependency>
+        <groupId>org.apache.sshd</groupId>
+        <artifactId>sshd-core</artifactId>
+        <version>${sshd-core.version}</version>
+      </dependency>
       <dependency>
         <groupId>ch.qos.logback</groupId>
         <artifactId>logback-classic</artifactId>
index d70bf4641192c488d043e2ba7ff4bc41279b1375..9a7677099dd8be04b4357b707c135a8279153a64 100644 (file)
           <groupId>org.opendaylight.controller.thirdparty</groupId>
           <artifactId>ganymed</artifactId>
         </dependency>
+        <dependency>
+          <groupId>org.apache.sshd</groupId>
+          <artifactId>sshd-core</artifactId>
+        </dependency>
         <dependency>
           <groupId>org.opendaylight.yangtools</groupId>
           <artifactId>binding-generator-api</artifactId>
index 344694381a222850fb23ccac0cc8415b3296f472..dba2908e9c4a9309bf3a1acba4e4fcc5974f7a03 100644 (file)
@@ -73,6 +73,7 @@ public class TestHelper {
                 mavenBundle(CONTROLLER, "netconf-client").versionAsInProject(), //
                 mavenBundle(CONTROLLER, "netconf-util").versionAsInProject(), //
                 mavenBundle(CONTROLLER, "netconf-netty-util").versionAsInProject(), //
+                mavenBundle("org.apache.sshd", "sshd-core").versionAsInProject(), //
                 mavenBundle("org.openexi", "nagasena").versionAsInProject(), //
                 mavenBundle("org.openexi", "nagasena-rta").versionAsInProject(), //
                 mavenBundle(CONTROLLER + ".thirdparty", "ganymed").versionAsInProject(), //
index 87b3f837e8c43a25f74f78cfbdd0b425bd8d94e9..2aa5d15224be5efa4c4f6f56a2498eab9df5b4f7 100644 (file)
@@ -12,7 +12,7 @@ import io.netty.util.concurrent.Promise;
 import java.io.IOException;
 import org.opendaylight.controller.netconf.nettyutil.AbstractChannelInitializer;
 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
-import org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.SshHandler;
+import org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.AsyncSshHandler;
 import org.opendaylight.protocol.framework.SessionListenerFactory;
 
 final class SshClientChannelInitializer extends AbstractChannelInitializer<NetconfClientSession> {
@@ -32,7 +32,7 @@ final class SshClientChannelInitializer extends AbstractChannelInitializer<Netco
     @Override
     public void initialize(final Channel ch, final Promise<NetconfClientSession> promise) {
         try {
-            ch.pipeline().addFirst(SshHandler.createForNetconfSubsystem(authenticationHandler));
+            ch.pipeline().addFirst(AsyncSshHandler.createForNetconfSubsystem(authenticationHandler));
             super.initialize(ch,promise);
         } catch (final IOException e) {
             throw new RuntimeException(e);
index 6826b4a09ca6013f57c192ef07fcef3bdd7e7387..56f674bc34f287d74224ad269a1274c657109e68 100644 (file)
@@ -9,13 +9,10 @@
 package org.opendaylight.controller.netconf.it;
 
 import static java.util.Arrays.asList;
-import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
-import ch.ethz.ssh2.Connection;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.EventLoopGroup;
 import io.netty.channel.nio.NioEventLoopGroup;
@@ -30,8 +27,6 @@ import junit.framework.Assert;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
 import org.opendaylight.controller.config.spi.ModuleFactory;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
@@ -47,6 +42,7 @@ import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProduce
 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
+import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
 import org.opendaylight.controller.netconf.ssh.authentication.AuthProviderImpl;
@@ -144,16 +140,6 @@ public class NetconfITSecureTest extends AbstractNetconfConfigTest {
     }
 
     public AuthenticationHandler getAuthHandler() throws IOException {
-        final AuthenticationHandler authHandler = mock(AuthenticationHandler.class);
-        doAnswer(new Answer() {
-            @Override
-            public Object answer(final InvocationOnMock invocation) throws Throwable {
-                Connection conn = (Connection) invocation.getArguments()[0];
-                conn.authenticateWithPassword("user", "pwd");
-                return null;
-            }
-        }).when(authHandler).authenticate(any(Connection.class));
-        doReturn("auth handler").when(authHandler).toString();
-        return authHandler;
+        return new LoginPassword("user", "pwd");
     }
 }
index 13041598d1a149932aa37e6efa8cb6e414decd87..80dc1ae0fd352fd6827701059eeb10fa5050f5f2 100644 (file)
       <groupId>org.opendaylight.controller.thirdparty</groupId>
       <artifactId>ganymed</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.sshd</groupId>
+      <artifactId>sshd-core</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.openexi</groupId>
       <artifactId>nagasena</artifactId>
@@ -73,6 +77,7 @@
       <groupId>xmlunit</groupId>
       <artifactId>xmlunit</artifactId>
     </dependency>
+
   </dependencies>
 
   <build>
@@ -82,7 +87,7 @@
         <artifactId>maven-bundle-plugin</artifactId>
         <configuration>
           <instructions>
-            <Import-Package>ch.ethz.ssh2, com.google.common.base, com.google.common.collect, io.netty.buffer,
+            <Import-Package>org.apache.sshd.*, ch.ethz.ssh2, com.google.common.base, com.google.common.collect, io.netty.buffer,
               io.netty.channel, io.netty.channel.socket, io.netty.handler.codec, io.netty.handler.ssl, io.netty.util,
               io.netty.util.concurrent, javax.xml.transform, javax.xml.transform.dom, javax.xml.transform.sax,
               javax.xml.transform.stream, org.opendaylight.controller.netconf.api,
index b22b792d52980227d2aa85da39436535a19097b2..eea2b8693a26449a5f96b8d791f4b07082273bf8 100644 (file)
@@ -9,12 +9,17 @@
 package org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication;
 
 import ch.ethz.ssh2.Connection;
-
 import java.io.IOException;
+import org.apache.sshd.ClientSession;
 
 /**
  * Class providing authentication facility to SSH handler.
  */
 public abstract class AuthenticationHandler {
     public abstract void authenticate(Connection connection) throws IOException;
+
+
+    public abstract String getUsername();
+
+    public abstract org.apache.sshd.client.future.AuthFuture authenticate(final ClientSession session) throws IOException;
 }
index b67aa0f96dcd53a084965fc3766399a26f2a5869..553e5359ffed1cd8b5728734bb5546e3abb0f72e 100644 (file)
@@ -8,10 +8,13 @@
 
 package org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication;
 
-import ch.ethz.ssh2.Connection;
-
 import java.io.IOException;
 
+import org.apache.sshd.ClientSession;
+import org.apache.sshd.client.future.AuthFuture;
+
+import ch.ethz.ssh2.Connection;
+
 /**
  * Class Providing username/password authentication option to
  * {@link org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.SshHandler}
@@ -27,10 +30,21 @@ public class LoginPassword extends AuthenticationHandler {
 
     @Override
     public void authenticate(Connection connection) throws IOException {
-        boolean isAuthenticated = connection.authenticateWithPassword(username, password);
+        final boolean isAuthenticated = connection.authenticateWithPassword(username, password);
 
         if (!isAuthenticated) {
             throw new IOException("Authentication failed.");
         }
     }
+
+    @Override
+    public String getUsername() {
+        return username;
+    }
+
+    @Override
+    public AuthFuture authenticate(final ClientSession session) throws IOException {
+        session.addPasswordIdentity(password);
+        return session.auth();
+    }
 }
diff --git a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java
new file mode 100644 (file)
index 0000000..2761a45
--- /dev/null
@@ -0,0 +1,267 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.netconf.nettyutil.handler.ssh.client;
+
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelOutboundHandlerAdapter;
+import io.netty.channel.ChannelPromise;
+import java.io.IOException;
+import java.net.SocketAddress;
+import org.apache.sshd.ClientChannel;
+import org.apache.sshd.ClientSession;
+import org.apache.sshd.SshClient;
+import org.apache.sshd.client.future.AuthFuture;
+import org.apache.sshd.client.future.ConnectFuture;
+import org.apache.sshd.client.future.OpenFuture;
+import org.apache.sshd.common.future.CloseFuture;
+import org.apache.sshd.common.future.SshFutureListener;
+import org.apache.sshd.common.io.IoInputStream;
+import org.apache.sshd.common.io.IoReadFuture;
+import org.apache.sshd.common.util.Buffer;
+import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Netty SSH handler class. Acts as interface between Netty and SSH library.
+ */
+public class AsyncSshHandler extends ChannelOutboundHandlerAdapter {
+
+    private static final Logger logger = LoggerFactory.getLogger(AsyncSshHandler.class);
+    public static final String SUBSYSTEM = "netconf";
+
+    public static final SshClient DEFAULT_CLIENT = SshClient.setUpDefaultClient();
+
+    public static final int SSH_DEFAULT_NIO_WORKERS = 8;
+
+    static {
+        // TODO make configurable, or somehow reuse netty threadpool
+        DEFAULT_CLIENT.setNioWorkers(SSH_DEFAULT_NIO_WORKERS);
+        DEFAULT_CLIENT.start();
+    }
+
+    private final AuthenticationHandler authenticationHandler;
+    private final SshClient sshClient;
+
+    private SshReadAsyncListener sshReadAsyncListener;
+    private ClientChannel channel;
+    private ClientSession session;
+    private ChannelPromise connectPromise;
+
+    public static AsyncSshHandler createForNetconfSubsystem(final AuthenticationHandler authenticationHandler) throws IOException {
+        return new AsyncSshHandler(authenticationHandler, DEFAULT_CLIENT);
+    }
+
+    /**
+     *
+     * @param authenticationHandler
+     * @param sshClient started SshClient
+     * @throws IOException
+     */
+    public AsyncSshHandler(final AuthenticationHandler authenticationHandler, final SshClient sshClient) throws IOException {
+        this.authenticationHandler = Preconditions.checkNotNull(authenticationHandler);
+        this.sshClient = Preconditions.checkNotNull(sshClient);
+        // Start just in case
+        sshClient.start();
+    }
+
+    private void startSsh(final ChannelHandlerContext ctx, final SocketAddress address) {
+        logger.debug("Starting SSH to {} on channel: {}", address, ctx.channel());
+
+        final ConnectFuture sshConnectionFuture = sshClient.connect(authenticationHandler.getUsername(), address);
+        sshConnectionFuture.addListener(new SshFutureListener<ConnectFuture>() {
+            @Override
+            public void operationComplete(final ConnectFuture future) {
+                if (future.isConnected()) {
+                    handleSshSessionCreated(future, ctx);
+                } else {
+                    handleSshSetupFailure(ctx, future.getException());
+                }
+            }
+        });
+    }
+
+    private synchronized void handleSshSessionCreated(final ConnectFuture future, final ChannelHandlerContext ctx) {
+        try {
+            logger.trace("SSH session created on channel: {}", ctx.channel());
+
+            session = future.getSession();
+            final AuthFuture authenticateFuture = authenticationHandler.authenticate(session);
+            authenticateFuture.addListener(new SshFutureListener<AuthFuture>() {
+                @Override
+                public void operationComplete(final AuthFuture future) {
+                    if (future.isSuccess()) {
+                        handleSshAuthenticated(session, ctx);
+                    } else {
+                        handleSshSetupFailure(ctx, future.getException());
+                    }
+                }
+            });
+        } catch (final IOException e) {
+            handleSshSetupFailure(ctx, e);
+        }
+    }
+
+    private synchronized void handleSshAuthenticated(final ClientSession session, final ChannelHandlerContext ctx) {
+        try {
+            logger.debug("SSH session authenticated on channel: {}, server version: {}", ctx.channel(), session.getServerVersion());
+
+            channel = session.createSubsystemChannel(SUBSYSTEM);
+            channel.setStreaming(ClientChannel.Streaming.Async);
+            channel.open().addListener(new SshFutureListener<OpenFuture>() {
+                @Override
+                public void operationComplete(final OpenFuture future) {
+                    if(future.isOpened()) {
+                        handleSshChanelOpened(ctx);
+                    } else {
+                        handleSshSetupFailure(ctx, future.getException());
+                    }
+                }
+            });
+
+
+        } catch (final IOException e) {
+            handleSshSetupFailure(ctx, e);
+        }
+    }
+
+    private synchronized void handleSshChanelOpened(final ChannelHandlerContext ctx) {
+        logger.trace("SSH subsystem channel opened successfully on channel: {}", ctx.channel());
+
+        connectPromise.setSuccess();
+        connectPromise = null;
+        ctx.fireChannelActive();
+
+        final IoInputStream asyncOut = channel.getAsyncOut();
+        sshReadAsyncListener = new SshReadAsyncListener(ctx, asyncOut);
+    }
+
+    private synchronized void handleSshSetupFailure(final ChannelHandlerContext ctx, final Throwable e) {
+        logger.warn("Unable to setup SSH connection on channel: {}", ctx.channel(), e);
+        connectPromise.setFailure(e);
+        connectPromise = null;
+        throw new IllegalStateException("Unable to setup SSH connection on channel: " + ctx.channel(), e);
+    }
+
+    @Override
+    public synchronized void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
+        try {
+            if(channel.getAsyncIn().isClosed() || channel.getAsyncIn().isClosing()) {
+                handleSshSessionClosed(ctx);
+            } else {
+                channel.getAsyncIn().write(toBuffer(msg));
+                ((ByteBuf) msg).release();
+            }
+        } catch (final Exception e) {
+            logger.warn("Exception while writing to SSH remote on channel {}", ctx.channel(), e);
+            throw new IllegalStateException("Exception while writing to SSH remote on channel " + ctx.channel(),e);
+        }
+    }
+
+    private static void handleSshSessionClosed(final ChannelHandlerContext ctx) {
+        logger.debug("SSH session closed on channel: {}", ctx.channel());
+        ctx.fireChannelInactive();
+    }
+
+    private Buffer toBuffer(final Object msg) {
+        // TODO Buffer vs ByteBuf translate, Can we handle that better ?
+        Preconditions.checkState(msg instanceof ByteBuf);
+        final ByteBuf byteBuf = (ByteBuf) msg;
+        final byte[] temp = new byte[byteBuf.readableBytes()];
+        byteBuf.readBytes(temp, 0, byteBuf.readableBytes());
+        return new Buffer(temp);
+    }
+
+    @Override
+    public synchronized void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) throws Exception {
+        this.connectPromise = promise;
+        startSsh(ctx, remoteAddress);
+    }
+
+    @Override
+    public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
+        disconnect(ctx, promise);
+    }
+
+    @Override
+    public synchronized void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
+        if(sshReadAsyncListener != null) {
+            sshReadAsyncListener.close();
+        }
+
+        session.close(false).addListener(new SshFutureListener<CloseFuture>() {
+            @Override
+            public void operationComplete(final CloseFuture future) {
+                if(future.isClosed() == false) {
+                    session.close(true);
+                }
+                session = null;
+            }
+        });
+
+        channel = null;
+    }
+
+    /**
+     * Listener over async input stream from SSH session.
+     * This listeners schedules reads in a loop until the session is closed or read fails.
+     */
+    private static class SshReadAsyncListener implements SshFutureListener<IoReadFuture>, AutoCloseable {
+        private static final int BUFFER_SIZE = 8192;
+
+        private final ChannelHandlerContext ctx;
+
+        private IoInputStream asyncOut;
+        private Buffer buf;
+        private IoReadFuture currentReadFuture;
+
+        public SshReadAsyncListener(final ChannelHandlerContext ctx, final IoInputStream asyncOut) {
+            this.ctx = ctx;
+            this.asyncOut = asyncOut;
+            buf = new Buffer(BUFFER_SIZE);
+            asyncOut.read(buf).addListener(this);
+        }
+
+        @Override
+        public synchronized void operationComplete(final IoReadFuture future) {
+            if(future.getException() != null) {
+
+                if(asyncOut.isClosed() || asyncOut.isClosing()) {
+                    // We are closing
+                    handleSshSessionClosed(ctx);
+                } else {
+                    logger.warn("Exception while reading from SSH remote on channel {}", ctx.channel(), future.getException());
+                    throw new IllegalStateException("Exception while reading from SSH remote on channel " + ctx.channel(), future.getException());
+                }
+            }
+
+            if (future.getRead() > 0) {
+                ctx.fireChannelRead(Unpooled.wrappedBuffer(buf.array(), 0, future.getRead()));
+
+                // Schedule next read
+                buf = new Buffer(BUFFER_SIZE);
+                currentReadFuture = asyncOut.read(buf);
+                currentReadFuture.addListener(this);
+            }
+        }
+
+        @Override
+        public synchronized void close() throws Exception {
+            // Remove self as listener on close to prevent reading from closed input
+            if(currentReadFuture != null) {
+                currentReadFuture.removeListener(this);
+            }
+
+            asyncOut = null;
+        }
+    }
+}
index febf3abf8e6fcbb6b035dfafffebb784e6392f44..8d48077f93174dbf211d4ac5be0568a9b6cfab9b 100644 (file)
       <groupId>org.opendaylight.controller.thirdparty</groupId>
       <artifactId>ganymed</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.sshd</groupId>
+      <artifactId>sshd-core</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>