From 33631d99f259c0d0f4f696acaf1a9c25a2ae09f1 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 23 Oct 2023 19:12:24 +0200 Subject: [PATCH] Centralize NETCONF over SSH subsystem name We have quite a few places which hard-code the "netconf" string. Centralize them in TransportConstants along with pointer to where it is defined. JIRA: NETCONF-1106 JIRA: NETCONF-1108 Change-Id: Ifc273c084a59f52c37c7d123de7256f682d5a0d9 Signed-off-by: Robert Varga --- .../northbound/SshServerTransport.java | 6 ++-- .../protocol/CallHomeSessionContext.java | 7 ++-- .../handler/ssh/client/AsyncSshHandler.java | 5 ++- .../netconf/api/TransportConstants.java | 32 +++++++++++++++++++ .../client/NetconfClientFactoryImpl.java | 6 ++-- .../server/NetconfServerFactoryImpl.java | 5 +-- 6 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/TransportConstants.java diff --git a/apps/netconf-nb/src/main/java/org/opendaylight/netconf/northbound/SshServerTransport.java b/apps/netconf-nb/src/main/java/org/opendaylight/netconf/northbound/SshServerTransport.java index 9260b42fb0..099cb03409 100644 --- a/apps/netconf-nb/src/main/java/org/opendaylight/netconf/northbound/SshServerTransport.java +++ b/apps/netconf-nb/src/main/java/org/opendaylight/netconf/northbound/SshServerTransport.java @@ -10,6 +10,7 @@ package org.opendaylight.netconf.northbound; import java.io.IOException; import java.util.List; import java.util.concurrent.ExecutionException; +import org.opendaylight.netconf.api.TransportConstants; import org.opendaylight.netconf.auth.AuthProvider; import org.opendaylight.netconf.server.ServerChannelInitializer; import org.opendaylight.netconf.server.ServerTransportInitializer; @@ -42,6 +43,7 @@ public final class SshServerTransport implements AutoCloseable { public @interface Configuration { @AttributeDefinition String bindingAddress() default "0.0.0.0"; + // NOTE: default is not TransportConstants.SSH_TCP_PORT to allow unprivileged execution @AttributeDefinition(min = "1", max = "65535") int portNumber() default 2830; } @@ -67,8 +69,8 @@ public final class SshServerTransport implements AutoCloseable { final var localPort = listenParams.requireLocalPort().getValue(); try { - sshServer = factoryHolder.factory().listenServer("netconf", new ServerTransportInitializer(initializer), - listenParams, null, factoryMgr -> { + sshServer = factoryHolder.factory().listenServer(TransportConstants.SSH_SUBSYSTEM, + new ServerTransportInitializer(initializer), listenParams, null, factoryMgr -> { factoryMgr.setUserAuthFactories(List.of(UserAuthPasswordFactory.INSTANCE)); factoryMgr.setPasswordAuthenticator( (username, password, session) -> authProvider.authenticated(username, password)); diff --git a/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java b/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java index bfaca07c09..7f7fe81e5e 100644 --- a/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java +++ b/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java @@ -24,6 +24,7 @@ import java.security.PublicKey; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import org.eclipse.jdt.annotation.Nullable; +import org.opendaylight.netconf.api.TransportConstants; import org.opendaylight.netconf.client.NetconfClientSession; import org.opendaylight.netconf.client.NetconfClientSessionListener; import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory; @@ -42,9 +43,7 @@ import org.slf4j.LoggerFactory; // Non-final for testing class CallHomeSessionContext implements CallHomeProtocolSessionContext { - private static final Logger LOG = LoggerFactory.getLogger(CallHomeSessionContext.class); - private static final String NETCONF = "netconf"; @VisibleForTesting static final Session.AttributeKey SESSION_KEY = new Session.AttributeKey<>(); @@ -86,8 +85,8 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext { LOG.debug("Opening NETCONF Subsystem on {}", sshSession); try { final MinaSshNettyChannel nettyChannel = newMinaSshNettyChannel(); - final ClientChannel netconfChannel = - ((NetconfClientSessionImpl) sshSession).createSubsystemChannel(NETCONF, nettyChannel.pipeline()); + final ClientChannel netconfChannel = ((NetconfClientSessionImpl) sshSession).createSubsystemChannel( + TransportConstants.SSH_SUBSYSTEM, nettyChannel.pipeline()); netconfChannel.setStreaming(ClientChannel.Streaming.Async); netconfChannel.open().addListener(newSshFutureListener(netconfChannel, nettyChannel)); } catch (IOException e) { diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java index 32ebf5348e..c9660544ef 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java @@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit; import org.checkerframework.checker.lock.qual.GuardedBy; import org.checkerframework.checker.lock.qual.Holding; import org.eclipse.jdt.annotation.Nullable; +import org.opendaylight.netconf.api.TransportConstants; import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler; import org.opendaylight.netconf.shaded.sshd.client.channel.ChannelSubsystem; import org.opendaylight.netconf.shaded.sshd.client.channel.ClientChannel; @@ -51,8 +52,6 @@ public final class AsyncSshHandler extends ChannelOutboundHandlerAdapter { } } - public static final String SUBSYSTEM = "netconf"; - public static final int SSH_DEFAULT_NIO_WORKERS = 8; public static final NetconfSshClient DEFAULT_CLIENT; @@ -201,7 +200,7 @@ public final class AsyncSshHandler extends ChannelOutboundHandlerAdapter { final OpenFuture openFuture; try { - channel = clientSession.createSubsystemChannel(SUBSYSTEM, ctx); + channel = clientSession.createSubsystemChannel(TransportConstants.SSH_SUBSYSTEM, ctx); channel.setStreaming(ClientChannel.Streaming.Async); openFuture = channel.open(); } catch (final IOException e) { diff --git a/protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/TransportConstants.java b/protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/TransportConstants.java new file mode 100644 index 0000000000..88d522b759 --- /dev/null +++ b/protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/TransportConstants.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.netconf.api; + +import org.eclipse.jdt.annotation.NonNullByDefault; + +/** + * Various constants related to NETCONF transport layer. + */ +@NonNullByDefault +public final class TransportConstants { + /** + * The name of the SSH subsystem used to carry NETCONF sessions, as defined in + * RFC6242. + */ + public static final String SSH_SUBSYSTEM = "netconf"; + + /** + * The default TCP port to use for NETCONF over SSH, as defined in + * RFC6242. + */ + public static final int SSH_TCP_PORT = 830; + + private TransportConstants() { + // Hidden on purpose + } +} diff --git a/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java b/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java index 06b95737b7..36ddec0345 100644 --- a/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java +++ b/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java @@ -18,6 +18,7 @@ import com.google.common.util.concurrent.SettableFuture; import io.netty.util.HashedWheelTimer; import io.netty.util.Timer; import javax.inject.Singleton; +import org.opendaylight.netconf.api.TransportConstants; import org.opendaylight.netconf.client.conf.NetconfClientConfiguration; import org.opendaylight.netconf.transport.api.TransportChannel; import org.opendaylight.netconf.transport.api.TransportChannelListener; @@ -74,8 +75,9 @@ public class NetconfClientFactoryImpl implements NetconfClientFactory { configuration.getTcpParameters(), configuration.getTransportSslHandlerFactory()); } } else if (SSH.equals(protocol)) { - factory.connectClient("netconf", new ClientTransportChannelListener(future, channelInitializer), - configuration.getTcpParameters(), configuration.getSshParameters()); + factory.connectClient(TransportConstants.SSH_SUBSYSTEM, + new ClientTransportChannelListener(future, channelInitializer), configuration.getTcpParameters(), + configuration.getSshParameters()); } return future; } diff --git a/protocol/netconf-server/src/main/java/org/opendaylight/netconf/server/NetconfServerFactoryImpl.java b/protocol/netconf-server/src/main/java/org/opendaylight/netconf/server/NetconfServerFactoryImpl.java index d5e971659c..9a4d577331 100644 --- a/protocol/netconf-server/src/main/java/org/opendaylight/netconf/server/NetconfServerFactoryImpl.java +++ b/protocol/netconf-server/src/main/java/org/opendaylight/netconf/server/NetconfServerFactoryImpl.java @@ -10,6 +10,7 @@ package org.opendaylight.netconf.server; import static java.util.Objects.requireNonNull; import com.google.common.util.concurrent.ListenableFuture; +import org.opendaylight.netconf.api.TransportConstants; import org.opendaylight.netconf.server.api.NetconfServerFactory; import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException; import org.opendaylight.netconf.transport.ssh.SSHServer; @@ -40,7 +41,7 @@ public final class NetconfServerFactoryImpl implements NetconfServerFactory { public ListenableFuture createSshServer(final TcpServerGrouping tcpParams, final SshServerGrouping sshParams, final ServerFactoryManagerConfigurator configurator) throws UnsupportedConfigurationException { - return factory.listenServer("netconf", new ServerTransportInitializer(channelInitializer), tcpParams, sshParams, - configurator); + return factory.listenServer(TransportConstants.SSH_SUBSYSTEM, + new ServerTransportInitializer(channelInitializer), tcpParams, sshParams, configurator); } } -- 2.36.6