This is an automated email from the ASF dual-hosted git repository.

pvillard31 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new f1faf798ad1 NIFI-16622 Added closeQuietly in 
SocketUtils.createSocket() (#11961)
f1faf798ad1 is described below

commit f1faf798ad14030254f2280c0e1b878f7df8ba94
Author: David Handermann <[email protected]>
AuthorDate: Sun Nov 22 05:14:07 2026 -0600

    NIFI-16622 Added closeQuietly in SocketUtils.createSocket() (#11961)
---
 .../org/apache/nifi/io/socket/SocketUtils.java     | 25 ++++++++++-------
 .../org/apache/nifi/io/socket/SocketUtilsTest.java | 32 ++++++++++++++++++++++
 2 files changed, 47 insertions(+), 10 deletions(-)

diff --git 
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketUtils.java
 
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketUtils.java
index 23e6e39e646..901c20abefc 100644
--- 
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketUtils.java
+++ 
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketUtils.java
@@ -105,16 +105,21 @@ public final class SocketUtils {
         if (sslContext == null) {
             connectedSocket = socket;
         } else {
-            // Create SSLSocket after successful connection to support 
hostname verification
-            final SSLSocketFactory sslSocketFactory = 
sslContext.getSocketFactory();
-            final SSLSocket sslSocket = (SSLSocket) 
sslSocketFactory.createSocket(socket, address.getHostName(), address.getPort(), 
true);
-
-            
sslSocket.setEnabledProtocols(TlsPlatform.getPreferredProtocols().toArray(new 
String[0]));
-            final SSLParameters sslParameters = sslSocket.getSSLParameters();
-            
sslParameters.setEndpointIdentificationAlgorithm(TLS_ENDPOINT_IDENTIFICATION_ALGORITHM);
-            sslSocket.setSSLParameters(sslParameters);
-
-            connectedSocket = sslSocket;
+            try {
+                // Create SSLSocket after successful connection to support 
hostname verification
+                final SSLSocketFactory sslSocketFactory = 
sslContext.getSocketFactory();
+                final SSLSocket sslSocket = (SSLSocket) 
sslSocketFactory.createSocket(socket, address.getHostName(), address.getPort(), 
true);
+
+                
sslSocket.setEnabledProtocols(TlsPlatform.getPreferredProtocols().toArray(new 
String[0]));
+                final SSLParameters sslParameters = 
sslSocket.getSSLParameters();
+                
sslParameters.setEndpointIdentificationAlgorithm(TLS_ENDPOINT_IDENTIFICATION_ALGORITHM);
+                sslSocket.setSSLParameters(sslParameters);
+
+                connectedSocket = sslSocket;
+            } catch (final Exception e) {
+                closeQuietly(socket);
+                throw e;
+            }
         }
 
         return connectedSocket;
diff --git 
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/test/java/org/apache/nifi/io/socket/SocketUtilsTest.java
 
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/test/java/org/apache/nifi/io/socket/SocketUtilsTest.java
index f58d7334728..bceac03fde3 100644
--- 
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/test/java/org/apache/nifi/io/socket/SocketUtilsTest.java
+++ 
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/test/java/org/apache/nifi/io/socket/SocketUtilsTest.java
@@ -41,11 +41,19 @@ import java.util.concurrent.Future;
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLHandshakeException;
 import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
 import javax.security.auth.x500.X500Principal;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 class SocketUtilsTest {
 
@@ -107,6 +115,30 @@ class SocketUtilsTest {
         }
     }
 
+    @Test
+    void testCreateSocketConnectedException() throws IOException {
+        try (ServerSocket serverSocket = new ServerSocket(0, 1, 
InetAddress.getLoopbackAddress())) {
+            final SocketConfiguration config = new SocketConfiguration();
+            config.setConnectTimeout(CONNECT_TIMEOUT_MILLISECONDS);
+            config.setSocketTimeout(TIMEOUT_MILLISECONDS);
+
+            final SSLContext sslContext = mock(SSLContext.class);
+            final SSLSocketFactory sslSocketFactory = 
mock(SSLSocketFactory.class);
+            when(sslContext.getSocketFactory()).thenReturn(sslSocketFactory);
+
+            final IOException expectedException = new 
IOException("createSocket failed");
+            when(sslSocketFactory.createSocket(any(Socket.class), anyString(), 
anyInt(), anyBoolean())).thenThrow(expectedException);
+            config.setSslContext(sslContext);
+
+            final IOException exception = assertThrows(IOException.class, () 
-> {
+                try (Socket socket = 
SocketUtils.createSocket(InetSocketAddress.createUnresolved(LOCALHOST, 
serverSocket.getLocalPort()), config)) {
+                    assertFalse(socket.isClosed(), "Socket not closed");
+                }
+            });
+            assertEquals(expectedException, exception, "createSocket Exception 
not matched");
+        }
+    }
+
     private void connectAndHandshake(final KeyPair serverKeyPair, final 
X509Certificate serverCertificate) throws Exception {
         final SSLContext serverSslContext = 
buildServerSslContext(serverKeyPair, serverCertificate);
         final SSLContext clientSslContext = 
buildClientSslContext(serverCertificate);

Reply via email to