0
votes

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.

1
What is the value of config.getKnownHostPublicKey()?Martin Prikryl
It is bulk of alphanumetic characters like mhyrtt+ZdZpjUC+CeoQGEA got from the carrier system as their public key.Ramesh Kangamuthu
I have no idea what that is. It's not any public key or public key fingerprint format, I've ever seen. Definitely not something that JSch would accept.Martin Prikryl

1 Answers

1
votes

JSch.setKnownHosts accepts the public key in the format of OpenSSH authorized_keys file, which is like:

example.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0hVqZOvZ7yWgie9OHdTORJVI5fJJoH1yEGamAd5G3werH0z7e9ybtq1mGUeRkJtea7bzru0ISR0EZ9HIONoGYrDmI7S+BiwpDBUKjva4mAsvzzvsy6Ogy/apkxm6Kbcml8u4wjxaOw3NKzKqeBvR3pc+nQVA+SJUZq8D2XBRd4EDUFXeLzwqwen9G7gSLGB1hJkSuRtGRfOHbLUuCKNR8RV82i3JvlSnAwb3MwN0m3WGdlJA8J+5YAg4e6JgSKrsCObZK7W1R6iuyuH1zA+dtAHyDyYVHB4FnYZPL0hgz2PSb9c+iDEiFcT/lT4/dQ+kRW6DYn66lS8peS8zCJ9CSQ==

That's not the format you use.