We have to connect to an external SFTP server from our application using proxy server internet.ford.com by configuring username, password, and public key.
We are facing java code with JSch library to connect to the SFTP server. We are facing an issue when we are trying to connect the SFTP server from our application hosted in a server environment(Dev, QA)
I am getting UnknownHostKey
exception as shown in the below log.
4463 [8/19/19 12:05:06:301 EDT] 0001fadf TransCommunic I JSchException @ TransCommunicationMgr:- connect Result Code: UnknownHostKey: 74.126.93.138. RSA key fingerprint is e8:90:a9:f3:3d:8f:83:26:e3:24:2b:2f:a1:71:e3:7c
I set the public key as a byte array value in the below code chunk.
// knownHostPublicKey is a String variable
knownHostPublicKey=config.getKnownHostPublicKey();
jsch.setKnownHosts(new
ByteArrayInputStream(knownHostPublicKey.getBytes()));
Added the complete code that involves SFTP connection process to the show some code section.
I tried SFTP server connection using java JSch library and through proxy server internet.ford.com
public Session connectSFTP(final FtpCredentials config)
throws OfBusinessRuntimeException{
final String METHOD_NAME = "connect";
log.entering(CLASS_NAME, METHOD_NAME, config);
/*Local attributes declaration */
String host = null;
String user = null;
String password = null;
int elapsedTime = 0;
int sftpPort = 0;
String knownHostPublicKey = null;
String sftpProxy = null;
int sftpProxyPort=0;
Session sftpSession = null;
JSch jsch = new JSch();
/* Beginning of try catch block */
try {
// Getting FTP connection details.
if (config != null) {
host = config.getHost();
user = config.getUserId();
sftpPort = config.getSftpPort();
password = config.getPassword();
elapsedTime = config.getElapsedTime();
knownHostPublicKey=config.getKnownHostPublicKey();
/*byte[] hostPublicKey=Base64.getDecoder().decode(knownHostPublicKey);
HostKey hostKey=new HostKey(host,hostPublicKey);
jsch.getHostKeyRepository().add(hostKey,null);
*/
jsch.
jsch.setKnownHosts(new ByteArrayInputStream(knownHostPublicKey.getBytes()));
sftpProxy=config.getSftpProxy();
sftpProxyPort=config.getSftpProxyPort();
sftpSession = jsch.getSession(user, host, sftpPort);
}
log.info("Before Connect " + host + " User :- " + user);
if (host == null || user == null || password == null) {
throw new OfBusinessRuntimeException(
"SFTP Host Information not found.");
}
sftpSession.setHost(host);
sftpSession.setPassword(password);
sftpSession.setPort(sftpPort);
//java.util.Properties config = new java.util.Properties();
// // force aes256-ctr encryption
//config.put("cipher.s2c", "aes256-ctr");
//config.put("cipher.c2s", "aes256-ctr");
//config.put("CheckCiphers", "aes256-ctr");
//session.setConfig(config);
sftpSession.setProxy(new ProxyHTTP(sftpProxy,sftpProxyPort));
/*
* Setting the timeout to 30 seconds to ensure connection is made
* for testing setting the port to 22 as this should be the one
* to accept the connection
**/
sftpSession.setTimeout(elapsedTime);
sftpSession.connect();
log.info("SFTP Session Connection is successful");
} catch (final JSchException jschException) {
jschException.printStackTrace();
log.info("JSchException @ TransCommunicationMgr:- "
+ METHOD_NAME + " Result Code: "
+ jschException.getMessage());
//throw new OfBusinessRuntimeException(jschException.getMessage(),jschException);
}
/* Log existing method. */
log.exiting(CLASS_NAME, METHOD_NAME, sftpSession);
return sftpSession;
}
Expects the connection to an external SFTP server is successful.
config.getKnownHostPublicKey()
? – Martin Prikryl