Example usage for org.apache.commons.vfs2.provider.sftp SftpFileSystemConfigBuilder setStrictHostKeyChecking

List of usage examples for org.apache.commons.vfs2.provider.sftp SftpFileSystemConfigBuilder setStrictHostKeyChecking

Introduction

In this page you can find the example usage for org.apache.commons.vfs2.provider.sftp SftpFileSystemConfigBuilder setStrictHostKeyChecking.

Prototype

public void setStrictHostKeyChecking(final FileSystemOptions opts, final String hostKeyChecking)
        throws FileSystemException 

Source Link

Document

Configures the host key checking to use.

Usage

From source file:net.sourceforge.fullsync.fs.filesystems.SFTPFileSystem.java

@Override
public final void authSetup(final ConnectionDescription description, final FileSystemOptions options)
        throws org.apache.commons.vfs2.FileSystemException {
    StaticUserAuthenticator auth = new StaticUserAuthenticator(null,
            description.getParameter(ConnectionDescription.PARAMETER_USERNAME),
            description.getSecretParameter(ConnectionDescription.PARAMETER_PASSWORD));
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, auth);
    SftpFileSystemConfigBuilder cfg = SftpFileSystemConfigBuilder.getInstance();
    if (null != sshDirName) {
        cfg.setKnownHosts(options, new File(sshDirName, "known_hosts"));
    }/*from  w  ww  . ja v a2s  .  co  m*/
    logger.debug("SFTP using knownHosts: ", cfg.getKnownHosts(options));
    cfg.setUserInfo(options, this);
    cfg.setStrictHostKeyChecking(options, "ask");
    if ("enabled".equals(description.getParameter("publicKeyAuth"))) {
        cfg.setPreferredAuthentications(options, "publickey,password,keyboard-interactive");
    } else {
        cfg.setPreferredAuthentications(options, "password,keyboard-interactive");
    }
}

From source file:net.sourceforge.fullsync.fs.filesystems.SFTPAuthProvider.java

@Override
public final void authSetup(final ConnectionDescription description, final FileSystemOptions options)
        throws FileSystemException {
    String username = description.getUsername().orElse(""); //$NON-NLS-1$
    String password = description.getPassword().orElse(""); //$NON-NLS-1$
    StaticUserAuthenticator auth = new StaticUserAuthenticator(null, username, password);
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, auth);
    SftpFileSystemConfigBuilder cfg = SftpFileSystemConfigBuilder.getInstance();
    cfg.setUserDirIsRoot(options, desc.isUserDirIsRoot());
    if (null != SSH_DIR_NAME) {
        cfg.setKnownHosts(options, new File(SSH_DIR_NAME, "known_hosts")); //$NON-NLS-1$
    }/*from  www. j ava2  s  .c  o  m*/
    logger.debug("using knownHosts: ", cfg.getKnownHosts(options)); //$NON-NLS-1$
    cfg.setUserInfo(options, this);
    cfg.setStrictHostKeyChecking(options, "ask"); //$NON-NLS-1$
    if (description.getPublicKeyAuth().orElse(false).booleanValue()) {
        cfg.setPreferredAuthentications(options, "publickey,password,keyboard-interactive"); //$NON-NLS-1$
    } else {
        cfg.setPreferredAuthentications(options, "password,keyboard-interactive"); //$NON-NLS-1$
    }
}

From source file:hadoopInstaller.installation.Installer.java

private void configureVFS2SFTP() throws InstallationFatalError {
    getLog().trace("HadoopInstaller.Configure.SFTP.Start"); //$NON-NLS-1$
    FileSystemOptions options;// w  ww  .ja  va 2  s.  c o  m
    options = new FileSystemOptions();
    SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
    try {
        builder.setUserDirIsRoot(options, false);
        /*
         * TODO-- ssh-ask
         * 
         * In the case of ask, the UserInfo object should be passed with
         * builder.setUserInfo()
         */
        builder.setStrictHostKeyChecking(options, this.configuration.getStrictHostKeyChecking() ? "yes" : "no"); //$NON-NLS-1$//$NON-NLS-2$
        getLog().trace("HadoopInstaller.Configure.StrictHostKeyChecking", //$NON-NLS-1$
                this.configuration.getStrictHostKeyChecking());
        builder.setKnownHosts(options, new File(this.configuration.getSshKnownHosts()));
        getLog().trace("HadoopInstaller.Configure.KnownHosts", //$NON-NLS-1$
                this.configuration.getSshKnownHosts());
        File identities[] = { new File(getConfig().getSshKeyFile()) };
        getLog().trace("HadoopInstaller.Configure.PrivateKeyFile", //$NON-NLS-1$
                getConfig().getSshKeyFile());
        builder.setIdentities(options, identities);
        /*
         * TODO-- ssh-ask
         * 
         * what if the identities file is password protected? do we need to
         * use setUserInfo?
         */
    } catch (FileSystemException e) {
        throw new InstallationFatalError(e, "HadoopInstaller.Configure.SFTP.Fail"); //$NON-NLS-1$
    }
    this.sftpOptions = options;
    getLog().debug("HadoopInstaller.Configure.SFTP.Success"); //$NON-NLS-1$
}

