This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new de91ad3bb61b CAMEL-27263: camel-ftp - clear errors and optional
passwords for the FTPS key store and trust store options (#30457)
de91ad3bb61b is described below
commit de91ad3bb61b670885e31c990f00ae445c0a1542
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Nov 20 08:25:09 2026 +0100
CAMEL-27263: camel-ftp - clear errors and optional passwords for the FTPS
key store and trust store options (#30457)
The ftpClient.keyStore.* and ftpClient.trustStore.* options read the file
and called toCharArray() on the passwords whenever any of them was set, so
an option without file, a trust store without password and a key store
without keyPassword failed with a NullPointerException.
Without file the endpoint now fails with an IllegalArgumentException that
names the option. Without password the store is loaded without one, and a
trust store that then has no certificates fails with a message to set the
password. Without keyPassword the password of the key store is used.
The FTPS stores are loaded with try-with-resources.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../camel/component/file/remote/FtpsEndpoint.java | 64 +++++++++++++++-------
.../file/remote/FtpsEndpointTrustManagerTest.java | 57 +++++++++++++++++++
2 files changed, 102 insertions(+), 19 deletions(-)
diff --git
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsEndpoint.java
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsEndpoint.java
index 19af10e7e110..343d4aa8afea 100644
---
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsEndpoint.java
+++
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsEndpoint.java
@@ -37,7 +37,6 @@ import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam;
import org.apache.camel.support.jsse.SSLContextParameters;
-import org.apache.camel.util.IOHelper;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
@@ -140,22 +139,17 @@ public class FtpsEndpoint extends FtpEndpoint<FTPFile> {
if (ftpClientKeyStoreParameters != null) {
String type = (ftpClientKeyStoreParameters.containsKey("type"))
? (String) ftpClientKeyStoreParameters.get("type") :
KeyStore.getDefaultType();
- String file = (String) ftpClientKeyStoreParameters.get("file");
+ String file = storeFile(ftpClientKeyStoreParameters,
"ftpClient.keyStore.");
String password = (String)
ftpClientKeyStoreParameters.get("password");
String algorithm =
(ftpClientKeyStoreParameters.containsKey("algorithm"))
? (String)
ftpClientKeyStoreParameters.get("algorithm") :
KeyManagerFactory.getDefaultAlgorithm();
- String keyPassword = (String)
ftpClientKeyStoreParameters.get("keyPassword");
-
- KeyStore keyStore = KeyStore.getInstance(type);
- FileInputStream keyStoreFileInputStream = new
FileInputStream(new File(file));
- try {
- keyStore.load(keyStoreFileInputStream,
password.toCharArray());
- } finally {
- IOHelper.close(keyStoreFileInputStream, "keyStore", LOG);
- }
+ // the key is often protected with the password of the key
store
+ String keyPassword = (String)
ftpClientKeyStoreParameters.getOrDefault("keyPassword", password);
+
+ KeyStore keyStore = loadStore(type, file, password);
KeyManagerFactory keyMgrFactory =
KeyManagerFactory.getInstance(algorithm);
- keyMgrFactory.init(keyStore, keyPassword.toCharArray());
+ keyMgrFactory.init(keyStore, toChars(keyPassword));
client.setNeedClientAuth(true);
client.setKeyManager(keyMgrFactory.getKeyManagers()[0]);
}
@@ -163,17 +157,21 @@ public class FtpsEndpoint extends FtpEndpoint<FTPFile> {
if (ftpClientTrustStoreParameters != null) {
String type =
(ftpClientTrustStoreParameters.containsKey("type"))
? (String) ftpClientTrustStoreParameters.get("type") :
KeyStore.getDefaultType();
- String file = (String)
ftpClientTrustStoreParameters.get("file");
+ String file = storeFile(ftpClientTrustStoreParameters,
"ftpClient.trustStore.");
String password = (String)
ftpClientTrustStoreParameters.get("password");
String algorithm =
(ftpClientTrustStoreParameters.containsKey("algorithm"))
? (String)
ftpClientTrustStoreParameters.get("algorithm") :
TrustManagerFactory.getDefaultAlgorithm();
- KeyStore trustStore = KeyStore.getInstance(type);
- FileInputStream trustStoreFileInputStream = new
FileInputStream(new File(file));
- try {
- trustStore.load(trustStoreFileInputStream,
password.toCharArray());
- } finally {
- IOHelper.close(trustStoreFileInputStream, "trustStore",
LOG);
+ KeyStore trustStore = loadStore(type, file, password);
+ if (trustStore.size() == 0) {
+ // without the password, the certificates of a store that
protects them (such as a PKCS12 file
+ // made by keytool) cannot be read, and the client would
trust no server at all
+ throw new IllegalArgumentException(
+ "The trust store " + file + " has no certificates"
+ + (password == null
+ ? ": set
ftpClient.trustStore.password if the store needs"
+ + " a
password to read them"
+ : ""));
}
TrustManagerFactory trustMgrFactory =
TrustManagerFactory.getInstance(algorithm);
@@ -191,6 +189,34 @@ public class FtpsEndpoint extends FtpEndpoint<FTPFile> {
return client;
}
+ /**
+ * The file option of the key store or trust store options, which the
other options need.
+ */
+ private static String storeFile(Map<String, Object> parameters, String
prefix) {
+ Object file = parameters.get("file");
+ if (file == null) {
+ throw new IllegalArgumentException(
+ prefix + "file must be set when other " + prefix + "*
options are set: " + parameters.keySet());
+ }
+ return file.toString();
+ }
+
+ /**
+ * Loads a key store or trust store file. Without a password the store is
loaded without one, as a store without a
+ * password, or a JKS trust store, can be read.
+ */
+ private static KeyStore loadStore(String type, String file, String
password) throws Exception {
+ KeyStore store = KeyStore.getInstance(type);
+ try (FileInputStream is = new FileInputStream(new File(file))) {
+ store.load(is, toChars(password));
+ }
+ return store;
+ }
+
+ private static char[] toChars(String password) {
+ return password != null ? password.toCharArray() : null;
+ }
+
@Override
public RemoteFileOperations<FTPFile> createRemoteFileOperations() throws
Exception {
// configure ftp client
diff --git
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpsEndpointTrustManagerTest.java
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpsEndpointTrustManagerTest.java
index 289be07995b0..4c112d62b59a 100644
---
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpsEndpointTrustManagerTest.java
+++
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpsEndpointTrustManagerTest.java
@@ -17,7 +17,9 @@
package org.apache.camel.component.file.remote;
import java.io.InputStream;
+import java.io.OutputStream;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.cert.CertificateException;
@@ -30,12 +32,14 @@ import org.apache.camel.test.junit6.CamelTestSupport;
import org.apache.commons.net.ftp.FTPSClient;
import org.apache.commons.net.util.TrustManagerUtils;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* The trust manager of the FTPS client that the endpoint creates. The
certificate of the test server is self-signed and
@@ -81,6 +85,59 @@ public class FtpsEndpointTrustManagerTest extends
CamelTestSupport {
assertDoesNotThrow(() -> trustManager.checkServerTrusted(chain,
"RSA"));
}
+ @Test
+ public void trustStoreWithoutPasswordIsLoadedWithoutOne() throws Exception
{
+ // a JKS store can be read without its password
+ FtpsEndpoint endpoint = context.getEndpoint(
+
"ftps://localhost/dir?ftpClient.trustStore.file=./src/test/resources/server.jks&ftpClient.trustStore.type=JKS",
+ FtpsEndpoint.class);
+ X509TrustManager trustManager
+ = assertInstanceOf(X509TrustManager.class, ((FTPSClient)
endpoint.createFtpClient()).getTrustManager());
+ assertDoesNotThrow(() ->
trustManager.checkServerTrusted(serverCertificateChain(), "RSA"));
+ }
+
+ @Test
+ public void trustStoreOptionsWithoutFileFailWithAClearMessage() {
+ FtpsEndpoint endpoint = context.getEndpoint(
+
"ftps://localhost/dir?ftpClient.trustStore.type=JKS&ftpClient.trustStore.password=password",
+ FtpsEndpoint.class);
+ IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, endpoint::createFtpClient);
+ assertTrue(e.getMessage().startsWith("ftpClient.trustStore.file must
be set"), e.getMessage());
+ }
+
+ @Test
+ public void keyStoreOptionsWithoutFileFailWithAClearMessage() {
+ FtpsEndpoint endpoint = context.getEndpoint(
+ "ftps://localhost/dir?ftpClient.keyStore.password=password",
FtpsEndpoint.class);
+ IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, endpoint::createFtpClient);
+ assertTrue(e.getMessage().startsWith("ftpClient.keyStore.file must be
set"), e.getMessage());
+ }
+
+ @Test
+ public void keyStoreWithoutKeyPasswordUsesTheStorePassword() {
+ FtpsEndpoint endpoint = context.getEndpoint(
+
"ftps://localhost/dir?ftpClient.keyStore.file=./src/test/resources/server.jks&ftpClient.keyStore.type=JKS"
+ +
"&ftpClient.keyStore.password=password",
+ FtpsEndpoint.class);
+ assertDoesNotThrow(endpoint::createFtpClient);
+ }
+
+ @Test
+ public void
trustStoreWhoseCertificatesNeedThePasswordFailsWithAClearMessage(@TempDir Path
dir) throws Exception {
+ // a PKCS12 trust store protects its certificates with the password,
so without it none can be read
+ KeyStore pkcs12 = KeyStore.getInstance("PKCS12");
+ pkcs12.load(null, null);
+ pkcs12.setCertificateEntry("ftpserver", serverCertificateChain()[0]);
+ Path file = dir.resolve("trust.p12");
+ try (OutputStream os = Files.newOutputStream(file)) {
+ pkcs12.store(os, "secret".toCharArray());
+ }
+ FtpsEndpoint endpoint = context.getEndpoint(
+
"ftps://localhost/dir?ftpClient.trustStore.type=PKCS12&ftpClient.trustStore.file="
+ file, FtpsEndpoint.class);
+ IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, endpoint::createFtpClient);
+ assertTrue(e.getMessage().contains("set
ftpClient.trustStore.password"), e.getMessage());
+ }
+
@Test
public void customFtpClientKeepsItsTrustManager() throws Exception {
X509TrustManager before = assertInstanceOf(X509TrustManager.class,
customFtpClient.getTrustManager());