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 c770e795acf0 CAMEL-27019: camel-sftp - the password is sent once per 
connection, a rejected login is not retried, and the WARN gives the reason 
(#30167)
c770e795acf0 is described below

commit c770e795acf092298c33f249ce299276d3f22286
Author: Federico Mariani <[email protected]>
AuthorDate: Mon Nov 16 16:08:38 2026 +0100

    CAMEL-27019: camel-sftp - the password is sent once per connection, a 
rejected login is not retried, and the WARN gives the reason (#30167)
    
    - With a wrong password, the sftp consumer sent the password up to 18 times 
per poll: JSch asks for it again after each rejection, up to its `MaxAuthTries` 
(6) per connection, Camel's `UserInfo` gave it each time, and 
`SftpOperations.connect` opened up to `maximumReconnectAttempts` (3) 
connections also after a rejected login. With `autoCreate=true` the start-up 
connect added 18 more. On a server with a lockout policy this locks the account.
    - `SftpOperations`: the `UserInfo` gives the password once per session, 
with the `password` or the `keyboard-interactive` method. When the server asks 
again, it throws, so JSch ends the session (`Auth fail for user '...': the 
server did not accept the password`). A keyboard-interactive request without 
prompts does not count as a password answer.
    - `SftpOperations.tryConnect`: a rejected login (that exception, or JSch's 
`Auth fail ...` / `Auth cancel ...`) ends the reconnect attempts. Other 
connection failures are still retried `maximumReconnectAttempts` times.
    - `RemoteFileConsumer`: the `Cannot connect/login to: ...` WARN now gives 
the reason (`due to: <message of the cause>`), on one line, as an ftp reply of 
several lines holds line breaks.
    
    _Claude Code on behalf of Federico Mariani_
    
    Co-Authored-By: Claude <[email protected]>
---
 .../apache/camel/catalog/docs/sftp-component.adoc  |  16 +-
 .../component/file/remote/RemoteFileConsumer.java  |  39 +++-
 .../camel-ftp/src/main/docs/sftp-component.adoc    |  16 +-
 .../component/file/remote/SftpOperations.java      |  56 ++++-
 .../remote/RemoteFileConsumerSkipPollWarnTest.java | 111 +++++++++
 .../file/remote/RemoteFileConsumerWarnCapture.java |  67 ++++++
 .../integration/SftpConsumerWrongPasswordIT.java   | 259 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |  17 ++
 8 files changed, 563 insertions(+), 18 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/sftp-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/sftp-component.adoc
index 5403f09654d1..b6164adac772 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/sftp-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/sftp-component.adoc
@@ -406,7 +406,8 @@ ssh -vvv <server_address>
 === Connection errors
 
 When the connection fails, the exception is a 
`GenericFileOperationFailedException` with the message
-`Cannot connect to sftp://user@host:port`. The message of its cause, from 
JSch, tells what failed:
+`Cannot connect to sftp://user@host:port`. The message of its cause, from 
JSch, tells what failed, and the consumer
+logs it in its WARN `Cannot connect/login to: sftp://user@host:port due to: 
...`:
 
 [cols="2,3"]
 |===
@@ -422,7 +423,7 @@ than 22, the entry starts with `[host]:port`.
 `server_host_key`, `keyExchangeProtocols` for `kex`, or `ciphers` for 
`cipher.c2s` and `cipher.s2c`. See
 <<_restoring_deprecated_key_types_and_algorithms,Restoring Deprecated Key 
Types and Algorithms>>.
 
-|`Auth fail for methods '...'`
+|`Auth fail for methods '...'`, `Auth fail for user '...': the server did not 
accept the password`
 |The server refused the credentials: check the username, password or key. With 
an RSA key and a server that only
 accepts `ssh-rsa` signatures, add `ssh-rsa` to `publicKeyAcceptedAlgorithms`.
 
@@ -434,6 +435,17 @@ accepts `ssh-rsa` signatures, add `ssh-rsa` to 
`publicKeyAcceptedAlgorithms`.
 `RAW(...)`, see 
xref:manual::endpoint.adoc#_configuring_parameter_values_using_raw_values_such_as_passwords[raw
 values].
 |===
 
+A login that the server rejects is not retried, as each rejected login counts 
towards the lockout policy of a server,
+such as one that locks the account after a few failures. Camel gives the 
password once per connection, with the
+`password` or the `keyboard-interactive` method: when the server asks for it 
again, the connection ends with
+`Auth fail for user '...': the server did not accept the password`. So a 
server that asks for the password twice in
+one login, such as one that requires both the `password` and the 
`keyboard-interactive` methods
+(`AuthenticationMethods password,keyboard-interactive` in OpenSSH), rejects 
the login, with the same message, even when
+the password is right. A rejected login (`Auth fail ...` or `Auth cancel ...`) 
does not connect again for
+`maximumReconnectAttempts`, which applies to the other connection failures. 
The consumer tries again on its next
+poll, so a wrong password is sent once per poll (and once more when the 
consumer starts with `autoCreate=true`), and
+the producer tries again on its next message.
+
 === Symbolic links and the starting directory
 
 With `jailStartingDirectory` enabled (the default), the consumer resolves a 
listed file that the server reports as a
diff --git 
a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
 
b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
index 6b3134e8e799..2b43cf3e80a5 100644
--- 
a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
+++ 
b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.file.remote;
 
 import java.io.IOException;
 import java.util.List;
+import java.util.stream.Collectors;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePropertyKey;
@@ -99,20 +100,22 @@ public abstract class RemoteFileConsumer<T> extends 
GenericFileConsumer<T> {
         if (LOG.isTraceEnabled()) {
             LOG.trace("prePollCheck on {}", 
getEndpoint().getConfiguration().remoteServerInformation());
         }
-        Exception cause;
+        Exception cause = null;
+        boolean connected = false;
+        boolean autoCreateFailed = false;
         try {
             connectIfNecessary();
             loggedIn = true;
+            connected = true;
             if (!autoCreatedDone) {
                 autoCreateIfNecessary();
                 autoCreatedDone = true;
             }
         } catch (Exception e) {
             cause = e;
-            String msg = "Cannot connect/login to: " + remoteServer();
-            if (loggedIn && !autoCreatedDone) {
-                msg = "Cannot auto-create starting directory at: " + 
remoteServer();
-            }
+            // connected, so the starting directory could not be created
+            autoCreateFailed = connected;
+            String msg = autoCreateFailed ? cannotAutoCreate() : "Cannot 
connect/login to: " + remoteServer();
             LOG.debug(msg, cause);
             loggedIn = false;
             if 
(getEndpoint().getConfiguration().isThrowExceptionOnConnectFailed()) {
@@ -120,8 +123,8 @@ public abstract class RemoteFileConsumer<T> extends 
GenericFileConsumer<T> {
             }
         }
 
-        if (loggedIn && !autoCreatedDone) {
-            String message = "Cannot auto-create starting directory at: " + 
remoteServer() + ". Will skip this poll.";
+        if (autoCreateFailed) {
+            String message = cannotAutoCreate() + reason(cause) + ". Will skip 
this poll.";
             if (!autoCreateWarning) {
                 LOG.warn(message);
                 autoCreateWarning = true;
@@ -129,7 +132,7 @@ public abstract class RemoteFileConsumer<T> extends 
GenericFileConsumer<T> {
             return false;
         }
         if (!loggedIn) {
-            String message = "Cannot connect/login to: " + remoteServer() + ". 
Will skip this poll.";
+            String message = "Cannot connect/login to: " + remoteServer() + 
reason(cause) + ". Will skip this poll.";
             if (!loggedInWarning) {
                 LOG.warn(message);
                 loggedInWarning = true;
@@ -146,6 +149,26 @@ public abstract class RemoteFileConsumer<T> extends 
GenericFileConsumer<T> {
         return true;
     }
 
+    private String cannotAutoCreate() {
+        return "Cannot auto-create starting directory " + 
endpoint.getConfiguration().getDirectory() + " at: "
+               + remoteServer();
+    }
+
+    /**
+     * Why the poll is skipped, for the WARN: the message of the cause when 
there is one, as the sftp operations wrap
+     * the error of the SSH client in an exception whose message only names 
the server. On one line, as the message can
+     * hold the reply of the server, such as an ftp reply of several lines.
+     */
+    private static String reason(Exception cause) {
+        if (cause == null) {
+            return "";
+        }
+        Throwable reason = cause.getCause() != null ? cause.getCause() : cause;
+        String message = reason.getMessage() != null ? reason.getMessage() : 
reason.getClass().getName();
+        return " due to: "
+               + message.lines().map(String::strip).filter(line -> 
!line.isEmpty()).collect(Collectors.joining(" "));
+    }
+
     @Override
     protected void postPollCheck(int polledMessages) {
         if (LOG.isTraceEnabled()) {
diff --git a/components/camel-ftp/src/main/docs/sftp-component.adoc 
b/components/camel-ftp/src/main/docs/sftp-component.adoc
index 5403f09654d1..b6164adac772 100644
--- a/components/camel-ftp/src/main/docs/sftp-component.adoc
+++ b/components/camel-ftp/src/main/docs/sftp-component.adoc
@@ -406,7 +406,8 @@ ssh -vvv <server_address>
 === Connection errors
 
 When the connection fails, the exception is a 
`GenericFileOperationFailedException` with the message
-`Cannot connect to sftp://user@host:port`. The message of its cause, from 
JSch, tells what failed:
+`Cannot connect to sftp://user@host:port`. The message of its cause, from 
JSch, tells what failed, and the consumer
+logs it in its WARN `Cannot connect/login to: sftp://user@host:port due to: 
...`:
 
 [cols="2,3"]
 |===
@@ -422,7 +423,7 @@ than 22, the entry starts with `[host]:port`.
 `server_host_key`, `keyExchangeProtocols` for `kex`, or `ciphers` for 
`cipher.c2s` and `cipher.s2c`. See
 <<_restoring_deprecated_key_types_and_algorithms,Restoring Deprecated Key 
Types and Algorithms>>.
 
-|`Auth fail for methods '...'`
+|`Auth fail for methods '...'`, `Auth fail for user '...': the server did not 
accept the password`
 |The server refused the credentials: check the username, password or key. With 
an RSA key and a server that only
 accepts `ssh-rsa` signatures, add `ssh-rsa` to `publicKeyAcceptedAlgorithms`.
 
@@ -434,6 +435,17 @@ accepts `ssh-rsa` signatures, add `ssh-rsa` to 
`publicKeyAcceptedAlgorithms`.
 `RAW(...)`, see 
xref:manual::endpoint.adoc#_configuring_parameter_values_using_raw_values_such_as_passwords[raw
 values].
 |===
 
+A login that the server rejects is not retried, as each rejected login counts 
towards the lockout policy of a server,
+such as one that locks the account after a few failures. Camel gives the 
password once per connection, with the
+`password` or the `keyboard-interactive` method: when the server asks for it 
again, the connection ends with
+`Auth fail for user '...': the server did not accept the password`. So a 
server that asks for the password twice in
+one login, such as one that requires both the `password` and the 
`keyboard-interactive` methods
+(`AuthenticationMethods password,keyboard-interactive` in OpenSSH), rejects 
the login, with the same message, even when
+the password is right. A rejected login (`Auth fail ...` or `Auth cancel ...`) 
does not connect again for
+`maximumReconnectAttempts`, which applies to the other connection failures. 
The consumer tries again on its next
+poll, so a wrong password is sent once per poll (and once more when the 
consumer starts with `autoCreate=true`), and
+the producer tries again on its next message.
+
 === Symbolic links and the starting directory
 
 With `jailStartingDirectory` enabled (the default), the consumer resolves a 
listed file that the server reports as a
diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
index 8b2b5a012011..e3ef97455f1e 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
@@ -105,12 +105,27 @@ public class SftpOperations
     private static class TaskPayload {
         final RemoteFileConfiguration configuration;
         private Exception exception;
+        // the server rejected the login, which ends the reconnect attempts
+        private boolean authenticationFailed;
 
         public TaskPayload(RemoteFileConfiguration configuration) {
             this.configuration = configuration;
         }
     }
 
+    /**
+     * Ends the session when the server asks for the password again: JSch asks 
for it again each time the server rejects
+     * it, up to MaxAuthTries times per session, and each rejected password 
counts towards the lockout policy of the
+     * server.
+     */
+    private static final class PasswordNotAcceptedException extends 
RuntimeException {
+        private static final long serialVersionUID = 1L;
+
+        PasswordNotAcceptedException(String username) {
+            super("Auth fail for user '" + username + "': the server did not 
accept the password");
+        }
+    }
+
     public SftpOperations() {
     }
 
@@ -160,7 +175,7 @@ public class SftpOperations
 
                 TaskPayload payload = new TaskPayload(configuration);
 
-                if (!task.run(endpoint.getCamelContext(), this::tryConnect, 
payload)) {
+                if (!task.run(endpoint.getCamelContext(), this::tryConnect, 
payload) || payload.authenticationFailed) {
                     throw new GenericFileOperationFailedException(
                             "Cannot connect to " + 
configuration.remoteServerInformation(),
                             payload.exception);
@@ -178,6 +193,12 @@ public class SftpOperations
         }
     }
 
+    /**
+     * One attempt of the reconnect task.
+     *
+     * @return whether the task is done: connected, or the server rejected the 
login, which is not retried as each
+     *         rejected login counts towards the lockout policy of the server
+     */
     private boolean tryConnect(TaskPayload payload) {
         if (LOG.isTraceEnabled()) {
             LOG.trace("Reconnect attempt to {}", 
payload.configuration.remoteServerInformation());
@@ -219,15 +240,23 @@ public class SftpOperations
                     LOG.debug("Connected to {}", 
payload.configuration.remoteServerInformation());
                 }
             }
-        } catch (JSchException e) {
+        } catch (JSchException | PasswordNotAcceptedException e) {
             payload.exception = e;
+            payload.authenticationFailed = isAuthenticationFailure(e);
 
-            return false;
+            return payload.authenticationFailed;
         }
 
         return true;
     }
 
+    private static boolean isAuthenticationFailure(Exception e) {
+        // JSch fails with "Auth fail for methods ..." or "Auth cancel for 
methods ..." when no method authenticated
+        String message = e.getMessage();
+        return e instanceof PasswordNotAcceptedException
+                || message != null && (message.startsWith("Auth fail") || 
message.startsWith("Auth cancel"));
+    }
+
     private void configureBulkRequests() {
         try {
             tryConfigureBulkRequests();
@@ -427,16 +456,21 @@ public class SftpOperations
 
             private final CamelLogger messageLogger
                     = new CamelLogger(LOG, ((SftpConfiguration) 
configuration).getServerMessageLoggingLevel());
+            // the password is given once per session, with the password or 
the keyboard-interactive method
+            private boolean passwordGiven;
 
             public String getPassphrase() {
                 return null;
             }
 
             public String getPassword() {
-                return configuration.getPassword();
+                String password = configuration.getPassword();
+                passwordGiven |= password != null;
+                return password;
             }
 
             public boolean promptPassword(String s) {
+                checkPasswordNotGiven();
                 return true;
             }
 
@@ -464,14 +498,24 @@ public class SftpOperations
 
             public String[] promptKeyboardInteractive(
                     String destination, String name, String instruction, 
String[] prompt, boolean[] echo) {
-                // must return an empty array if password is null
-                if (configuration.getPassword() == null) {
+                // must return an empty array if password is null, or if the 
server asks nothing (only an instruction)
+                if (configuration.getPassword() == null || prompt.length == 0) 
{
                     return new String[0];
                 } else {
+                    checkPasswordNotGiven();
+                    passwordGiven = true;
                     return new String[] { configuration.getPassword() };
                 }
             }
 
+            private void checkPasswordNotGiven() {
+                if (passwordGiven) {
+                    // asked again: the server did not accept the password, so 
end the session instead of sending it
+                    // once more (JSch passes the exception on from 
Session.connect, after it disconnected)
+                    throw new 
PasswordNotAcceptedException(configuration.getUsername());
+                }
+            }
+
         });
 
         // set the SO_TIMEOUT for the time after the connect phase
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileConsumerSkipPollWarnTest.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileConsumerSkipPollWarnTest.java
new file mode 100644
index 000000000000..5d576e09034a
--- /dev/null
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileConsumerSkipPollWarnTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.camel.component.file.remote;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.apache.camel.component.file.GenericFileProcessStrategy;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.commons.net.ftp.FTPFile;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+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.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * The WARN of the remote file consumer when it skips a poll: it gives the 
reason on one line, and tells a starting
+ * directory that cannot be created apart from a server that cannot be 
connected to.
+ */
+class RemoteFileConsumerSkipPollWarnTest extends CamelTestSupport {
+
+    private final RemoteFileConsumerWarnCapture warnings = new 
RemoteFileConsumerWarnCapture();
+    private RemoteFileOperations<?> operations;
+
+    @BeforeEach
+    void captureWarnings() {
+        warnings.attach();
+    }
+
+    @AfterEach
+    void releaseWarnings() {
+        warnings.detach();
+    }
+
+    @SuppressWarnings("unchecked")
+    private RemoteFileConsumer<?> createConsumer(String uri) {
+        RemoteFileEndpoint<?> endpoint = context.getEndpoint(uri, 
RemoteFileEndpoint.class);
+        if (endpoint instanceof SftpEndpoint sftpEndpoint) {
+            RemoteFileOperations<SftpRemoteFile> sftpOperations = 
mock(RemoteFileOperations.class);
+            operations = sftpOperations;
+            return new SftpConsumer(sftpEndpoint, exchange -> {
+            }, sftpOperations, mock(GenericFileProcessStrategy.class));
+        }
+        FtpOperations ftpOperations = mock(FtpOperations.class);
+        operations = ftpOperations;
+        return new FtpConsumer((RemoteFileEndpoint<FTPFile>) endpoint, 
exchange -> {
+        }, ftpOperations, mock(GenericFileProcessStrategy.class));
+    }
+
+    @Test
+    void loginFailureWithReplyOfSeveralLinesIsLoggedOnOneLine() throws 
Exception {
+        RemoteFileConsumer<?> consumer = 
createConsumer("ftp://admin@localhost/inbox?autoCreate=false";);
+        // commons-net joins the lines of a reply with CRLF
+        when(operations.connect(any(), any())).thenThrow(new 
GenericFileOperationFailedException(
+                530, "530-Login incorrect.\r\n530 The account is locked after 
3 failures"));
+
+        assertFalse(consumer.prePollCheck());
+
+        assertEquals(List.of("Cannot connect/login to: 
ftp://admin@localhost:21 due to: File operation failed:"
+                             + " 530-Login incorrect. 530 The account is 
locked after 3 failures. Code: 530."
+                             + " Will skip this poll."),
+                warnings.messages());
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = { "ftp", "sftp" })
+    void autoCreateFailureIsLoggedWithItsReason(String scheme) throws 
Exception {
+        RemoteFileConsumer<?> consumer = createConsumer(scheme + 
"://admin@localhost:2121/inbox");
+        when(operations.connect(any(), any())).thenReturn(true);
+        when(operations.buildDirectory(anyString(), anyBoolean())).thenThrow(
+                new GenericFileOperationFailedException("Cannot build 
directory: inbox", new IOException("Permission denied")));
+
+        assertFalse(consumer.prePollCheck());
+
+        assertEquals(List.of("Cannot auto-create starting directory inbox at: 
" + scheme + "://admin@localhost:2121"
+                             + " due to: Permission denied. Will skip this 
poll."),
+                warnings.messages());
+
+        // the next poll creates the starting directory, and polls
+        doReturn(true).when(operations).buildDirectory(anyString(), 
anyBoolean());
+
+        assertTrue(consumer.prePollCheck());
+        assertEquals(1, warnings.messages().size());
+    }
+}
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileConsumerWarnCapture.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileConsumerWarnCapture.java
new file mode 100644
index 000000000000..97189d74f305
--- /dev/null
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileConsumerWarnCapture.java
@@ -0,0 +1,67 @@
+/*
+ * 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.camel.component.file.remote;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+
+/**
+ * Captures the WARN messages of the remote file consumer, between {@link 
#attach()} and {@link #detach()}.
+ */
+public class RemoteFileConsumerWarnCapture extends AbstractAppender {
+
+    private final List<String> messages = new CopyOnWriteArrayList<>();
+
+    public RemoteFileConsumerWarnCapture() {
+        super("RemoteFileConsumerWarnCapture", null, null, true, 
Property.EMPTY_ARRAY);
+    }
+
+    public void attach() {
+        start();
+        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+        ctx.getConfiguration().getRootLogger().addAppender(this, Level.WARN, 
null);
+        ctx.updateLoggers();
+    }
+
+    public void detach() {
+        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+        ctx.getConfiguration().getRootLogger().removeAppender(getName());
+        ctx.updateLoggers();
+        stop();
+    }
+
+    /**
+     * The WARN messages logged so far.
+     */
+    public List<String> messages() {
+        return messages;
+    }
+
+    @Override
+    public void append(LogEvent event) {
+        if (RemoteFileConsumer.class.getName().equals(event.getLoggerName())) {
+            messages.add(event.getMessage().getFormattedMessage());
+        }
+    }
+}
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/integration/SftpConsumerWrongPasswordIT.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/integration/SftpConsumerWrongPasswordIT.java
new file mode 100644
index 000000000000..be7c77ee06dc
--- /dev/null
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/integration/SftpConsumerWrongPasswordIT.java
@@ -0,0 +1,259 @@
+/*
+ * 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.camel.component.file.remote.sftp.integration;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.IntSupplier;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Endpoint;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.remote.RemoteFileConsumerWarnCapture;
+import 
org.apache.camel.component.file.remote.RemoteFilePollingConsumerPollStrategy;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.infra.ftp.services.embedded.SftpEmbeddedService;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.sshd.server.auth.UserAuthFactory;
+import org.apache.sshd.server.auth.keyboard.UserAuthKeyboardInteractiveFactory;
+import org.apache.sshd.server.auth.password.UserAuthPasswordFactory;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIf;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * With a wrong password, the sftp consumer sends the password once per poll: 
once per session, whether the server asks
+ * for it with the password or the keyboard-interactive method, and it does 
not open another session when the server
+ * rejected the login, as each rejected login counts towards the lockout 
policy of the server. Other connection failures
+ * are retried as before. The WARN of the consumer says why it cannot connect.
+ */
+@EnabledIf(value = 
"org.apache.camel.test.infra.ftp.services.embedded.SftpUtil#hasRequiredAlgorithms('src/test/resources/hostkey.pem')")
+class SftpConsumerWrongPasswordIT extends CamelTestSupport {
+
+    @RegisterExtension
+    CountingSftpEmbeddedService service = new CountingSftpEmbeddedService();
+
+    private final RemoteFileConsumerWarnCapture warnings = new 
RemoteFileConsumerWarnCapture();
+
+    @BeforeEach
+    void captureWarnings() {
+        warnings.attach();
+    }
+
+    @AfterEach
+    void releaseWarnings() {
+        warnings.detach();
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = { "password", "keyboard-interactive" })
+    void wrongPasswordIsSentOncePerPoll(String method) throws Exception {
+        service.allowOnly(method);
+
+        PollRecorder polls = consume("wrong", "&autoCreate=false", 
service::passwordChecks);
+
+        await().atMost(30, TimeUnit.SECONDS).until(() -> 
polls.countAtPoll.size() >= 2);
+        // the passwords the server checked by the end of the first and the 
second poll
+        assertEquals(List.of(1, 2), polls.countAtPoll.subList(0, 2));
+
+        // one WARN, until the consumer connects, which says that the login 
failed
+        assertEquals(List.of("Cannot connect/login to: 
sftp://admin@localhost:"; + service.getPort()
+                             + " due to: Auth fail for user 'admin': the 
server did not accept the password."
+                             + " Will skip this poll."),
+                warnings.messages());
+    }
+
+    @Test
+    void autoCreateSendsTheWrongPasswordOnceBeforeTheFirstPoll() throws 
Exception {
+        service.allowOnly("password");
+
+        // the consumer connects once when it starts to create the starting 
directory, and again for the first poll
+        PollRecorder polls = consume("wrong", "", service::passwordChecks);
+
+        await().atMost(30, TimeUnit.SECONDS).until(() -> 
!polls.countAtPoll.isEmpty());
+        assertEquals(2, polls.countAtPoll.get(0));
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = { "password", "keyboard-interactive" })
+    void rightPasswordLogsIn(String method) throws Exception {
+        service.allowOnly(method);
+        Path dir = service.getFtpRootDir().resolve("wrongpassword");
+        Files.createDirectories(dir);
+        Files.writeString(dir.resolve("hello.txt"), "Hello World");
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+
+        consume(CountingSftpEmbeddedService.PASSWORD, "", 
service::passwordChecks);
+
+        MockEndpoint.assertIsSatisfied(context);
+        assertEquals(1, service.passwordChecks());
+        assertTrue(warnings.messages().isEmpty(), "No WARN: " + 
warnings.messages());
+    }
+
+    @Test
+    void connectionFailureIsRetried() throws Exception {
+        try (ClosingServer server = new ClosingServer()) {
+            PollRecorder polls = new PollRecorder(server::connections);
+            context.getRegistry().bind("polls", polls);
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() {
+                    from("sftp://localhost:"; + server.getPort() + 
"/wrongpassword?username=admin&password=wrong"
+                         + 
"&useUserKnownHostsFile=false&autoCreate=false&reconnectDelay=10"
+                         + "&initialDelay=0&delay=1000&pollStrategy=#polls")
+                            .to("mock:result");
+                }
+            });
+
+            await().atMost(30, TimeUnit.SECONDS).until(() -> 
polls.countAtPoll.size() >= 2);
+            // the connections the server accepted by the end of the first and 
the second poll: maximumReconnectAttempts
+            // (3) per poll
+            assertEquals(List.of(3, 6), polls.countAtPoll.subList(0, 2));
+
+            assertEquals(List.of("Cannot connect/login to: 
sftp://admin@localhost:"; + server.getPort()
+                                 + " due to: connection is closed by foreign 
host. Will skip this poll."),
+                    warnings.messages());
+        }
+    }
+
+    private PollRecorder consume(String password, String options, IntSupplier 
counter) throws Exception {
+        PollRecorder polls = new PollRecorder(counter);
+        context.getRegistry().bind("polls", polls);
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                
from("sftp://localhost:{{ftp.server.port}}/{{ftp.root.dir}}/wrongpassword?username=admin&password=";
+                     + password + "&knownHostsFile=" + 
service.getKnownHostsFile()
+                     + "&initialDelay=0&delay=1000&pollStrategy=#polls" + 
options)
+                        .to("mock:result");
+            }
+        });
+        return polls;
+    }
+
+    /**
+     * The embedded server, which accepts the password {@link #PASSWORD} only, 
with the authentication methods a test
+     * chooses, and counts the passwords it checks.
+     */
+    static class CountingSftpEmbeddedService extends SftpEmbeddedService {
+
+        static final String PASSWORD = "admin";
+
+        private final AtomicInteger passwordChecks = new AtomicInteger();
+
+        @Override
+        public void setUpServer() throws Exception {
+            super.setUpServer();
+            // keyboard-interactive checks the answer with the same 
authenticator
+            sshd.setPasswordAuthenticator((username, password, session) -> {
+                passwordChecks.incrementAndGet();
+                return PASSWORD.equals(password);
+            });
+        }
+
+        void allowOnly(String method) {
+            UserAuthFactory factory = "password".equals(method)
+                    ? UserAuthPasswordFactory.INSTANCE : 
UserAuthKeyboardInteractiveFactory.INSTANCE;
+            sshd.setUserAuthFactories(List.of(factory));
+        }
+
+        int passwordChecks() {
+            return passwordChecks.get();
+        }
+    }
+
+    /**
+     * Records, at the end of each poll, the value of a counter of the server.
+     */
+    static class PollRecorder extends RemoteFilePollingConsumerPollStrategy {
+
+        final List<Integer> countAtPoll = new CopyOnWriteArrayList<>();
+        private final IntSupplier counter;
+
+        PollRecorder(IntSupplier counter) {
+            this.counter = counter;
+        }
+
+        @Override
+        public void commit(Consumer consumer, Endpoint endpoint, int 
polledMessages) {
+            countAtPoll.add(counter.getAsInt());
+            super.commit(consumer, endpoint, polledMessages);
+        }
+
+        @Override
+        public boolean rollback(Consumer consumer, Endpoint endpoint, int 
retryCounter, Exception e) throws Exception {
+            countAtPoll.add(counter.getAsInt());
+            return super.rollback(consumer, endpoint, retryCounter, e);
+        }
+    }
+
+    /**
+     * A server that closes every connection it accepts, before the SSH 
handshake.
+     */
+    static class ClosingServer implements AutoCloseable {
+
+        private final ServerSocket socket = new ServerSocket(0, 50, 
InetAddress.getLoopbackAddress());
+        private final AtomicInteger connections = new AtomicInteger();
+        private final Thread acceptor = new Thread(this::accept, 
"ClosingServer");
+
+        ClosingServer() throws IOException {
+            acceptor.setDaemon(true);
+            acceptor.start();
+        }
+
+        private void accept() {
+            while (!socket.isClosed()) {
+                try (Socket ignored = socket.accept()) {
+                    connections.incrementAndGet();
+                } catch (IOException e) {
+                    // closed
+                }
+            }
+        }
+
+        int getPort() {
+            return socket.getLocalPort();
+        }
+
+        int connections() {
+            return connections.get();
+        }
+
+        @Override
+        public void close() throws IOException {
+            socket.close();
+        }
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index bb1c272dca26..0b23c1b1d3da 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -422,6 +422,23 @@ Before, the refused listing was taken as an empty 
directory: the polls found no
 450 and 550 (a directory that is missing or empty) still give an empty 
listing. If such a consumer now fails, set
 `passiveMode=true`. See the xref:components::ftp-component.adoc[FTP component].
 
+=== camel-ftp - sftp sends the password once per connection, and does not 
retry a rejected login
+
+The `sftp` component now gives the password to the server once per connection, 
and no longer connects again when the
+server rejected the login: `maximumReconnectAttempts` applies to the other 
connection failures only. Before, a wrong
+password was sent up to 6 times per connection (the `MaxAuthTries` of JSch), 
on up to `maximumReconnectAttempts` (3)
+connections: 18 times per poll of the consumer, and 18 more when the consumer 
started with `autoCreate=true`, which is
+enough to lock the account on a server with a lockout policy. The consumer now 
tries again on its next poll, and the
+producer on its next message. When the server asks for the password again on 
the same connection, the connection
+ends, with the cause `Auth fail for user '<username>': the server did not 
accept the password`.
+
+The WARN that a consumer of the `ftp`, `ftps`, `sftp` and `mina-sftp` 
components logs when it cannot connect,
+`Cannot connect/login to: ... Will skip this poll.`, now gives the reason 
before `Will skip this poll.`, such as
+`due to: Auth fail for methods 'publickey,password'`, on one line. When the 
consumer connects but cannot create its
+starting directory (`autoCreate=true`), it now logs
+`Cannot auto-create starting directory <directory> at: ... due to: ... Will 
skip this poll.` instead.
+See the xref:components::sftp-component.adoc[SFTP component].
+
 === camel-kafka - exactlyOnce: a rejected send that a doCatch catches skips 
the record
 
 With `exactlyOnce`, a Kafka send through the source producer after an earlier 
send failed in the same transaction is

Reply via email to