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

coheigea pushed a commit to branch 4.1.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit d9146f1a48e2bbbfc9343fbfbcf3aac2ee0705a1
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Wed Nov 25 14:29:52 2026 +0000

    Close keystore and truststore streams in TLSParameterJaxBUtils (#3778)
    
    (cherry picked from commit 52636ed6ba2d6f6458cb90c1cd12653a2f31cfb0)
---
 .../configuration/jsse/TLSParameterJaxBUtils.java  | 28 ++++++++-----
 .../jsse/TLSParameterJaxBUtilsTest.java            | 47 ++++++++++++++++++++++
 2 files changed, 64 insertions(+), 11 deletions(-)

diff --git 
a/core/src/main/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtils.java
 
b/core/src/main/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtils.java
index 45e734e2e06..9d79ed2b047 100644
--- 
a/core/src/main/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtils.java
+++ 
b/core/src/main/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtils.java
@@ -166,7 +166,9 @@ public final class TLSParameterJaxBUtils {
                 keyStore.load(is, password);
             }
         } else if (kst.isSetUrl()) {
-            keyStore.load(openKeystoreUrl(kst.getUrl()), password);
+            try (InputStream is = openKeystoreUrl(kst.getUrl())) {
+                keyStore.load(is, password);
+            }
         } else {
             final String loc;
             if (trustStore) {
@@ -204,21 +206,25 @@ public final class TLSParameterJaxBUtils {
             type = KeyStore.getDefaultType();
         }
         if (pst.isSetFile()) {
-            InputStream is = Files.newInputStream(Paths.get(pst.getFile()));
-            return createTrustStore(is, type);
+            try (InputStream is = 
Files.newInputStream(Paths.get(pst.getFile()))) {
+                return createTrustStore(is, type);
+            }
         }
         if (pst.isSetResource()) {
-            final InputStream is = getResourceAsStream(pst.getResource());
-            if (is == null) {
-                final String msg =
-                    "Could not load truststore resource " + pst.getResource();
-                LOG.severe(msg);
-                throw new IOException(msg);
+            try (InputStream is = getResourceAsStream(pst.getResource())) {
+                if (is == null) {
+                    final String msg =
+                        "Could not load truststore resource " + 
pst.getResource();
+                    LOG.severe(msg);
+                    throw new IOException(msg);
+                }
+                return createTrustStore(is, type);
             }
-            return createTrustStore(is, type);
         }
         if (pst.isSetUrl()) {
-            return createTrustStore(openKeystoreUrl(pst.getUrl()), type);
+            try (InputStream is = openKeystoreUrl(pst.getUrl())) {
+                return createTrustStore(is, type);
+            }
         }
         throw new IllegalArgumentException("Could not create KeyStore based on 
information in CertStoreType");
     }
diff --git 
a/core/src/test/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtilsTest.java
 
b/core/src/test/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtilsTest.java
index 49706cdc58e..bdb59b152e8 100644
--- 
a/core/src/test/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtilsTest.java
+++ 
b/core/src/test/java/org/apache/cxf/configuration/jsse/TLSParameterJaxBUtilsTest.java
@@ -22,14 +22,18 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.security.KeyStore;
 
+import org.apache.cxf.configuration.security.CertStoreType;
 import org.apache.cxf.configuration.security.KeyStoreType;
 
+import org.junit.Assume;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 /**
@@ -90,4 +94,47 @@ public class TLSParameterJaxBUtilsTest {
         }
     }
 
+    private static long openFileDescriptors() throws IOException {
+        try (java.util.stream.Stream<Path> fds = 
Files.list(Paths.get("/proc/self/fd"))) {
+            return fds.count();
+        }
+    }
+
+    @Test
+    public void testKeystoreStreamsAreClosed() throws Exception {
+        Assume.assumeTrue(Files.isDirectory(Paths.get("/proc/self/fd")));
+        final String password = "changeit";
+        final Path tempJks = Files.createTempFile("cxf-urlstore-", ".jks");
+        final Path tempCerts = Files.createTempFile("cxf-certstore-", ".pem");
+        try {
+            KeyStore source = KeyStore.getInstance("JKS");
+            source.load(null, password.toCharArray());
+            try (OutputStream os = Files.newOutputStream(tempJks)) {
+                source.store(os, password.toCharArray());
+            }
+
+            KeyStoreType kst = new KeyStoreType();
+            kst.setType("JKS");
+            kst.setPassword(password);
+            kst.setUrl(tempJks.toUri().toString());
+
+            CertStoreType fileCst = new CertStoreType();
+            fileCst.setFile(tempCerts.toString());
+            CertStoreType urlCst = new CertStoreType();
+            urlCst.setUrl(tempCerts.toUri().toString());
+
+            long before = openFileDescriptors();
+            for (int i = 0; i < 50; i++) {
+                TLSParameterJaxBUtils.getKeyStore(kst, false);
+                TLSParameterJaxBUtils.getKeyStore(fileCst);
+                TLSParameterJaxBUtils.getKeyStore(urlCst);
+            }
+            long leaked = openFileDescriptors() - before;
+            assertTrue("Leaked " + leaked + " file descriptors", leaked < 20);
+        } finally {
+            Files.deleteIfExists(tempJks);
+            Files.deleteIfExists(tempCerts);
+        }
+    }
+
 }

Reply via email to