From source file:fr.cls.atoll.motu.library.misc.vfs.VFSManager.java

/**
 * Sets the scheme.//w w  w. j  a  va  2 s. c om
 * 
 * @param scheme the scheme
 * 
 * @return the file system options
 * 
 * @throws MotuException the motu exception
 */
public FileSystemOptions setSchemeOpts(String scheme, String host) throws MotuException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("setSchemeOpts(String, String) - start");
    }

    if (Organizer.isNullOrEmpty(scheme)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("setSchemeOpts(String, String) - end");
        }
        return opts;
    }

    if (opts == null) {
        opts = new FileSystemOptions();
    }

    FileSystemConfigBuilder fscb = null;
    MotuConfigFileSystemWrapper<Boolean> wrapperBoolean = new MotuConfigFileSystemWrapper<Boolean>();
    MotuConfigFileSystemWrapper<Period> wrapperPeriod = new MotuConfigFileSystemWrapper<Period>();
    MotuConfigFileSystemWrapper<String> wrapperString = new MotuConfigFileSystemWrapper<String>();

    try {
        try {
            fscb = standardFileSystemManager.getFileSystemConfigBuilder(scheme);
        } catch (FileSystemException e) {
            LOG.error("setSchemeOpts(String)", e);

            fscb = standardFileSystemManager.getFileSystemConfigBuilder(VFSManager.DEFAULT_SCHEME);
        }

        if (fscb instanceof FtpFileSystemConfigBuilder) {
            FtpFileSystemConfigBuilder ftpFscb = (FtpFileSystemConfigBuilder) fscb;
            Boolean userDirIsRoot = wrapperBoolean.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_FTPUSERDIRISROOT);
            if (userDirIsRoot != null) {
                ftpFscb.setUserDirIsRoot(opts, userDirIsRoot);
            } else {
                ftpFscb.setUserDirIsRoot(opts, false);
            }

            Boolean passiveMode = wrapperBoolean.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_FTPPASSIVEMODE);
            ;
            if (passiveMode != null) {
                ftpFscb.setPassiveMode(opts, passiveMode);
            }
            Period dataTimeOut = wrapperPeriod.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_FTPDATATIMEOUT);
            if (dataTimeOut != null) {
                long value = dataTimeOut.toStandardDuration().getMillis();
                if (value > Integer.MAX_VALUE) {
                    throw new MotuException(String.format(
                            "Motu Configuration : sftp timeout value is too large '%ld' milliseconds. Max is '%d'",
                            value, Integer.MAX_VALUE));
                }
                if (value > 0) {
                    ftpFscb.setDataTimeout(opts, (int) value);
                }
            }
        }

        if (fscb instanceof HttpFileSystemConfigBuilder) {
            HttpFileSystemConfigBuilder httpFscb = (HttpFileSystemConfigBuilder) fscb;

            Boolean isUseProxy = wrapperBoolean.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_USEHTTPPROXY);
            if ((isUseProxy != null) && (isUseProxy)) {
                String proxyHost = wrapperString.getFieldValue(host,
                        MotuConfigFileSystemWrapper.PROP_HTTPPROXYHOST);
                String proxyPort = wrapperString.getFieldValue(host,
                        MotuConfigFileSystemWrapper.PROP_HTTPPROXYPORT);
                httpFscb.setProxyHost(opts, proxyHost);
                httpFscb.setProxyPort(opts, Integer.parseInt(proxyPort));
            }

        }

        if (fscb instanceof SftpFileSystemConfigBuilder) {
            SftpFileSystemConfigBuilder sftpFscb = (SftpFileSystemConfigBuilder) fscb;

            Boolean userDirIsRoot = wrapperBoolean.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_SFTPUSERDIRISROOT);
            if (userDirIsRoot != null) {
                sftpFscb.setUserDirIsRoot(opts, userDirIsRoot);
            } else {
                sftpFscb.setUserDirIsRoot(opts, false);
            }

            String strictHostKeyChecking = wrapperString.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_STRICTHOSTKEYCHECKING);
            if (strictHostKeyChecking != null) {
                sftpFscb.setStrictHostKeyChecking(opts, strictHostKeyChecking);
            }

            Period SftpSessionTimeOut = wrapperPeriod.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_SFTPSESSIONTIMEOUT);
            if (SftpSessionTimeOut != null) {
                long value = SftpSessionTimeOut.toStandardDuration().getMillis();
                if (value > Integer.MAX_VALUE) {
                    throw new MotuException(String.format(
                            "Motu Configuration : sftp timeout value is too large '%ld' milliseconds. Max is '%d'",
                            value, Integer.MAX_VALUE));
                }
                if (value > 0) {
                    sftpFscb.setTimeout(opts, (int) value);
                }
            }

            Boolean isUseProxy = wrapperBoolean.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_USESFTPPROXY);
            if ((isUseProxy != null) && (isUseProxy)) {
                String proxyHost = wrapperString.getFieldValue(host,
                        MotuConfigFileSystemWrapper.PROP_SFTPPROXYHOST);
                String proxyPort = wrapperString.getFieldValue(host,
                        MotuConfigFileSystemWrapper.PROP_SFTPPROXYPORT);
                sftpFscb.setProxyHost(opts, proxyHost);
                sftpFscb.setProxyPort(opts, Integer.parseInt(proxyPort));
            }

        }

    } catch (FileSystemException e) {
        LOG.error("setSchemeOpts(String, String)", e);

        throw new MotuException("Error in VFSManager#setScheme", e);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("setSchemeOpts(String, String) - end");
    }
    return opts;
}

