b91bdc8da67d95a630077170ae0cdd78869a32af
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / SshProxyServer.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.ssh;
10
11 import com.google.common.collect.Lists;
12 import io.netty.channel.EventLoopGroup;
13 import java.io.IOException;
14 import java.nio.channels.AsynchronousChannelGroup;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.TimeUnit;
22 import org.apache.sshd.SshServer;
23 import org.apache.sshd.common.Cipher;
24 import org.apache.sshd.common.FactoryManager;
25 import org.apache.sshd.common.NamedFactory;
26 import org.apache.sshd.common.RuntimeSshException;
27 import org.apache.sshd.common.cipher.ARCFOUR128;
28 import org.apache.sshd.common.cipher.ARCFOUR256;
29 import org.apache.sshd.common.io.IoAcceptor;
30 import org.apache.sshd.common.io.IoConnector;
31 import org.apache.sshd.common.io.IoHandler;
32 import org.apache.sshd.common.io.IoServiceFactory;
33 import org.apache.sshd.common.io.IoServiceFactoryFactory;
34 import org.apache.sshd.common.io.nio2.Nio2Acceptor;
35 import org.apache.sshd.common.io.nio2.Nio2Connector;
36 import org.apache.sshd.common.io.nio2.Nio2ServiceFactoryFactory;
37 import org.apache.sshd.common.util.CloseableUtils;
38 import org.apache.sshd.server.Command;
39 import org.apache.sshd.server.ServerFactoryManager;
40
41 /**
42  * Proxy SSH server that just delegates decrypted content to a delegate server within same VM.
43  * Implemented using Apache Mina SSH lib.
44  */
45 public class SshProxyServer implements AutoCloseable {
46
47     private static final ARCFOUR128.Factory DEFAULT_ARCFOUR128_FACTORY = new ARCFOUR128.Factory();
48     private static final ARCFOUR256.Factory DEFAULT_ARCFOUR256_FACTORY = new ARCFOUR256.Factory();
49     private final SshServer sshServer;
50     private final ScheduledExecutorService minaTimerExecutor;
51     private final EventLoopGroup clientGroup;
52     private final IoServiceFactoryFactory nioServiceWithPoolFactoryFactory;
53
54     public SshProxyServer(final ScheduledExecutorService minaTimerExecutor, final EventLoopGroup clientGroup, final ExecutorService nioExecutor) {
55         this.minaTimerExecutor = minaTimerExecutor;
56         this.clientGroup = clientGroup;
57         this.nioServiceWithPoolFactoryFactory = new NioServiceWithPoolFactory.NioServiceWithPoolFactoryFactory(nioExecutor);
58         this.sshServer = SshServer.setUpDefaultServer();
59     }
60
61     public void bind(final SshProxyServerConfiguration sshProxyServerConfiguration) throws IOException {
62         sshServer.setHost(sshProxyServerConfiguration.getBindingAddress().getHostString());
63         sshServer.setPort(sshProxyServerConfiguration.getBindingAddress().getPort());
64
65         //remove rc4 ciphers
66         final List<NamedFactory<Cipher>> cipherFactories = sshServer.getCipherFactories();
67         for (Iterator<NamedFactory<Cipher>> i = cipherFactories.iterator(); i.hasNext(); ) {
68             final NamedFactory<Cipher> factory = i.next();
69             if (factory.getName().contains(DEFAULT_ARCFOUR128_FACTORY.getName())
70                     || factory.getName().contains(DEFAULT_ARCFOUR256_FACTORY.getName())) {
71                 i.remove();
72             }
73         }
74         sshServer.setPasswordAuthenticator(sshProxyServerConfiguration.getAuthenticator());
75         sshServer.setKeyPairProvider(sshProxyServerConfiguration.getKeyPairProvider());
76
77         sshServer.setIoServiceFactoryFactory(nioServiceWithPoolFactoryFactory);
78         sshServer.setScheduledExecutorService(minaTimerExecutor);
79         sshServer.setProperties(getProperties(sshProxyServerConfiguration));
80
81         final RemoteNetconfCommand.NetconfCommandFactory netconfCommandFactory =
82                 new RemoteNetconfCommand.NetconfCommandFactory(clientGroup, sshProxyServerConfiguration.getLocalAddress());
83         sshServer.setSubsystemFactories(Lists.<NamedFactory<Command>>newArrayList(netconfCommandFactory));
84         sshServer.start();
85     }
86
87     private static Map<String, String> getProperties(final SshProxyServerConfiguration sshProxyServerConfiguration) {
88         return new HashMap<String, String>()
89         {
90             {
91                 put(ServerFactoryManager.IDLE_TIMEOUT, String.valueOf(sshProxyServerConfiguration.getIdleTimeout()));
92                 // TODO make auth timeout configurable on its own
93                 put(ServerFactoryManager.AUTH_TIMEOUT, String.valueOf(sshProxyServerConfiguration.getIdleTimeout()));
94             }
95         };
96     }
97
98     @Override
99     public void close() {
100         try {
101             sshServer.stop(true);
102         } catch (final InterruptedException e) {
103             throw new RuntimeException("Interrupted while stopping sshServer", e);
104         } finally {
105             sshServer.close(true);
106         }
107     }
108
109     /**
110      * Based on Nio2ServiceFactory with one addition: injectable executor
111      */
112     private static final class NioServiceWithPoolFactory extends CloseableUtils.AbstractCloseable implements IoServiceFactory {
113
114         private final FactoryManager manager;
115         private final AsynchronousChannelGroup group;
116
117         public NioServiceWithPoolFactory(final FactoryManager manager, final ExecutorService executor) {
118             this.manager = manager;
119             try {
120                 group = AsynchronousChannelGroup.withThreadPool(executor);
121             } catch (final IOException e) {
122                 throw new RuntimeSshException(e);
123             }
124         }
125
126         public IoConnector createConnector(final IoHandler handler) {
127             return new Nio2Connector(manager, handler, group);
128         }
129
130         public IoAcceptor createAcceptor(final IoHandler handler) {
131             return new Nio2Acceptor(manager, handler, group);
132         }
133
134         @Override
135         protected void doCloseImmediately() {
136             try {
137                 group.shutdownNow();
138                 group.awaitTermination(5, TimeUnit.SECONDS);
139             } catch (final Exception e) {
140                 log.debug("Exception caught while closing channel group", e);
141             } finally {
142                 super.doCloseImmediately();
143             }
144         }
145
146         private static final class NioServiceWithPoolFactoryFactory extends Nio2ServiceFactoryFactory {
147
148             private final ExecutorService nioExecutor;
149
150             private NioServiceWithPoolFactoryFactory(final ExecutorService nioExecutor) {
151                 this.nioExecutor = nioExecutor;
152             }
153
154             @Override
155             public IoServiceFactory create(final FactoryManager manager) {
156                 return new NioServiceWithPoolFactory(manager, nioExecutor);
157             }
158         }
159     }
160
161 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.