Search in sources :

Example 1 with AgentProxy

use of com.jcraft.jsch.agentproxy.AgentProxy in project dbeaver by serge-rider.

the class SSHImplementationAbstract method initTunnel.

@Override
public DBPConnectionConfiguration initTunnel(DBRProgressMonitor monitor, DBPPlatform platform, DBWHandlerConfiguration configuration, DBPConnectionConfiguration connectionInfo) throws DBException, IOException {
    String dbPortString = connectionInfo.getHostPort();
    if (CommonUtils.isEmpty(dbPortString)) {
        dbPortString = configuration.getDriver().getDefaultPort();
        if (CommonUtils.isEmpty(dbPortString)) {
            throw new DBException("Database port not specified and no default port number for driver '" + configuration.getDriver().getName() + "'");
        }
    }
    String sshAuthType = configuration.getStringProperty(SSHConstants.PROP_AUTH_TYPE);
    String sshHost = configuration.getStringProperty(DBWHandlerConfiguration.PROP_HOST);
    int sshPortNum = configuration.getIntProperty(DBWHandlerConfiguration.PROP_PORT);
    int aliveInterval = configuration.getIntProperty(SSHConstants.PROP_ALIVE_INTERVAL);
    int connectTimeout = configuration.getIntProperty(SSHConstants.PROP_CONNECT_TIMEOUT);
    String sshLocalHost = CommonUtils.toString(configuration.getProperty(SSHConstants.PROP_LOCAL_HOST));
    int sshLocalPort = configuration.getIntProperty(SSHConstants.PROP_LOCAL_PORT);
    String sshRemoteHost = CommonUtils.toString(configuration.getProperty(SSHConstants.PROP_REMOTE_HOST));
    int sshRemotePort = configuration.getIntProperty(SSHConstants.PROP_REMOTE_PORT);
    // String aliveCount = properties.get(SSHConstants.PROP_ALIVE_COUNT);
    if (CommonUtils.isEmpty(sshHost)) {
        throw new DBException("SSH host not specified");
    }
    if (sshPortNum == 0) {
        throw new DBException("SSH port not specified");
    }
    if (CommonUtils.isEmpty(configuration.getUserName())) {
        configuration.setUserName(System.getProperty(StandardConstants.ENV_USER_NAME));
    }
    if (sshLocalPort == 0) {
        if (savedLocalPort != 0) {
            sshLocalPort = savedLocalPort;
        } else if (platform != null) {
            sshLocalPort = SSHUtils.findFreePort(platform);
        }
    }
    if (CommonUtils.isEmpty(sshRemoteHost)) {
        sshRemoteHost = connectionInfo.getHostName();
    }
    if (sshRemotePort == 0 && configuration.getDriver() != null) {
        sshRemotePort = CommonUtils.toInt(connectionInfo.getHostPort());
    }
    SSHConstants.AuthType authType = SSHConstants.AuthType.PASSWORD;
    if (sshAuthType != null) {
        authType = SSHConstants.AuthType.valueOf(sshAuthType);
    }
    File privKeyFile = null;
    String privKeyPath = configuration.getStringProperty(SSHConstants.PROP_KEY_PATH);
    if (authType == SSHConstants.AuthType.PUBLIC_KEY) {
        if (CommonUtils.isEmpty(privKeyPath)) {
            throw new DBException("Private key path is empty");
        }
        privKeyFile = new File(privKeyPath);
        if (!privKeyFile.exists()) {
            throw new DBException("Private key file '" + privKeyFile.getAbsolutePath() + "' doesn't exist");
        }
    }
    if (authType == SSHConstants.AuthType.AGENT) {
        try {
            agentProxy = new AgentProxy(new PageantConnector());
            log.debug("SSH: Connected with pageant");
        } catch (Exception e) {
            log.debug("pageant connect exception", e);
        }
        if (agentProxy == null) {
            try {
                USocketFactory udsf = new JNAUSocketFactory();
                agentProxy = new AgentProxy(new SSHAgentConnector(udsf));
                log.debug("SSH: Connected with ssh-agent");
            } catch (Exception e) {
                log.debug("ssh-agent connection exception", e);
            }
        }
        if (agentProxy == null) {
            throw new DBException("Unable to initialize SSH agent");
        }
    }
    if (connectTimeout == 0) {
        connectTimeout = SSHConstants.DEFAULT_CONNECT_TIMEOUT;
    }
    monitor.subTask("Initiating tunnel at '" + sshHost + "'");
    setupTunnel(monitor, configuration, sshHost, aliveInterval, sshPortNum, privKeyFile, connectTimeout, sshLocalHost, sshLocalPort, sshRemoteHost, sshRemotePort);
    savedLocalPort = sshLocalPort;
    savedConfiguration = configuration;
    savedConnectionInfo = connectionInfo;
    connectionInfo = new DBPConnectionConfiguration(connectionInfo);
    // Replace database host/port and URL
    if (CommonUtils.isEmpty(sshLocalHost)) {
        connectionInfo.setHostName(SSHConstants.LOCALHOST_NAME);
    } else {
        connectionInfo.setHostName(sshLocalHost);
    }
    connectionInfo.setHostPort(Integer.toString(sshLocalPort));
    if (configuration.getDriver() != null) {
        // Driver can be null in case of orphan tunnel config (e.g. in network profile)
        String newURL = configuration.getDriver().getDataSourceProvider().getConnectionURL(configuration.getDriver(), connectionInfo);
        connectionInfo.setUrl(newURL);
    }
    return connectionInfo;
}
Also used : DBException(org.jkiss.dbeaver.DBException) AgentProxy(com.jcraft.jsch.agentproxy.AgentProxy) JNAUSocketFactory(com.jcraft.jsch.agentproxy.usocket.JNAUSocketFactory) IOException(java.io.IOException) DBException(org.jkiss.dbeaver.DBException) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) JNAUSocketFactory(com.jcraft.jsch.agentproxy.usocket.JNAUSocketFactory) USocketFactory(com.jcraft.jsch.agentproxy.USocketFactory) SSHAgentConnector(com.jcraft.jsch.agentproxy.connector.SSHAgentConnector) PageantConnector(com.jcraft.jsch.agentproxy.connector.PageantConnector) File(java.io.File)

