Search in sources :

Example 1 with SftpListener

use of com.axway.ats.core.filetransfer.model.ftp.SftpListener in project ats-framework by Axway.

the class SftpClient method doConnect.

private void doConnect(String publicKeyAlias) throws FileTransferException {
    // make new SFTP object for every new connection
    disconnect();
    this.jsch = new JSch();
    /* NOTE: Due to logging being global (static), if one thread enables debug mode, all threads will log messages,
         * and if another thread disables it, logging will be stopped for all threads,
         * until at least one thread enables it again
         */
    if (isDebugMode()) {
        JSch.setLogger(new SftpListener());
        debugProgressMonitor = new SftpFileTransferProgressMonitor();
    } else {
        debugProgressMonitor = null;
    }
    try {
        if (username != null) {
            this.session = jsch.getSession(this.username, this.hostname, this.port);
            this.session.setPassword(this.password);
        } else {
            this.jsch.addIdentity(this.keystoreFile, this.publicKeyAlias, this.keystorePassword.getBytes());
            this.session = this.jsch.getSession(this.username, this.hostname, this.port);
        }
        // skip checking of known hosts and verifying RSA keys
        this.session.setConfig("StrictHostKeyChecking", "no");
        // make keyboard-interactive last authentication method
        this.session.setConfig("PreferredAuthentications", "publickey,password,keyboard-interactive");
        this.session.setTimeout(this.timeout);
        if (this.ciphers != null && this.ciphers.size() > 0) {
            StringBuilder ciphers = new StringBuilder();
            for (SshCipher cipher : this.ciphers) {
                ciphers.append(cipher.getSshAlgorithmName() + ",");
            }
            this.session.setConfig("cipher.c2s", ciphers.toString());
            this.session.setConfig("cipher.s2c", ciphers.toString());
            this.session.setConfig("CheckCiphers", ciphers.toString());
        }
        if (this.listener != null) {
            this.listener.setResponses(new ArrayList<String>());
            JSch.setLogger((com.jcraft.jsch.Logger) this.listener);
        }
        /* SFTP reference implementation does not support transfer mode.
               Due to that, JSch does not support it either.
               For now setTransferMode() has no effect. It may be left like that,
               implemented to throw new FileTransferException( "Not implemented" )
               or log warning, that SFTP protocol does not support transfer mode change.
            */
        this.session.connect();
        this.channel = (ChannelSftp) this.session.openChannel("sftp");
        this.channel.connect();
    } catch (JSchException e) {
        throw new FileTransferException("Unable to connect!", e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) SshCipher(com.axway.ats.common.filetransfer.SshCipher) SftpListener(com.axway.ats.core.filetransfer.model.ftp.SftpListener) JSch(com.jcraft.jsch.JSch) SftpFileTransferProgressMonitor(com.axway.ats.core.filetransfer.model.ftp.SftpFileTransferProgressMonitor)

Example 2 with SftpListener

use of com.axway.ats.core.filetransfer.model.ftp.SftpListener in project ats-framework by Axway.

the class SftpClient method doConnect.

private void doConnect() throws FileTransferException {
    // make new SFTP object for every new connection
    disconnect();
    /* NOTE: Due to logging being global (static), if one thread enables debug mode, all threads will log messages,
         * and if another thread disables it, logging will be stopped for all threads,
         * until at least one thread enables it again
         */
    if (isDebugMode()) {
        JSch.setLogger(new SftpListener());
        debugProgressMonitor = new SftpFileTransferProgressMonitor();
    } else {
        debugProgressMonitor = null;
    }
    try {
        addHostKeyRepository();
        if (username != null) {
            this.session = jsch.getSession(this.username, this.hostname, this.port);
        }
        this.session.setPassword(this.password);
        if (!StringUtils.isNullOrEmpty(this.keyStoreFile)) {
            byte[] privateKeyContent = getPrivateKeyContent();
            this.jsch.addIdentity("client_prvkey", privateKeyContent, null, null);
        }
        // The internally used client version 'SSH-2.0-JSCH-0.1.54' needs to be changed to 'SSH-2.0-OpenSSH_2.5.3'
        this.session.setClientVersion("SSH-2.0-OpenSSH_2.5.3");
        /**
         * if no trust server certificate or trust store are provided, do not check if hostname is in known hosts *
         */
        if (StringUtils.isNullOrEmpty(this.trustedServerSSLCerfiticateFile) && StringUtils.isNullOrEmpty(this.trustStoreFile)) {
            // skip checking of known hosts and verifying RSA keys
            this.session.setConfig("StrictHostKeyChecking", "no");
        }
        // make keyboard-interactive last authentication method
        this.session.setConfig("PreferredAuthentications", "publickey,password,keyboard-interactive");
        this.session.setTimeout(this.timeout);
        if (this.ciphers != null && this.ciphers.size() > 0) {
            StringBuilder ciphers = new StringBuilder();
            for (SshCipher cipher : this.ciphers) {
                ciphers.append(cipher.getSshAlgorithmName() + ",");
            }
            this.session.setConfig("cipher.c2s", ciphers.toString());
            this.session.setConfig("cipher.s2c", ciphers.toString());
            this.session.setConfig("CheckCiphers", ciphers.toString());
        }
        if (this.listener != null) {
            this.listener.setResponses(new ArrayList<String>());
            JSch.setLogger((com.jcraft.jsch.Logger) this.listener);
        }
        /* 
             * SFTP reference implementation does not support transfer mode.
               Due to that, JSch does not support it either.
               For now setTransferMode() has no effect. It may be left like that,
               implemented to throw new FileTransferException( "Not implemented" )
               or log warning, that SFTP protocol does not support transfer mode change.
            */
        this.session.connect();
        this.channel = (ChannelSftp) this.session.openChannel("sftp");
        this.channel.connect();
    } catch (Exception e) {
        String errMessage = "Unable to connect to  " + hostname + " on port " + this.port + " using username " + username + " and password " + password;
        log.error(errMessage, e);
        throw new FileTransferException(e);
    }
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) SshCipher(com.axway.ats.common.filetransfer.SshCipher) SftpListener(com.axway.ats.core.filetransfer.model.ftp.SftpListener) SftpFileTransferProgressMonitor(com.axway.ats.core.filetransfer.model.ftp.SftpFileTransferProgressMonitor) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) JSchException(com.jcraft.jsch.JSchException)

Aggregations

FileTransferException (com.axway.ats.common.filetransfer.FileTransferException)2 SshCipher (com.axway.ats.common.filetransfer.SshCipher)2 SftpFileTransferProgressMonitor (com.axway.ats.core.filetransfer.model.ftp.SftpFileTransferProgressMonitor)2 SftpListener (com.axway.ats.core.filetransfer.model.ftp.SftpListener)2 JSchException (com.jcraft.jsch.JSchException)2 JSch (com.jcraft.jsch.JSch)1 SftpException (com.jcraft.jsch.SftpException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1