Skip to content

Commit 775e6fd

Browse files
azaaimanartembilan
authored andcommitted
GH-8581: Don't overwrite external SshClient
Fixes #8581 Do not overwrite configuration of externally provided `SshClient` in the `DefaultSftpSessionFactory` * Replace JUnit `assertDoesNotThrow` by AssertJ `assertThatNoException` in test **Cherry-pick to `6.0.x`**
1 parent 6d7ee46 commit 775e6fd

File tree

2 files changed

+64
-27
lines changed

2 files changed

+64
-27
lines changed

spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* @author Pat Turner
6262
* @author Artem Bilan
6363
* @author Krzysztof Debski
64+
* @author Auke Zaaiman
6465
*
6566
* @since 2.0
6667
*/
@@ -335,36 +336,45 @@ private void doInitClient() throws IOException {
335336
if (this.port <= 0) {
336337
this.port = SshConstants.DEFAULT_PORT;
337338
}
338-
ServerKeyVerifier serverKeyVerifier =
339-
this.allowUnknownKeys ? AcceptAllServerKeyVerifier.INSTANCE : RejectAllServerKeyVerifier.INSTANCE;
340-
if (this.knownHosts != null) {
341-
serverKeyVerifier = new ResourceKnownHostsServerKeyVerifier(this.knownHosts);
342-
}
343-
this.sshClient.setServerKeyVerifier(serverKeyVerifier);
344-
345-
this.sshClient.setPasswordIdentityProvider(PasswordIdentityProvider.wrapPasswords(this.password));
346-
if (this.privateKey != null) {
347-
IoResource<Resource> privateKeyResource =
348-
new AbstractIoResource<>(Resource.class, this.privateKey) {
349-
350-
@Override
351-
public InputStream openInputStream() throws IOException {
352-
return getResourceValue().getInputStream();
353-
}
354-
};
355-
try {
356-
Collection<KeyPair> keys =
357-
SecurityUtils.getKeyPairResourceParser()
358-
.loadKeyPairs(null, privateKeyResource,
359-
FilePasswordProvider.of(this.privateKeyPassphrase));
360-
this.sshClient.setKeyIdentityProvider(KeyIdentityProvider.wrapKeyPairs(keys));
339+
340+
doInitInnerClient();
341+
342+
this.sshClient.start();
343+
}
344+
345+
private void doInitInnerClient() throws IOException {
346+
if (this.isInnerClient) {
347+
ServerKeyVerifier serverKeyVerifier =
348+
this.allowUnknownKeys ? AcceptAllServerKeyVerifier.INSTANCE : RejectAllServerKeyVerifier.INSTANCE;
349+
if (this.knownHosts != null) {
350+
serverKeyVerifier = new ResourceKnownHostsServerKeyVerifier(this.knownHosts);
361351
}
362-
catch (GeneralSecurityException ex) {
363-
throw new IOException("Cannot load private key: " + this.privateKey.getFilename(), ex);
352+
this.sshClient.setServerKeyVerifier(serverKeyVerifier);
353+
354+
this.sshClient.setPasswordIdentityProvider(PasswordIdentityProvider.wrapPasswords(this.password));
355+
if (this.privateKey != null) {
356+
IoResource<Resource> privateKeyResource =
357+
new AbstractIoResource<>(Resource.class, this.privateKey) {
358+
359+
@Override
360+
public InputStream openInputStream() throws IOException {
361+
return getResourceValue().getInputStream();
362+
}
363+
364+
};
365+
try {
366+
Collection<KeyPair> keys =
367+
SecurityUtils.getKeyPairResourceParser()
368+
.loadKeyPairs(null, privateKeyResource,
369+
FilePasswordProvider.of(this.privateKeyPassphrase));
370+
this.sshClient.setKeyIdentityProvider(KeyIdentityProvider.wrapKeyPairs(keys));
371+
}
372+
catch (GeneralSecurityException ex) {
373+
throw new IOException("Cannot load private key: " + this.privateKey.getFilename(), ex);
374+
}
364375
}
376+
this.sshClient.setUserInteraction(this.userInteraction);
365377
}
366-
this.sshClient.setUserInteraction(this.userInteraction);
367-
this.sshClient.start();
368378
}
369379

370380
@Override

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/session/SftpSessionFactoryTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import java.util.Collections;
2424
import java.util.List;
2525

26+
import org.apache.sshd.client.SshClient;
27+
import org.apache.sshd.client.auth.password.PasswordIdentityProvider;
28+
import org.apache.sshd.client.keyverifier.AcceptAllServerKeyVerifier;
2629
import org.apache.sshd.common.SshException;
2730
import org.apache.sshd.server.SshServer;
2831
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
@@ -33,12 +36,14 @@
3336
import org.springframework.core.task.SimpleAsyncTaskExecutor;
3437

3538
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.assertj.core.api.Assertions.assertThatNoException;
3640
import static org.assertj.core.api.Assertions.fail;
3741
import static org.awaitility.Awaitility.await;
3842

3943
/**
4044
* @author Gary Russell
4145
* @author Artem Bilan
46+
* @author Auke Zaaiman
4247
*
4348
* @since 3.0.2
4449
*/
@@ -126,4 +131,26 @@ public void concurrentGetSessionDoesntCauseFailure() throws IOException {
126131
}
127132
}
128133

134+
@Test
135+
void externallyProvidedSshClientShouldNotHaveItsConfigurationOverwritten() throws IOException {
136+
try (SshServer server = SshServer.setUpDefaultServer()) {
137+
server.setPasswordAuthenticator((arg0, arg1, arg2) -> true);
138+
server.setPort(0);
139+
server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath()));
140+
server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
141+
server.start();
142+
143+
SshClient externalClient = SshClient.setUpDefaultClient();
144+
externalClient.setServerKeyVerifier(AcceptAllServerKeyVerifier.INSTANCE);
145+
externalClient.setPasswordIdentityProvider(PasswordIdentityProvider.wrapPasswords("pass"));
146+
147+
DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory(externalClient, false);
148+
sftpSessionFactory.setHost("localhost");
149+
sftpSessionFactory.setPort(server.getPort());
150+
sftpSessionFactory.setUser("user");
151+
152+
assertThatNoException().isThrownBy(sftpSessionFactory::getSession);
153+
}
154+
}
155+
129156
}

0 commit comments

Comments
 (0)