2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.nettyutil.handler.ssh.client;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelOutboundHandlerAdapter;
15 import io.netty.channel.ChannelPromise;
16 import io.netty.util.concurrent.Future;
17 import io.netty.util.concurrent.GenericFutureListener;
18 import java.io.IOException;
19 import java.net.SocketAddress;
20 import java.util.HashMap;
21 import org.apache.sshd.ClientChannel;
22 import org.apache.sshd.ClientSession;
23 import org.apache.sshd.SshClient;
24 import org.apache.sshd.client.future.AuthFuture;
25 import org.apache.sshd.client.future.ConnectFuture;
26 import org.apache.sshd.client.future.OpenFuture;
27 import org.apache.sshd.common.future.CloseFuture;
28 import org.apache.sshd.common.future.SshFutureListener;
29 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Netty SSH handler class. Acts as interface between Netty and SSH library.
36 public class AsyncSshHandler extends ChannelOutboundHandlerAdapter {
38 public static final String SUBSYSTEM = "netconf";
39 public static final SshClient DEFAULT_CLIENT = SshClient.setUpDefaultClient();
40 public static final int SSH_DEFAULT_NIO_WORKERS = 8;
41 private static final Logger LOG = LoggerFactory.getLogger(AsyncSshHandler.class);
42 // Disable default timeouts from mina sshd
43 private static final long DEFAULT_TIMEOUT = -1L;
46 DEFAULT_CLIENT.setProperties(new HashMap<String, String>(){
48 put(SshClient.AUTH_TIMEOUT, Long.toString(DEFAULT_TIMEOUT));
49 put(SshClient.IDLE_TIMEOUT, Long.toString(DEFAULT_TIMEOUT));
52 // TODO make configurable, or somehow reuse netty threadpool
53 DEFAULT_CLIENT.setNioWorkers(SSH_DEFAULT_NIO_WORKERS);
54 DEFAULT_CLIENT.start();
57 private final AuthenticationHandler authenticationHandler;
58 private final SshClient sshClient;
59 private Future negotiationFuture;
61 private AsyncSshHandlerReader sshReadAsyncListener;
62 private AsyncSshHandlerWriter sshWriteAsyncHandler;
64 private ClientChannel channel;
65 private ClientSession session;
66 private ChannelPromise connectPromise;
67 private GenericFutureListener negotiationFutureListener;
70 public AsyncSshHandler(final AuthenticationHandler authenticationHandler, final SshClient sshClient, final Future negotiationFuture) throws IOException {
71 this(authenticationHandler, sshClient);
72 this.negotiationFuture = negotiationFuture;
77 * @param authenticationHandler
78 * @param sshClient started SshClient
81 public AsyncSshHandler(final AuthenticationHandler authenticationHandler, final SshClient sshClient) throws IOException {
82 this.authenticationHandler = Preconditions.checkNotNull(authenticationHandler);
83 this.sshClient = Preconditions.checkNotNull(sshClient);
86 public static AsyncSshHandler createForNetconfSubsystem(final AuthenticationHandler authenticationHandler) throws IOException {
87 return new AsyncSshHandler(authenticationHandler, DEFAULT_CLIENT);
92 * Create AsyncSshHandler for netconf subsystem. Negotiation future has to be set to success after successful netconf
95 * @param authenticationHandler
96 * @param negotiationFuture
100 public static AsyncSshHandler createForNetconfSubsystem(final AuthenticationHandler authenticationHandler, final Future negotiationFuture) throws IOException {
101 return new AsyncSshHandler(authenticationHandler, DEFAULT_CLIENT, negotiationFuture);
104 private void startSsh(final ChannelHandlerContext ctx, final SocketAddress address) {
105 LOG.debug("Starting SSH to {} on channel: {}", address, ctx.channel());
107 final ConnectFuture sshConnectionFuture = sshClient.connect(authenticationHandler.getUsername(), address);
108 sshConnectionFuture.addListener(new SshFutureListener<ConnectFuture>() {
110 public void operationComplete(final ConnectFuture future) {
111 if (future.isConnected()) {
112 handleSshSessionCreated(future, ctx);
114 handleSshSetupFailure(ctx, future.getException());
120 private synchronized void handleSshSessionCreated(final ConnectFuture future, final ChannelHandlerContext ctx) {
122 LOG.trace("SSH session created on channel: {}", ctx.channel());
124 session = future.getSession();
125 final AuthFuture authenticateFuture = authenticationHandler.authenticate(session);
126 authenticateFuture.addListener(new SshFutureListener<AuthFuture>() {
128 public void operationComplete(final AuthFuture future) {
129 if (future.isSuccess()) {
130 handleSshAuthenticated(session, ctx);
132 // Exception does not have to be set in the future, add simple exception in such case
133 final Throwable exception = future.getException() == null ?
134 new IllegalStateException("Authentication failed") :
135 future.getException();
136 handleSshSetupFailure(ctx, exception);
140 } catch (final IOException e) {
141 handleSshSetupFailure(ctx, e);
145 private synchronized void handleSshAuthenticated(final ClientSession session, final ChannelHandlerContext ctx) {
147 LOG.debug("SSH session authenticated on channel: {}, server version: {}", ctx.channel(), session.getServerVersion());
149 channel = session.createSubsystemChannel(SUBSYSTEM);
150 channel.setStreaming(ClientChannel.Streaming.Async);
151 channel.open().addListener(new SshFutureListener<OpenFuture>() {
153 public void operationComplete(final OpenFuture future) {
154 if(future.isOpened()) {
155 handleSshChanelOpened(ctx);
157 handleSshSetupFailure(ctx, future.getException());
163 } catch (final IOException e) {
164 handleSshSetupFailure(ctx, e);
168 private synchronized void handleSshChanelOpened(final ChannelHandlerContext ctx) {
169 LOG.trace("SSH subsystem channel opened successfully on channel: {}", ctx.channel());
171 if(negotiationFuture == null) {
172 connectPromise.setSuccess();
175 // TODO we should also read from error stream and at least log from that
177 sshReadAsyncListener = new AsyncSshHandlerReader(new AutoCloseable() {
179 public void close() throws Exception {
180 AsyncSshHandler.this.disconnect(ctx, ctx.newPromise());
182 }, new AsyncSshHandlerReader.ReadMsgHandler() {
184 public void onMessageRead(final ByteBuf msg) {
185 ctx.fireChannelRead(msg);
187 }, channel.toString(), channel.getAsyncOut());
189 // if readAsyncListener receives immediate close, it will close this handler and closing this handler sets channel variable to null
190 if(channel != null) {
191 sshWriteAsyncHandler = new AsyncSshHandlerWriter(channel.getAsyncIn());
192 ctx.fireChannelActive();
196 private synchronized void handleSshSetupFailure(final ChannelHandlerContext ctx, final Throwable e) {
197 LOG.warn("Unable to setup SSH connection on channel: {}", ctx.channel(), e);
199 // If the promise is not yet done, we have failed with initial connect and set connectPromise to failure
200 if(!connectPromise.isDone()) {
201 connectPromise.setFailure(e);
204 disconnect(ctx, ctx.newPromise());
208 public synchronized void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
209 sshWriteAsyncHandler.write(ctx, msg, promise);
213 public synchronized void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) throws Exception {
214 LOG.debug("SSH session connecting on channel {}. promise: {} ", ctx.channel(), connectPromise);
215 this.connectPromise = promise;
217 if(negotiationFuture != null) {
219 negotiationFutureListener = new GenericFutureListener<Future<Object>>() {
221 public void operationComplete(Future future) throws Exception {
222 if (future.isSuccess())
223 connectPromise.setSuccess();
226 //complete connection promise with netconf negotiation future
227 negotiationFuture.addListener(negotiationFutureListener);
229 startSsh(ctx, remoteAddress);
233 public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
234 disconnect(ctx, promise);
238 public synchronized void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) {
239 LOG.trace("Closing SSH session on channel: {} with connect promise in state: {}", ctx.channel(), connectPromise);
241 // If we have already succeeded and the session was dropped after, we need to fire inactive to notify reconnect logic
242 if(connectPromise.isSuccess()) {
243 ctx.fireChannelInactive();
246 if(sshWriteAsyncHandler != null) {
247 sshWriteAsyncHandler.close();
250 if(sshReadAsyncListener != null) {
251 sshReadAsyncListener.close();
254 //If connection promise is not already set, it means negotiation failed
255 //we must set connection promise to failure
256 if(!connectPromise.isDone()) {
257 connectPromise.setFailure(new IllegalStateException("Negotiation failed"));
260 //Remove listener from negotiation future, we don't want notifications
261 //from negotiation anymore
262 if(negotiationFuture != null) {
263 negotiationFuture.removeListener(negotiationFutureListener);
266 if(session!= null && !session.isClosed() && !session.isClosing()) {
267 session.close(false).addListener(new SshFutureListener<CloseFuture>() {
269 public void operationComplete(final CloseFuture future) {
270 if (future.isClosed() == false) {
278 // Super disconnect is necessary in this case since we are using NioSocketChannel and it needs to cleanup its resources
279 // e.g. Socket that it tries to open in its constructor (https://bugs.opendaylight.org/show_bug.cgi?id=2430)
280 // TODO better solution would be to implement custom ChannelFactory + Channel that will use mina SSH lib internally: port this to custom channel implementation
282 // Disconnect has to be closed after inactive channel event was fired, because it interferes with it
283 super.disconnect(ctx, ctx.newPromise());
284 } catch (final Exception e) {
285 LOG.warn("Unable to cleanup all resources for channel: {}. Ignoring.", ctx.channel(), e);
289 promise.setSuccess();
290 LOG.debug("SSH session closed on channel: {}", ctx.channel());