Search in sources :

Example 1 with SshClient

use of com.sshtools.j2ssh.SshClient in project iaf by ibissource.

the class FtpSession method openSftpClient.

private void openSftpClient(String remoteDirectory) throws FtpConnectException {
    try {
        // Set the connection properties and if necessary the proxy properties
        SshConnectionProperties sshProp = new SshConnectionProperties();
        sshProp.setHost(host);
        sshProp.setPort(port);
        if (StringUtils.isNotEmpty(prefCSEncryption))
            sshProp.setPrefCSEncryption(prefCSEncryption);
        if (StringUtils.isNotEmpty(prefSCEncryption))
            sshProp.setPrefCSEncryption(prefSCEncryption);
        if (!StringUtils.isEmpty(proxyHost)) {
            sshProp.setTransportProvider(proxyTransportType);
            sshProp.setProxyHost(proxyHost);
            sshProp.setProxyPort(proxyPort);
            CredentialFactory pcf = new CredentialFactory(getProxyAuthAlias(), proxyUsername, proxyPassword);
            if (!StringUtils.isEmpty(pcf.getUsername())) {
                sshProp.setProxyUsername(pcf.getUsername());
                sshProp.setProxyPassword(pcf.getPassword());
            }
        }
        // make a secure connection with the remote host
        sshClient = new SshClient();
        if (StringUtils.isNotEmpty(knownHostsPath)) {
            AbstractKnownHostsKeyVerification hv = null;
            if (consoleKnownHostsVerifier) {
                hv = new ConsoleKnownHostsKeyVerification(knownHostsPath);
            } else {
                hv = new SftpHostVerification(knownHostsPath);
            }
            sshClient.connect(sshProp, hv);
        } else {
            sshClient.connect(sshProp, new IgnoreHostKeyVerification());
        }
        SshAuthenticationClient sac;
        if (!isKeyboardInteractive()) {
            // pass the authentication information
            sac = getSshAuthentication();
        } else {
            // TODO: detecteren dat sshClient.getAvailableAuthMethods("ftpmsg")
            // wel keyboard-interactive terug geeft, maar geen password en dan deze methode
            // gebruiken
            final CredentialFactory credentialFactory = new CredentialFactory(getAuthAlias(), getUsername(), getPassword());
            KBIAuthenticationClient kbiAuthenticationClient = new KBIAuthenticationClient();
            kbiAuthenticationClient.setUsername(credentialFactory.getUsername());
            kbiAuthenticationClient.setKBIRequestHandler(new KBIRequestHandler() {

                public void showPrompts(String name, String instruction, KBIPrompt[] prompts) {
                    // deze 3 regels in x.zip naar Zenz gemaild, hielp ook niet
                    if (prompts == null) {
                        return;
                    }
                    for (int i = 0; i < prompts.length; i++) {
                        prompts[i].setResponse(credentialFactory.getPassword());
                    }
                }
            });
            sac = kbiAuthenticationClient;
        }
        int result = sshClient.authenticate(sac);
        if (result != AuthenticationProtocolState.COMPLETE) {
            closeSftpClient();
            throw new IOException("Could not authenticate to sftp server " + result);
        }
        // use the connection for sftp
        sftpClient = sshClient.openSftpClient();
        if (!StringUtils.isEmpty(remoteDirectory)) {
            sftpClient.cd(remoteDirectory);
        }
    } catch (Exception e) {
        closeSftpClient();
        throw new FtpConnectException(e);
    }
}
Also used : SshAuthenticationClient(com.sshtools.j2ssh.authentication.SshAuthenticationClient) SshClient(com.sshtools.j2ssh.SshClient) AbstractKnownHostsKeyVerification(com.sshtools.j2ssh.transport.AbstractKnownHostsKeyVerification) IgnoreHostKeyVerification(com.sshtools.j2ssh.transport.IgnoreHostKeyVerification) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) KBIPrompt(com.sshtools.j2ssh.authentication.KBIPrompt) KBIAuthenticationClient(com.sshtools.j2ssh.authentication.KBIAuthenticationClient) IOException(java.io.IOException) KBIRequestHandler(com.sshtools.j2ssh.authentication.KBIRequestHandler) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SshConnectionProperties(com.sshtools.j2ssh.configuration.SshConnectionProperties) ConsoleKnownHostsKeyVerification(com.sshtools.j2ssh.transport.ConsoleKnownHostsKeyVerification)

Aggregations

SshClient (com.sshtools.j2ssh.SshClient)1 KBIAuthenticationClient (com.sshtools.j2ssh.authentication.KBIAuthenticationClient)1 KBIPrompt (com.sshtools.j2ssh.authentication.KBIPrompt)1 KBIRequestHandler (com.sshtools.j2ssh.authentication.KBIRequestHandler)1 SshAuthenticationClient (com.sshtools.j2ssh.authentication.SshAuthenticationClient)1 SshConnectionProperties (com.sshtools.j2ssh.configuration.SshConnectionProperties)1 AbstractKnownHostsKeyVerification (com.sshtools.j2ssh.transport.AbstractKnownHostsKeyVerification)1 ConsoleKnownHostsKeyVerification (com.sshtools.j2ssh.transport.ConsoleKnownHostsKeyVerification)1 IgnoreHostKeyVerification (com.sshtools.j2ssh.transport.IgnoreHostKeyVerification)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)1 CredentialFactory (nl.nn.adapterframework.util.CredentialFactory)1