2 * Copyright (c) 2016 Brocade Communication Systems 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
8 package org.opendaylight.netconf.callhome.protocol;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import java.io.IOException;
14 import java.net.InetSocketAddress;
15 import java.net.SocketAddress;
16 import java.security.PublicKey;
17 import org.apache.sshd.client.SshClient;
18 import org.apache.sshd.client.future.AuthFuture;
19 import org.apache.sshd.client.keyverifier.ServerKeyVerifier;
20 import org.apache.sshd.client.session.ClientSession;
21 import org.apache.sshd.client.session.SessionFactory;
22 import org.apache.sshd.common.future.SshFutureListener;
23 import org.apache.sshd.common.io.IoAcceptor;
24 import org.apache.sshd.common.io.IoServiceFactory;
25 import org.apache.sshd.common.session.Session;
26 import org.apache.sshd.common.session.SessionListener;
27 import org.apache.sshd.netty.NettyIoServiceFactory;
28 import org.opendaylight.netconf.callhome.protocol.CallHomeSessionContext.Factory;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class NetconfCallHomeServer implements AutoCloseable, ServerKeyVerifier {
34 private static final Logger LOG = LoggerFactory.getLogger(NetconfCallHomeServer.class);
36 private final CallHomeAuthorizationProvider authProvider;
37 private final IoServiceFactory serviceFactory;
38 private final InetSocketAddress bindAddress;
39 private final StatusRecorder recorder;
40 private final Factory sessionFactory;
41 private final IoAcceptor acceptor;
42 private final SshClient client;
44 NetconfCallHomeServer(final SshClient sshClient, final CallHomeAuthorizationProvider authProvider,
45 final Factory factory, final InetSocketAddress socketAddress, final StatusRecorder recorder) {
46 this(sshClient, authProvider, factory, socketAddress, recorder,
47 new NettyIoServiceFactory(factory.getNettyGroup()));
51 NetconfCallHomeServer(final SshClient sshClient, final CallHomeAuthorizationProvider authProvider,
52 final Factory factory, final InetSocketAddress socketAddress, final StatusRecorder recorder,
53 final IoServiceFactory serviceFactory) {
54 this.client = requireNonNull(sshClient);
55 this.authProvider = requireNonNull(authProvider);
56 this.sessionFactory = requireNonNull(factory);
57 this.bindAddress = socketAddress;
58 this.recorder = recorder;
59 this.serviceFactory = requireNonNull(serviceFactory);
61 sshClient.setServerKeyVerifier(this);
62 sshClient.addSessionListener(createSessionListener());
64 acceptor = serviceFactory.createAcceptor(new SessionFactory(sshClient));
68 SshClient getClient() {
72 SessionListener createSessionListener() {
73 return new SessionListener() {
75 public void sessionEvent(final Session session, final Event event) {
76 ClientSession clientSession = (ClientSession) session;
77 LOG.debug("SSH session {} event {}", session, event);
80 doAuth(clientSession);
83 CallHomeSessionContext.getFrom(clientSession).openNetconfChannel();
91 public void sessionCreated(final Session session) {
92 LOG.debug("SSH session {} created", session);
96 public void sessionClosed(final Session session) {
97 CallHomeSessionContext ctx = CallHomeSessionContext.getFrom((ClientSession) session);
101 LOG.debug("SSH Session {} closed", session);
104 private void doAuth(final ClientSession session) {
106 final AuthFuture authFuture = CallHomeSessionContext.getFrom(session).authorize();
107 authFuture.addListener(newAuthSshFutureListener(session));
108 } catch (IOException e) {
109 LOG.error("Failed to authorize session {}", session, e);
115 private SshFutureListener<AuthFuture> newAuthSshFutureListener(final ClientSession session) {
116 final PublicKey serverKey = session.getKex().getServerKey();
118 return new SshFutureListener<AuthFuture>() {
120 public void operationComplete(final AuthFuture authFuture) {
121 if (authFuture.isSuccess()) {
123 } else if (authFuture.isFailure()) {
124 onFailure(authFuture.getException());
125 } else if (authFuture.isCanceled()) {
128 authFuture.removeListener(this);
131 private void onSuccess() {
132 LOG.debug("Authorize success");
135 private void onFailure(final Throwable throwable) {
136 LOG.error("Authorize failed for session {}", session, throwable);
137 recorder.reportFailedAuth(serverKey);
141 private void onCanceled() {
142 LOG.warn("Authorize cancelled");
149 public boolean verifyServerKey(final ClientSession sshClientSession, final SocketAddress remoteAddress,
150 final PublicKey serverKey) {
151 final CallHomeAuthorization authorization = authProvider.provideAuth(remoteAddress, serverKey);
152 // server is not authorized
153 if (!authorization.isServerAllowed()) {
154 LOG.info("Incoming session {} was rejected by Authorization Provider.", sshClientSession);
157 CallHomeSessionContext session = sessionFactory.createIfNotExists(
158 sshClientSession, authorization, remoteAddress);
159 // Session was created, session with same name does not exists
160 if (session != null) {
163 // Session was not created, session with same name exists
164 LOG.info("Incoming session {} was rejected. Session with same name {} is already active.",
165 sshClientSession, authorization.getSessionName());
169 public void bind() throws IOException {
172 acceptor.bind(bindAddress);
173 } catch (IOException e) {
174 LOG.error("Unable to start NETCONF CallHome Service on {}", bindAddress, e);
180 public void close() {
181 acceptor.close(true);
182 serviceFactory.close(true);