Search in sources :

Example 1 with StrictHostChecking

use of com.oracle.bedrock.runtime.remote.options.StrictHostChecking in project oracle-bedrock by coherence-community.

the class JSchSessionFactory method createSession.

/**
 * Create a JSch {@link Session} connected to the specified remote host.
 *
 * @param userName        the user name to use to connect to the specified host
 * @param authentication  the {@link Authentication} method to use to authenticate the user
 * @param hostName        the host name of the remote host to connect to
 * @param port            the port on the remote host to connect to
 * @param socketFactory   the {@link JSchSocketFactory} to use
 * @param optionsByType   the {@link OptionsByType} to use to control the session
 *
 * @return a {@link Session} connected to the specified remote host
 *
 * @throws JSchException if an error occurs creating the {@link Session}
 */
public Session createSession(String hostName, int port, String userName, Authentication authentication, JSchSocketFactory socketFactory, OptionsByType optionsByType) throws JSchException {
    // allow the authentication to configure the framework
    if (authentication instanceof JSchBasedAuthentication) {
        ((JSchBasedAuthentication) authentication).configureFramework(jsch);
    }
    // create the remote session
    Session session = jsch.getSession(userName, hostName, port);
    // establish the specialized socket factory for the session
    session.setSocketFactory(socketFactory);
    // the session should not cause the JVM not to exit
    session.setDaemonThread(true);
    // determine the timeout
    Timeout timeout = optionsByType.getOrDefault(Timeout.class, Timeout.autoDetect());
    int timeoutMS = (int) timeout.getDuration().to(TimeUnit.MILLISECONDS);
    // set the default session timeouts (in milliseconds)
    session.setTimeout(timeoutMS);
    // allow the authentication to configure the session
    if (authentication instanceof JSchBasedAuthentication) {
        ((JSchBasedAuthentication) authentication).configureSession(session);
    }
    // ----- configure the session channel properties -----
    Properties config = new Properties();
    // are we to use strict-host-checking? (when it's not defined it's enabled it by default)
    StrictHostChecking strictHostChecking = optionsByType.get(StrictHostChecking.class);
    config.put("StrictHostKeyChecking", strictHostChecking.isEnabled() ? "yes" : "no");
    UserKnownHostsFile userKnownHostsFile = optionsByType.get(UserKnownHostsFile.class);
    String file = userKnownHostsFile == null ? null : userKnownHostsFile.getFile();
    if (file != null && !file.trim().isEmpty()) {
        config.put("UserKnownHostsFile", file.trim());
    }
    session.setConfig(config);
    // connect the session
    session.connect();
    return session;
}
Also used : Timeout(com.oracle.bedrock.options.Timeout) StrictHostChecking(com.oracle.bedrock.runtime.remote.options.StrictHostChecking) Properties(java.util.Properties) UserKnownHostsFile(com.oracle.bedrock.runtime.remote.options.UserKnownHostsFile) Session(com.jcraft.jsch.Session)

Aggregations

Session (com.jcraft.jsch.Session)1 Timeout (com.oracle.bedrock.options.Timeout)1 StrictHostChecking (com.oracle.bedrock.runtime.remote.options.StrictHostChecking)1 UserKnownHostsFile (com.oracle.bedrock.runtime.remote.options.UserKnownHostsFile)1 Properties (java.util.Properties)1