Example 2 with AgentProxy

use of com.jcraft.jsch.agentproxy.AgentProxy in project dbeaver by dbeaver.

the class SSHImplementationAbstract method initTunnel.

@Override
public DBPConnectionConfiguration initTunnel(DBRProgressMonitor monitor, DBPPlatform platform, DBWHandlerConfiguration configuration, DBPConnectionConfiguration connectionInfo) throws DBException, IOException {
    String dbPortString = connectionInfo.getHostPort();
    if (CommonUtils.isEmpty(dbPortString)) {
        dbPortString = configuration.getDriver().getDefaultPort();
        if (CommonUtils.isEmpty(dbPortString)) {
            throw new DBException("Database port not specified and no default port number for driver '" + configuration.getDriver().getName() + "'");
        }
    }
    String sshAuthType = configuration.getStringProperty(SSHConstants.PROP_AUTH_TYPE);
    String sshHost = configuration.getStringProperty(DBWHandlerConfiguration.PROP_HOST);
    int sshPortNum = configuration.getIntProperty(DBWHandlerConfiguration.PROP_PORT);
    int aliveInterval = configuration.getIntProperty(SSHConstants.PROP_ALIVE_INTERVAL);
    int connectTimeout = configuration.getIntProperty(SSHConstants.PROP_CONNECT_TIMEOUT);
    String sshLocalHost = CommonUtils.toString(configuration.getProperty(SSHConstants.PROP_LOCAL_HOST));
    int sshLocalPort = configuration.getIntProperty(SSHConstants.PROP_LOCAL_PORT);
    String sshRemoteHost = CommonUtils.toString(configuration.getProperty(SSHConstants.PROP_REMOTE_HOST));
    int sshRemotePort = configuration.getIntProperty(SSHConstants.PROP_REMOTE_PORT);
    // String aliveCount = properties.get(SSHConstants.PROP_ALIVE_COUNT);
    if (CommonUtils.isEmpty(sshHost)) {
        throw new DBException("SSH host not specified");
    }
    if (sshPortNum == 0) {
        throw new DBException("SSH port not specified");
    }
    if (CommonUtils.isEmpty(configuration.getUserName())) {
        configuration.setUserName(System.getProperty(StandardConstants.ENV_USER_NAME));
    }
    if (sshLocalPort == 0) {
        if (savedLocalPort != 0) {
            sshLocalPort = savedLocalPort;
        } else if (platform != null) {
            sshLocalPort = SSHUtils.findFreePort(platform);
        }
    }
    if (CommonUtils.isEmpty(sshRemoteHost)) {
        sshRemoteHost = connectionInfo.getHostName();
    }
    if (sshRemotePort == 0 && configuration.getDriver() != null) {
        sshRemotePort = CommonUtils.toInt(connectionInfo.getHostPort());
    }
    SSHConstants.AuthType authType = SSHConstants.AuthType.PASSWORD;
    if (sshAuthType != null) {
        authType = SSHConstants.AuthType.valueOf(sshAuthType);
    }
    File privKeyFile = null;
    String privKeyPath = configuration.getStringProperty(SSHConstants.PROP_KEY_PATH);
    if (authType == SSHConstants.AuthType.PUBLIC_KEY) {
        if (CommonUtils.isEmpty(privKeyPath)) {
            throw new DBException("Private key path is empty");
        }
        privKeyFile = new File(privKeyPath);
        if (!privKeyFile.exists()) {
            throw new DBException("Private key file '" + privKeyFile.getAbsolutePath() + "' doesn't exist");
        }
    }
    if (authType == SSHConstants.AuthType.AGENT) {
        try {
            agentProxy = new AgentProxy(new PageantConnector());
            log.debug("SSH: Connected with pageant");
        } catch (Exception e) {
            log.debug("pageant connect exception", e);
        }
        if (agentProxy == null) {
            try {
                USocketFactory udsf = new JNAUSocketFactory();
                agentProxy = new AgentProxy(new SSHAgentConnector(udsf));
                log.debug("SSH: Connected with ssh-agent");
            } catch (Exception e) {
                log.debug("ssh-agent connection exception", e);
            }
        }
        if (agentProxy == null) {
            throw new DBException("Unable to initialize SSH agent");
        }
    }
    if (connectTimeout == 0) {
        connectTimeout = SSHConstants.DEFAULT_CONNECT_TIMEOUT;
    }
    monitor.subTask("Initiating tunnel at '" + sshHost + "'");
    setupTunnel(monitor, configuration, sshHost, aliveInterval, sshPortNum, privKeyFile, connectTimeout, sshLocalHost, sshLocalPort, sshRemoteHost, sshRemotePort);
    savedLocalPort = sshLocalPort;
    savedConfiguration = configuration;
    savedConnectionInfo = connectionInfo;
    connectionInfo = new DBPConnectionConfiguration(connectionInfo);
    // Replace database host/port and URL
    if (CommonUtils.isEmpty(sshLocalHost)) {
        connectionInfo.setHostName(SSHConstants.LOCALHOST_NAME);
    } else {
        connectionInfo.setHostName(sshLocalHost);
    }
    connectionInfo.setHostPort(Integer.toString(sshLocalPort));
    if (configuration.getDriver() != null) {
        // Driver can be null in case of orphan tunnel config (e.g. in network profile)
        String newURL = configuration.getDriver().getDataSourceProvider().getConnectionURL(configuration.getDriver(), connectionInfo);
        connectionInfo.setUrl(newURL);
    }
    return connectionInfo;
}
Also used : DBException(org.jkiss.dbeaver.DBException) AgentProxy(com.jcraft.jsch.agentproxy.AgentProxy) JNAUSocketFactory(com.jcraft.jsch.agentproxy.usocket.JNAUSocketFactory) IOException(java.io.IOException) DBException(org.jkiss.dbeaver.DBException) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) JNAUSocketFactory(com.jcraft.jsch.agentproxy.usocket.JNAUSocketFactory) USocketFactory(com.jcraft.jsch.agentproxy.USocketFactory) SSHAgentConnector(com.jcraft.jsch.agentproxy.connector.SSHAgentConnector) PageantConnector(com.jcraft.jsch.agentproxy.connector.PageantConnector) File(java.io.File)

Aggregations

AgentProxy (com.jcraft.jsch.agentproxy.AgentProxy)2 USocketFactory (com.jcraft.jsch.agentproxy.USocketFactory)2 PageantConnector (com.jcraft.jsch.agentproxy.connector.PageantConnector)2 SSHAgentConnector (com.jcraft.jsch.agentproxy.connector.SSHAgentConnector)2 JNAUSocketFactory (com.jcraft.jsch.agentproxy.usocket.JNAUSocketFactory)2 File (java.io.File)2 IOException (java.io.IOException)2 DBException (org.jkiss.dbeaver.DBException)2 DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)2