You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2026/09/17 17:37:18 UTC
(logging-flume) branch trunk updated: Close the keystore file in `SslContextAwareAbstractSource` (#506)
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/logging-flume.git
The following commit(s) were added to refs/heads/trunk by this push:
new 9b5f6ba1 Close the keystore file in `SslContextAwareAbstractSource` (#506)
9b5f6ba1 is described below
commit 9b5f6ba1e66daf34809f549d29b7801c3c19bbe1
Author: Piotr P. Karwasz <pk...@apache.org>
AuthorDate: Thu Sep 17 19:37:12 2026 +0200
Close the keystore file in `SslContextAwareAbstractSource` (#506)
* Close the keystore file in `SslContextAwareAbstractSource`
The keystore was loaded through a `FileInputStream` that was never
closed, both when validating the configuration and each time an
`SSLContext` was created. The leaked handle keeps the keystore locked
on Windows until the stream is garbage collected, which makes tests that
place the keystore in a JUnit `@TempDir` fail during cleanup.
Assisted-By: Claude Fable 5.1 <no...@anthropic.com>
* Link the changelog entry to the pull request
Assisted-By: Claude Fable 5.1 <no...@anthropic.com>
* Add the pull request link to the changelog entry
Assisted-By: Claude Fable 5.1 <no...@anthropic.com>
---
.../source/SslContextAwareAbstractSource.java | 9 +-
.../source/TestSslContextAwareAbstractSource.java | 124 +++++++++++++++++++++
.../.2.x.x/fix_ssl_keystore_stream_leak.xml | 8 ++
3 files changed, 138 insertions(+), 3 deletions(-)
diff --git a/flume-ng-core/src/main/java/org/apache/flume/source/SslContextAwareAbstractSource.java b/flume-ng-core/src/main/java/org/apache/flume/source/SslContextAwareAbstractSource.java
index 26cca6ff..d5c97623 100644
--- a/flume-ng-core/src/main/java/org/apache/flume/source/SslContextAwareAbstractSource.java
+++ b/flume-ng-core/src/main/java/org/apache/flume/source/SslContextAwareAbstractSource.java
@@ -17,6 +17,7 @@
package org.apache.flume.source;
import java.io.FileInputStream;
+import java.io.InputStream;
import java.security.KeyStore;
import java.util.Arrays;
import java.util.LinkedHashSet;
@@ -104,9 +105,9 @@ public abstract class SslContextAwareAbstractSource extends AbstractSource {
if (sslEnabled) {
Objects.requireNonNull(keystore, KEYSTORE_KEY + " must be specified when SSL is enabled");
Objects.requireNonNull(keystorePassword, KEYSTORE_PASSWORD_KEY + " must be specified when SSL is enabled");
- try {
+ try (InputStream in = new FileInputStream(keystore)) {
KeyStore ks = KeyStore.getInstance(keystoreType);
- ks.load(new FileInputStream(keystore), keystorePassword.toCharArray());
+ ks.load(in, keystorePassword.toCharArray());
} catch (Exception ex) {
throw new FlumeException("Source " + getName() + " configured with invalid keystore: " + keystore, ex);
}
@@ -117,7 +118,9 @@ public abstract class SslContextAwareAbstractSource extends AbstractSource {
if (sslEnabled) {
try {
KeyStore ks = KeyStore.getInstance(keystoreType);
- ks.load(new FileInputStream(keystore), keystorePassword.toCharArray());
+ try (InputStream in = new FileInputStream(keystore)) {
+ ks.load(in, keystorePassword.toCharArray());
+ }
// can be set with "ssl.KeyManagerFactory.algorithm"
String algorithm = KeyManagerFactory.getDefaultAlgorithm();
diff --git a/flume-ng-core/src/test/java/org/apache/flume/source/TestSslContextAwareAbstractSource.java b/flume-ng-core/src/test/java/org/apache/flume/source/TestSslContextAwareAbstractSource.java
new file mode 100644
index 00000000..81570401
--- /dev/null
+++ b/flume-ng-core/src/test/java/org/apache/flume/source/TestSslContextAwareAbstractSource.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.flume.source;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.flume.Context;
+import org.apache.flume.FlumeException;
+import org.apache.flume.sdk.test.TestKeyStores;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class TestSslContextAwareAbstractSource {
+
+ private static final String KEYSTORE_PASSWORD = "password";
+
+ @Rule
+ public final TemporaryFolder tempFolder = new TemporaryFolder();
+
+ private Path keystore;
+
+ /** Minimal concrete source exposing the SSL configuration of the abstract class. */
+ private static final class TestSource extends SslContextAwareAbstractSource {
+ void configure(Context context) {
+ configureSsl(context);
+ }
+ }
+
+ @Before
+ public void writeKeystore() throws Exception {
+ keystore = TestKeyStores.selfSigned("CN=localhost")
+ .writeKeyStore(tempFolder.newFile("keystore.jks").toPath(), "JKS", KEYSTORE_PASSWORD);
+ }
+
+ private Context sslContext(String password) {
+ Context context = new Context();
+ context.put("ssl", "true");
+ context.put("keystore", keystore.toString());
+ context.put("keystore-password", password);
+ return context;
+ }
+
+ private TestSource configuredSource() {
+ TestSource source = new TestSource();
+ source.configure(sslContext(KEYSTORE_PASSWORD));
+ return source;
+ }
+
+ @Test
+ public void sslContextIsCreatedFromKeystore() {
+ TestSource source = configuredSource();
+ assertTrue(source.isSslEnabled());
+ assertTrue(source.getSslContextSupplier().get().isPresent());
+ assertTrue(source.getSslEngineSupplier(false).get().isPresent());
+ }
+
+ @Test
+ public void noSslContextWhenSslIsDisabled() {
+ TestSource source = new TestSource();
+ source.configure(new Context());
+ assertFalse(source.isSslEnabled());
+ assertFalse(source.getSslContextSupplier().get().isPresent());
+ }
+
+ @Test(expected = FlumeException.class)
+ public void wrongKeystorePasswordIsRejected() {
+ new TestSource().configure(sslContext("wrong"));
+ }
+
+ @Test
+ public void keystoreIsDeletableAfterUse() {
+ // The source must not keep the keystore open: on Windows an open file cannot be deleted
+ assertTrue(configuredSource().getSslContextSupplier().get().isPresent());
+ assertTrue("The keystore is still open", keystore.toFile().delete());
+ }
+
+ @Test
+ public void keystoreDescriptorIsReleased() throws IOException {
+ // On Linux the open file descriptors of the process are listed under `/proc/self/fd`
+ Path fdDir = Paths.get("/proc/self/fd");
+ assumeTrue("Not running on Linux", Files.isDirectory(fdDir));
+ assertTrue(configuredSource().getSslContextSupplier().get().isPresent());
+ String keystorePath = keystore.toRealPath().toString();
+ try (Stream<Path> fds = Files.list(fdDir)) {
+ List<Path> open =
+ fds.filter(fd -> keystorePath.equals(readLink(fd))).collect(Collectors.toList());
+ assertTrue("File descriptors still open on the keystore: " + open, open.isEmpty());
+ }
+ }
+
+ private static String readLink(Path fd) {
+ try {
+ return Files.readSymbolicLink(fd).toString();
+ } catch (IOException e) {
+ // The descriptor was closed between the listing and the read
+ return null;
+ }
+ }
+}
diff --git a/src/changelog/.2.x.x/fix_ssl_keystore_stream_leak.xml b/src/changelog/.2.x.x/fix_ssl_keystore_stream_leak.xml
new file mode 100644
index 00000000..6263b991
--- /dev/null
+++ b/src/changelog/.2.x.x/fix_ssl_keystore_stream_leak.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="https://logging.apache.org/xml/ns"
+ xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+ type="fixed">
+ <issue id="506" link="https://github.com/apache/logging-flume/pull/506"/>
+ <description format="asciidoc">Fix keystore file descriptor leak in `SslContextAwareAbstractSource`.</description>
+</entry>