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.ssh;
11 import com.google.common.collect.ImmutableList;
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.List;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.TimeUnit;
21 import org.apache.sshd.SshServer;
22 import org.apache.sshd.common.Cipher;
23 import org.apache.sshd.common.FactoryManager;
24 import org.apache.sshd.common.NamedFactory;
25 import org.apache.sshd.common.RuntimeSshException;
26 import org.apache.sshd.common.cipher.ARCFOUR128;
27 import org.apache.sshd.common.cipher.ARCFOUR256;
28 import org.apache.sshd.common.io.IoAcceptor;
29 import org.apache.sshd.common.io.IoConnector;
30 import org.apache.sshd.common.io.IoHandler;
31 import org.apache.sshd.common.io.IoServiceFactory;
32 import org.apache.sshd.common.io.IoServiceFactoryFactory;
33 import org.apache.sshd.common.io.nio2.Nio2Acceptor;
34 import org.apache.sshd.common.io.nio2.Nio2Connector;
35 import org.apache.sshd.common.io.nio2.Nio2ServiceFactoryFactory;
36 import org.apache.sshd.common.util.CloseableUtils;
37 import org.apache.sshd.server.ServerFactoryManager;
40 * Proxy SSH server that just delegates decrypted content to a delegate server within same VM.
41 * Implemented using Apache Mina SSH lib.
43 public class SshProxyServer implements AutoCloseable {
45 private static final ARCFOUR128.Factory DEFAULT_ARCFOUR128_FACTORY = new ARCFOUR128.Factory();
46 private static final ARCFOUR256.Factory DEFAULT_ARCFOUR256_FACTORY = new ARCFOUR256.Factory();
47 private final SshServer sshServer;
48 private final ScheduledExecutorService minaTimerExecutor;
49 private final EventLoopGroup clientGroup;
50 private final IoServiceFactoryFactory nioServiceWithPoolFactoryFactory;
52 public SshProxyServer(final ScheduledExecutorService minaTimerExecutor,
53 final EventLoopGroup clientGroup, final ExecutorService nioExecutor) {
54 this.minaTimerExecutor = minaTimerExecutor;
55 this.clientGroup = clientGroup;
56 this.nioServiceWithPoolFactoryFactory =
57 new NioServiceWithPoolFactory.NioServiceWithPoolFactoryFactory(nioExecutor);
58 this.sshServer = SshServer.setUpDefaultServer();
61 public void bind(final SshProxyServerConfiguration sshProxyServerConfiguration) throws IOException {
62 sshServer.setHost(sshProxyServerConfiguration.getBindingAddress().getHostString());
63 sshServer.setPort(sshProxyServerConfiguration.getBindingAddress().getPort());
66 final List<NamedFactory<Cipher>> cipherFactories = sshServer.getCipherFactories();
67 cipherFactories.removeIf(factory -> factory.getName().contains(DEFAULT_ARCFOUR128_FACTORY.getName())
68 || factory.getName().contains(DEFAULT_ARCFOUR256_FACTORY.getName()));
69 sshServer.setPasswordAuthenticator(
70 (username, password, session)
71 -> sshProxyServerConfiguration.getAuthenticator().authenticated(username, password));
73 sshProxyServerConfiguration.getPublickeyAuthenticator().ifPresent(sshServer::setPublickeyAuthenticator);
75 sshServer.setKeyPairProvider(sshProxyServerConfiguration.getKeyPairProvider());
77 sshServer.setIoServiceFactoryFactory(nioServiceWithPoolFactoryFactory);
78 sshServer.setScheduledExecutorService(minaTimerExecutor);
79 sshServer.setProperties(getProperties(sshProxyServerConfiguration));
81 final RemoteNetconfCommand.NetconfCommandFactory netconfCommandFactory =
82 new RemoteNetconfCommand.NetconfCommandFactory(clientGroup,
83 sshProxyServerConfiguration.getLocalAddress());
84 sshServer.setSubsystemFactories(ImmutableList.of(netconfCommandFactory));
88 private static Map<String, String> getProperties(final SshProxyServerConfiguration sshProxyServerConfiguration) {
89 final Map<String, String> ret = new HashMap<>();
90 ret.put(ServerFactoryManager.IDLE_TIMEOUT, String.valueOf(sshProxyServerConfiguration.getIdleTimeout()));
91 // TODO make auth timeout configurable on its own
92 ret.put(ServerFactoryManager.AUTH_TIMEOUT, String.valueOf(sshProxyServerConfiguration.getIdleTimeout()));
100 sshServer.stop(true);
101 } catch (final InterruptedException e) {
102 throw new RuntimeException("Interrupted while stopping sshServer", e);
104 sshServer.close(true);
109 * Based on Nio2ServiceFactory with one addition: injectable executor.
111 private static final class NioServiceWithPoolFactory
112 extends CloseableUtils.AbstractCloseable implements IoServiceFactory {
114 private final FactoryManager manager;
115 private final AsynchronousChannelGroup group;
117 NioServiceWithPoolFactory(final FactoryManager manager, final ExecutorService executor) {
118 this.manager = manager;
120 group = AsynchronousChannelGroup.withThreadPool(executor);
121 } catch (final IOException e) {
122 throw new RuntimeSshException(e);
127 public IoConnector createConnector(final IoHandler handler) {
128 return new Nio2Connector(manager, handler, group);
132 public IoAcceptor createAcceptor(final IoHandler handler) {
133 return new Nio2Acceptor(manager, handler, group);
136 @SuppressWarnings("checkstyle:IllegalCatch")
138 protected void doCloseImmediately() {
141 group.awaitTermination(5, TimeUnit.SECONDS);
142 } catch (final Exception e) {
143 log.debug("Exception caught while closing channel group", e);
145 super.doCloseImmediately();
149 private static final class NioServiceWithPoolFactoryFactory extends Nio2ServiceFactoryFactory {
151 private final ExecutorService nioExecutor;
153 private NioServiceWithPoolFactoryFactory(final ExecutorService nioExecutor) {
154 this.nioExecutor = nioExecutor;
158 public IoServiceFactory create(final FactoryManager manager) {
159 return new NioServiceWithPoolFactory(manager, nioExecutor);