From source file:pl.otros.vfs.browser.util.VFSUtils.java

/**
 * Returns a file representation/* ww w  .j  a v a 2 s  .  com*/
 *
 * @param filePath The file path
 * @return a file representation
 * @throws FileSystemException
 */
public static FileObject resolveFileObject(String filePath) throws FileSystemException {
    LOGGER.info("Resolving file: {}", filePath);
    if (filePath.startsWith("sftp://")) {
        SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
        builder.setStrictHostKeyChecking(opts, "no");
        builder.setUserDirIsRoot(opts, false);
        builder.setCompression(opts, "zlib,none");

    } else if (filePath.startsWith("smb://")) {

    } else if (filePath.startsWith("ftp://")) {
        FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
    }
    UserAuthenticatorFactory factory = new UserAuthenticatorFactory();

    OtrosUserAuthenticator authenticator = factory.getUiUserAuthenticator(persistentAuthStore, sessionAuthStore,
            filePath, opts);

    if (pathContainsCredentials(filePath)) {
        authenticator = null;
    }
    return resolveFileObject(filePath, opts, authenticator, persistentAuthStore, sessionAuthStore);
}

From source file:pl.otros.vfs.browser.util.VFSUtils.java

/**
 * Returns a file representation/*w  ww  .  j  a  va 2  s .  c o m*/
 *
 * @param filePath The file path
 * @param options  The filesystem options
 * @return a file representation
 * @throws FileSystemException
 */
public static FileObject resolveFileObject(String filePath, FileSystemOptions options,
        OtrosUserAuthenticator authenticator, AuthStore persistentAuthStore, AuthStore sessionAuthStore)
        throws FileSystemException {
    if (filePath.startsWith("sftp://")) {
        SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
        builder.setStrictHostKeyChecking(opts, "no");
        builder.setUserDirIsRoot(opts, false);
        builder.setCompression(opts, "zlib,none");
    }

    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, authenticator);
    FileObject resolveFile;

    VFSURIParser parser = new VFSURIParser(filePath);
    //Get file type to force authentication
    try {
        resolveFile = getFileSystemManager().resolveFile(filePath, options);
        resolveFile.getType();
    } catch (FileSystemException e) {
        LOGGER.error("Error resolving file " + filePath, e);
        Throwable rootCause = Throwables.getRootCause(e);
        boolean authorizationFailed = false;
        authorizationFailed = checkForWrongCredentials(rootCause);
        if (authorizationFailed) {
            LOGGER.error("Wrong user name or password for " + filePath);
            //clear last data
            //authenticator can be null if user/password was entered in URL
            if (authenticator != null) {
                UserAuthenticationDataWrapper lastUserAuthenticationData = authenticator
                        .getLastUserAuthenticationData();
                lastUserAuthenticationData.remove(UserAuthenticationDataWrapper.PASSWORD);
                String user = new String(lastUserAuthenticationData.getData(UserAuthenticationData.USERNAME));
                UserAuthenticationInfo auInfo = new UserAuthenticationInfo(parser.getProtocol().getName(),
                        parser.getHostname(), user);
                sessionAuthStore.remove(auInfo);
                sessionAuthStore.add(auInfo, lastUserAuthenticationData);
                LOGGER.info("Removing password for {} on {}",
                        new Object[] {
                                new String(lastUserAuthenticationData.getData(UserAuthenticationData.USERNAME)),
                                filePath });
            }
        }
        throw e;
    }

    if (resolveFile != null && authenticator != null && authenticator.getLastUserAuthenticationData() != null) {
        UserAuthenticationDataWrapper lastUserAuthenticationData = authenticator
                .getLastUserAuthenticationData();
        Map<Type, char[]> addedTypes = lastUserAuthenticationData.getAddedTypes();
        String user = new String(addedTypes.get(UserAuthenticationData.USERNAME));
        UserAuthenticationInfo auInfo = new UserAuthenticationInfo(parser.getProtocol().getName(),
                parser.getHostname(), user);
        sessionAuthStore.add(auInfo, lastUserAuthenticationData.copy());
        if (authenticator.isPasswordSave()) {
            LOGGER.info("Saving password for {}://{}@{}",
                    new Object[] { parser.getProtocol().getName(), user, parser.getHostname() });
            persistentAuthStore.add(auInfo, lastUserAuthenticationData);
            saveAuthStore();
        }
    }
    return resolveFile;
}