Centralize NETCONF over SSH subsystem name 00/108600/4
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 23 Oct 2023 17:12:24 +0000 (19:12 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 24 Oct 2023 06:45:01 +0000 (06:45 +0000)
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 <robert.varga@pantheon.tech>
apps/netconf-nb/src/main/java/org/opendaylight/netconf/northbound/SshServerTransport.java
netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java
netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java
protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/TransportConstants.java [new file with mode: 0644]
protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java
protocol/netconf-server/src/main/java/org/opendaylight/netconf/server/NetconfServerFactoryImpl.java

index 9260b42fb06e777a6ab3a847b359ac090ce77ef4..099cb03409183db15c7c506964e4d0776a019a9f 100644 (file)
@@ -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));
index bfaca07c09d533be203e2f05167023ce690937eb..7f7fe81e5eab6458bfa9ce1a844a5f980825ff6b 100644 (file)
@@ -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<CallHomeSessionContext> 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) {
index 32ebf5348e981178d099d7e77599291575a77fda..c9660544eff95b68e6a1c9384d876475c7c3988d 100644 (file)
@@ -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 (file)
index 0000000..88d522b
--- /dev/null
@@ -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
+     * <a href="https://www.rfc-editor.org/rfc/rfc6242#section-7">RFC6242</a>.
+     */
+    public static final String SSH_SUBSYSTEM = "netconf";
+
+    /**
+     * The default TCP port to use for NETCONF over SSH, as defined in
+     * <a href="https://www.rfc-editor.org/rfc/rfc6242#section-7">RFC6242</a>.
+     */
+    public static final int SSH_TCP_PORT = 830;
+
+    private TransportConstants() {
+        // Hidden on purpose
+    }
+}
index 06b95737b75b846b63ff6c931973f91c78574956..36ddec034509f1a00de995beb56847d9383fb7fc 100644 (file)
@@ -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;
     }
index d5e971659cf7362c5423d0653e5135de9b3a0ebb..9a4d577331753a6ad4280596ab5f51315f72ea23 100644 (file)
@@ -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<SSHServer> 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);
     }
 }