2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.transport.ssh;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.bootstrap.ServerBootstrap;
13 import io.netty.channel.EventLoopGroup;
14 import java.io.IOException;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.netconf.shaded.sshd.client.future.AuthFuture;
17 import org.opendaylight.netconf.shaded.sshd.client.future.OpenFuture;
18 import org.opendaylight.netconf.shaded.sshd.common.session.Session;
19 import org.opendaylight.netconf.shaded.sshd.netty.NettyIoServiceFactoryFactory;
20 import org.opendaylight.netconf.transport.api.TransportChannelListener;
21 import org.opendaylight.netconf.transport.api.TransportStack;
22 import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException;
23 import org.opendaylight.netconf.transport.tcp.TCPClient;
24 import org.opendaylight.netconf.transport.tcp.TCPServer;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ssh.client.rev230417.SshClientGrouping;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.client.rev230417.TcpClientGrouping;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.server.rev230417.TcpServerGrouping;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * A {@link TransportStack} acting as an SSH client.
34 public final class SSHClient extends SSHTransportStack {
35 private static final Logger LOG = LoggerFactory.getLogger(SSHClient.class);
37 private final String subsystem;
39 private SSHClient(final String subsystem, final TransportChannelListener listener,
40 final TransportSshClient sshClient) {
41 super(listener, sshClient, sshClient.getSessionFactory());
42 // Mirrors check in ChannelSubsystem's constructor
43 if (subsystem.isBlank()) {
44 throw new IllegalArgumentException("Blank subsystem");
46 this.subsystem = subsystem;
49 static SSHClient of(final NettyIoServiceFactoryFactory ioServiceFactory, final EventLoopGroup group,
50 final String subsystem, final TransportChannelListener listener, final SshClientGrouping clientParams)
51 throws UnsupportedConfigurationException {
52 return new SSHClient(subsystem, listener, new TransportSshClient.Builder(ioServiceFactory, group)
53 .transportParams(clientParams.getTransportParams())
54 .keepAlives(clientParams.getKeepalives())
55 .clientIdentity(clientParams.getClientIdentity())
56 .serverAuthentication(clientParams.getServerAuthentication())
60 @NonNull ListenableFuture<SSHClient> connect(final Bootstrap bootstrap, final TcpClientGrouping connectParams)
61 throws UnsupportedConfigurationException {
62 return transformUnderlay(this, TCPClient.connect(asListener(), bootstrap, connectParams));
65 @NonNull ListenableFuture<SSHClient> listen(final ServerBootstrap bootstrap, final TcpServerGrouping listenParams)
66 throws UnsupportedConfigurationException {
67 return transformUnderlay(this, TCPServer.listen(asListener(), bootstrap, listenParams));
71 void onKeyEstablished(final Session session) throws IOException {
72 // server key is accepted, trigger authentication flow
73 final var sessionId = sessionId(session);
74 LOG.debug("Authenticating session {}", sessionId);
75 cast(session).auth().addListener(future -> onAuthComplete(future, sessionId));
78 private void onAuthComplete(final AuthFuture future, final Long sessionId) {
79 if (!future.isSuccess()) {
80 LOG.info("Session {} authentication failed", sessionId);
81 deleteSession(sessionId);
83 LOG.debug("Session {} authenticated", sessionId);
88 void onAuthenticated(final Session session) throws IOException {
89 final var sessionId = sessionId(session);
90 LOG.debug("Opening \"{}\" subsystem on session {}", subsystem, sessionId);
92 final var underlay = getUnderlayOf(sessionId);
93 final var clientSession = cast(session);
94 final var channel = clientSession.createSubsystemChannel(subsystem);
95 channel.onClose(() -> clientSession.close(true));
96 channel.open(underlay).addListener(future -> onSubsystemOpenComplete(future, sessionId));
99 private void onSubsystemOpenComplete(final OpenFuture future, final Long sessionId) {
100 if (future.isOpened()) {
101 transportEstablished(sessionId);
103 LOG.error("Failed to establish transport on session {}", sessionId, future.getException());
104 deleteSession(sessionId);
108 private static TransportClientSession cast(final Session session) throws IOException {
109 return TransportUtils.checkCast(TransportClientSession.class, session);