Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add file: to private key path and public key path to load resources #212

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import org.apache.sshd.sftp.client.SftpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.annotation.ServiceActivator;
Expand Down Expand Up @@ -44,6 +45,8 @@
public class AutoConfiguration {

private Logger logger = LoggerFactory.getLogger(AutoConfiguration.class);
@Autowired
ResourceLoader resourceLoader;

private SftpInputProperties properties;

Expand All @@ -67,11 +70,15 @@ public SessionFactory<SftpClient.DirEntry> sftpSessionFactory() throws InvalidCo
factory.setPort(properties.getPort());
factory.setUser(properties.getUsername());
if (properties.getSshPrivateKey() != null) {
if(!(new File(properties.getSshPrivateKey()).exists()))
throw new KnownHostFileNotDefinedException("Cannot find known_hosts file private key file. ");

logger.info("SFTP Configuration: setPrivateKey");
Resource resource = new DefaultResourceLoader().getResource(properties.getSshPrivateKey());
logger.info("SFTP Configuration: privateKey length {}", resource.contentLength());
logger.info("SFTP Configuration: privateKey {}", resource.getContentAsString(Charset.defaultCharset()));
Resource resource = resourceLoader.getResource("file:"+properties.getSshPrivateKey());;
logger.info("SFTP Configuration: privateKey - length {}", resource.contentLength());
logger.info("SFTP Configuration: privateKey - content {}", resource.getContentAsString(Charset.defaultCharset()));
factory.setPrivateKey(resource);

factory.setPrivateKeyPassphrase(properties.getSshPrivatePassphrase());
} else {
logger.info("SFTP Configuration: setPassword");
Expand All @@ -88,7 +95,11 @@ public SessionFactory<SftpClient.DirEntry> sftpSessionFactory() throws InvalidCo
if (!knownHostFile.exists())
throw new KnownHostFileNotFoundException("Cannot find known_hosts file when allow-unknown-keys is false.");

factory.setKnownHostsResource(new DefaultResourceLoader().getResource(properties.getKnownHostFile()));
logger.info("SFTP Known Hosts");
Resource resource = resourceLoader.getResource("file:"+properties.getKnownHostFile());
logger.info("SFTP Known Hosts: length = {}", resource.contentLength());
logger.info("SFTP Known Hosts: content = {}", resource.getContentAsString(Charset.defaultCharset()));
factory.setPrivateKey(resource);
}

CachingSessionFactory<SftpClient.DirEntry> cachingSessionFactory = new CachingSessionFactory<>(factory);
Expand Down
Loading