Skip to content

Commit 6b82681

Browse files
authored
GH-3211: Add DefSftpSF.setKnownHosts(Resource) (#3212)
* GH-3211: Add DefSftpSF.setKnownHosts(Resource) Fixes #3211 * Add `DefaultSftpSessionFactory.setKnownHosts(Resource)` to allow to configure externally any resource for file with known_hosts content * Deprecate an existing method in favor of new one * The new method makes it aligned with the `setPrivateKey(Resource)` * Fix tests do not use a deprecated method any more **Cherry-pick to 5.2.x** * * Rename to `setKnownHostsResource()` to avoid XML parser confusion * Change `sftp.adoc` to reflect a new property
1 parent 36f96ec commit 6b82681

12 files changed

+51
-47
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.sftp.session;
1818

1919
import java.io.IOException;
20+
import java.io.InputStream;
2021
import java.time.Duration;
2122
import java.util.Arrays;
2223
import java.util.Properties;
@@ -27,6 +28,7 @@
2728
import org.apache.commons.logging.LogFactory;
2829

2930
import org.springframework.beans.factory.BeanCreationException;
31+
import org.springframework.core.io.FileSystemResource;
3032
import org.springframework.core.io.Resource;
3133
import org.springframework.integration.file.remote.session.SessionFactory;
3234
import org.springframework.integration.file.remote.session.SharedSessionCapable;
@@ -81,7 +83,7 @@ public class DefaultSftpSessionFactory implements SessionFactory<LsEntry>, Share
8183

8284
private String password;
8385

84-
private String knownHosts;
86+
private Resource knownHosts;
8587

8688
private Resource privateKey;
8789

@@ -191,8 +193,21 @@ public void setPassword(String password) {
191193
* false (default).</b>
192194
* @param knownHosts The known hosts.
193195
* @see JSch#setKnownHosts(String)
196+
* @deprecated since 5.2.5 in favor of {@link #setKnownHostsResource(Resource)}
194197
*/
198+
@Deprecated
195199
public void setKnownHosts(String knownHosts) {
200+
setKnownHostsResource(new FileSystemResource(knownHosts));
201+
}
202+
203+
/**
204+
* Specifies the filename that will be used for a host key repository.
205+
* The file has the same format as OpenSSH's known_hosts file.
206+
* @param knownHosts the resource for known hosts.
207+
* @see JSch#setKnownHosts(InputStream)
208+
* @since 5.2.5
209+
*/
210+
public void setKnownHostsResource(Resource knownHosts) {
196211
this.knownHosts = knownHosts;
197212
}
198213

@@ -323,7 +338,7 @@ public void setEnableDaemonThread(Boolean enableDaemonThread) {
323338
* implementation must respond to Jsch calls in a suitable way.
324339
* <p>
325340
* Jsch calls {@link UserInfo#promptYesNo(String)} when connecting to an unknown host,
326-
* or when a known host's key has changed (see {@link #setKnownHosts(String)
341+
* or when a known host's key has changed (see {@link #setKnownHostsResource(Resource)}
327342
* knownHosts}). Generally, it should return false as returning true will accept all
328343
* new keys or key changes.
329344
* <p>
@@ -347,7 +362,7 @@ public void setUserInfo(UserInfo userInfo) {
347362
/**
348363
* When no {@link UserInfo} has been provided, set to true to unconditionally allow
349364
* connecting to an unknown host or when a host's key has changed (see
350-
* {@link #setKnownHosts(String) knownHosts}). Default false (since 4.2).
365+
* {@link #setKnownHostsResource(Resource) knownHosts}). Default false (since 4.2).
351366
* Set to true if a knownHosts file is not provided.
352367
* @param allowUnknownKeys true to allow connecting to unknown hosts.
353368
* @since 4.1.7
@@ -380,8 +395,7 @@ public SftpSession getSession() {
380395
freshJschSession = true;
381396
}
382397
sftpSession = new SftpSession(jschSession);
383-
JavaUtils.INSTANCE
384-
.acceptIfNotNull(this.channelConnectTimeout, sftpSession::setChannelConnectTimeout);
398+
JavaUtils.INSTANCE.acceptIfNotNull(this.channelConnectTimeout, sftpSession::setChannelConnectTimeout);
385399
sftpSession.connect();
386400
if (this.isSharedSession && freshJschSession) {
387401
this.sharedJschSession = jschSession;
@@ -408,8 +422,8 @@ private com.jcraft.jsch.Session initJschSession() throws JSchException, IOExcept
408422
if (this.port <= 0) {
409423
this.port = 22;
410424
}
411-
if (StringUtils.hasText(this.knownHosts)) {
412-
this.jsch.setKnownHosts(this.knownHosts);
425+
if (this.knownHosts != null) {
426+
this.jsch.setKnownHosts(this.knownHosts.getInputStream());
413427
}
414428

415429
// private key
@@ -467,6 +481,7 @@ public void resetSharedSession() {
467481
* sensible defaults if null. As the password is configured in this Factory, the
468482
* wrapper will return the factory's configured password and only delegate to the
469483
* UserInfo if null.
484+
*
470485
* @since 4.1.7
471486
*/
472487
private class UserInfoWrapper implements UserInfo, UIKeyboardInteractive {
@@ -548,7 +563,7 @@ public boolean promptYesNo(String message) {
548563
}
549564
else {
550565
if (LOGGER.isDebugEnabled()) {
551-
LOGGER.debug("No UserInfo provided - " + message + ", returning:"
566+
LOGGER.debug("No UserInfo provided - " + message + ", returning: "
552567
+ DefaultSftpSessionFactory.this.allowUnknownKeys);
553568
}
554569
return DefaultSftpSessionFactory.this.allowUnknownKeys;

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapterParserCachingTests-context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
1818
<beans:property name="host" value="loclahost"/>
19-
<beans:property name="knownHosts" value="local, foo.com, bar.foo"/>
19+
<beans:property name="knownHostsResource" value="#{ new org.springframework.core.io.ByteArrayResource('local, foo.com, bar.foo'.bytes)}"/>
2020
<beans:property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftp_rsa"/>
2121
<beans:property name="privateKeyPassphrase" value="ghj"/>
2222
<beans:property name="password" value="hello"/>

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapterParserTests-context-fail-autocreate.xml

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,17 @@
33
xmlns="http://www.springframework.org/schema/integration"
44
xmlns:beans="http://www.springframework.org/schema/beans"
55
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6-
xmlns:p="http://www.springframework.org/schema/p"
7-
xmlns:context="http://www.springframework.org/schema/context"
8-
xmlns:util="http://www.springframework.org/schema/util"
9-
xmlns:tool="http://www.springframework.org/schema/tool"
10-
xmlns:lang="http://www.springframework.org/schema/lang"
116
xmlns:sftp="http://www.springframework.org/schema/integration/sftp"
127
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
138
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
14-
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
15-
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd
16-
http://www.springframework.org/schema/tool https://www.springframework.org/schema/tool/spring-tool.xsd
17-
http://www.springframework.org/schema/lang https://www.springframework.org/schema/lang/spring-lang.xsd
189
http://www.springframework.org/schema/integration/sftp https://www.springframework.org/schema/integration/sftp/spring-integration-sftp.xsd">
1910

2011
<channel id="requestChannel"/>
2112

2213
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
2314
<beans:property name="host" value="loclahost"/>
24-
<beans:property name="knownHosts" value="local, foo.com, bar.foo"/>
15+
<beans:property name="knownHostsResource"
16+
value="#{ new org.springframework.core.io.ByteArrayResource('local, foo.com, bar.foo'.bytes)}"/>
2517
<beans:property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>
2618
<beans:property name="privateKeyPassphrase" value="ghj"/>
2719
<beans:property name="password" value="hello"/>
@@ -30,17 +22,17 @@
3022
</beans:bean>
3123

3224
<sftp:inbound-channel-adapter id="sftpAdapterNoAutoCreate"
33-
channel="requestChannel"
34-
session-factory="sftpSessionFactory"
35-
filter="filter"
36-
remote-directory="/foo"
37-
local-directory="file:foo"
38-
auto-create-local-directory="false"
39-
delete-remote-files="false">
25+
channel="requestChannel"
26+
session-factory="sftpSessionFactory"
27+
filter="filter"
28+
remote-directory="/foo"
29+
local-directory="file:foo"
30+
auto-create-local-directory="false"
31+
delete-remote-files="false">
4032
<poller fixed-rate="1000"/>
4133
</sftp:inbound-channel-adapter>
4234

43-
<beans:bean id="filter" class="org.springframework.integration.sftp.filters.SftpPatternMatchingFileListFilter">
35+
<beans:bean id="filter" class="org.springframework.integration.sftp.filters.SftpSimplePatternFileListFilter">
4436
<beans:constructor-arg value="."/>
4537
</beans:bean>
4638

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapterParserTests-context-fail.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SimpleSftpSessionFactory">
2323
<beans:property name="host" value="loclahost"/>
24-
<beans:property name="knownHosts" value="local, foo.com, bar.foo"/>
24+
<beans:property name="knownHostsResource" value="local, foo.com, bar.foo"/>
2525
<beans:property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>
2626
<beans:property name="privateKeyPassphrase" value="ghj"/>
2727
<beans:property name="password" value="hello"/>

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapterParserTests-context.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
2424
<beans:property name="host" value="loclahost"/>
25-
<beans:property name="knownHosts" value="local, foo.com, bar.foo"/>
25+
<beans:property name="knownHostsResource"
26+
value="#{ new org.springframework.core.io.ByteArrayResource('local, foo.com, bar.foo'.bytes)}"/>
2627
<beans:property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftp_rsa"/>
2728
<beans:property name="privateKeyPassphrase" value="ghj"/>
2829
<beans:property name="password" value="hello"/>

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/MessageHistory-context.xml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,17 @@
33
xmlns="http://www.springframework.org/schema/integration"
44
xmlns:beans="http://www.springframework.org/schema/beans"
55
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6-
xmlns:p="http://www.springframework.org/schema/p"
7-
xmlns:context="http://www.springframework.org/schema/context"
8-
xmlns:util="http://www.springframework.org/schema/util"
9-
xmlns:tool="http://www.springframework.org/schema/tool"
10-
xmlns:lang="http://www.springframework.org/schema/lang"
116
xmlns:sftp="http://www.springframework.org/schema/integration/sftp"
127
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
138
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
14-
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
15-
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd
16-
http://www.springframework.org/schema/tool https://www.springframework.org/schema/tool/spring-tool.xsd
17-
http://www.springframework.org/schema/lang https://www.springframework.org/schema/lang/spring-lang.xsd
189
http://www.springframework.org/schema/integration/sftp https://www.springframework.org/schema/integration/sftp/spring-integration-sftp.xsd">
1910

2011
<channel id="inboundFilesChannel"/>
2112

2213
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
2314
<beans:property name="host" value="loclahost"/>
24-
<beans:property name="knownHosts" value="local, foo.com, bar.foo"/>
15+
<beans:property name="knownHostsResource"
16+
value="#{ new org.springframework.core.io.ByteArrayResource('local, foo.com, bar.foo'.bytes)}"/>
2517
<beans:property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>
2618
<beans:property name="privateKeyPassphrase" value="ghj"/>
2719
<beans:property name="password" value="hello"/>

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapterParserCachingTests-context.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
1111
<property name="host" value="localhost"/>
12-
<property name="knownHosts" value="local, foo.com, bar.foo"/>
12+
<property name="knownHostsResource"
13+
value="#{ new org.springframework.core.io.ByteArrayResource('local, foo.com, bar.foo'.bytes)}"/>
1314
<property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>
1415
<property name="privateKeyPassphrase" value="ghj"/>
1516
<property name="password" value="hello"/>

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapterParserTests-context-fail-fileFileGen.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SimpleSftpSessionFactory">
1111
<property name="host" value="localhost"/>
12-
<property name="knownHosts" value="local, foo.com, bar.foo"/>
12+
<property name="knownHostsResource" value="local, foo.com, bar.foo"/>
1313
<property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>
1414
<property name="privateKeyPassphrase" value="ghj"/>
1515
<property name="password" value="hello"/>

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapterParserTests-context-fail.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
1111
<property name="host" value="localhost"/>
12-
<property name="knownHosts" value="local, foo.com, bar.foo"/>
12+
<property name="knownHostsResource"
13+
value="#{ new org.springframework.core.io.ByteArrayResource('local, foo.com, bar.foo'.bytes)}"/>
1314
<property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>
1415
<property name="privateKeyPassphrase" value="ghj"/>
1516
<property name="password" value="hello"/>

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapterParserTests-context.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
1111
<property name="host" value="localhost"/>
12-
<property name="knownHosts" value="local, foo.com, bar.foo"/>
12+
<property name="knownHostsResource"
13+
value="#{ new org.springframework.core.io.ByteArrayResource('local, foo.com, bar.foo'.bytes)}"/>
1314
<property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>
1415
<property name="privateKeyPassphrase" value="ghj"/>
1516
<property name="password" value="hello"/>

0 commit comments

Comments
 (0)