Search in sources :

Example 96 with DBPConnectionConfiguration

use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by serge-rider.

the class ExasolConnectionPage method loadSettings.

@Override
public void loadSettings() {
    super.loadSettings();
    setImageDescriptor(EXASOL_LOGO_IMG);
    // Load values from new connection info
    DBPConnectionConfiguration connectionInfo = site.getActiveDataSource().getConnectionConfiguration();
    if (hostText != null) {
        if (!CommonUtils.isEmpty(connectionInfo.getHostName())) {
            hostText.setText(connectionInfo.getHostName());
        } else {
            hostText.setText("");
        }
    }
    if (portText != null) {
        if (!CommonUtils.isEmpty(connectionInfo.getHostPort())) {
            portText.setText(String.valueOf(connectionInfo.getHostPort()));
        } else if (site.getDriver().getDefaultPort() != null) {
            portText.setText(site.getDriver().getDefaultPort());
        } else {
            portText.setText("8563");
        }
    }
    if (usernameText != null) {
        usernameText.setText(CommonUtils.notEmpty(connectionInfo.getUserName()));
    }
    if (passwordText != null) {
        passwordText.setText(CommonUtils.notEmpty(connectionInfo.getUserPassword()));
    }
    String backupHostText = connectionInfo.getProviderProperty(ExasolConstants.DRV_BACKUP_HOST_LIST);
    if (!CommonUtils.isEmpty(backupHostText)) {
        this.backupHostLabel.setEnabled(true);
        this.backupHostText.setText(backupHostText);
        this.backupHostText.setEnabled(true);
        this.useBackupHostList.setSelection(true);
    } else {
        this.backupHostLabel.setEnabled(false);
        this.backupHostText.setEnabled(false);
        this.useBackupHostList.setSelection(false);
    }
    String encryptComm = connectionInfo.getProviderProperty(ExasolConstants.DRV_ENCRYPT);
    if (encryptComm != null) {
        if (encryptComm.equals("1"))
            this.encryptCommunication.setEnabled(true);
    }
}
Also used : DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)

Example 97 with DBPConnectionConfiguration

use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by serge-rider.

the class JDBCDataSource method openConnection.

protected Connection openConnection(@NotNull DBRProgressMonitor monitor, @NotNull String purpose) throws DBCException {
    // It MUST be a JDBC driver
    Driver driverInstance;
    try {
        driverInstance = getDriverInstance(monitor);
    } catch (DBException e) {
        throw new DBCConnectException("Can't create driver instance", e, this);
    }
    // Set properties
    Properties connectProps = new Properties();
    {
        // Use properties defined by datasource itself
        Map<String, String> internalProps = getInternalConnectionProperties(monitor, purpose);
        if (internalProps != null) {
            connectProps.putAll(internalProps);
        }
    }
    {
        // Use driver properties
        final Map<Object, Object> driverProperties = container.getDriver().getConnectionProperties();
        for (Map.Entry<Object, Object> prop : driverProperties.entrySet()) {
            connectProps.setProperty(CommonUtils.toString(prop.getKey()), CommonUtils.toString(prop.getValue()));
        }
    }
    DBPConnectionConfiguration connectionInfo = container.getActualConnectionConfiguration();
    for (Map.Entry<String, String> prop : connectionInfo.getProperties().entrySet()) {
        connectProps.setProperty(CommonUtils.toString(prop.getKey()), CommonUtils.toString(prop.getValue()));
    }
    if (!CommonUtils.isEmpty(connectionInfo.getUserName())) {
        connectProps.put(DBConstants.DATA_SOURCE_PROPERTY_USER, getConnectionUserName(connectionInfo));
    }
    if (!CommonUtils.isEmpty(connectionInfo.getUserPassword())) {
        connectProps.put(DBConstants.DATA_SOURCE_PROPERTY_PASSWORD, getConnectionUserPassword(connectionInfo));
    }
    // Obtain connection
    try {
        final String url = getConnectionURL(connectionInfo);
        if (driverInstance != null) {
            try {
                if (!driverInstance.acceptsURL(url)) {
                    // Just write a warning in log. Some drivers are poorly coded and always returns false here.
                    log.error("Bad URL: " + url);
                }
            } catch (Throwable e) {
                log.debug("Error in " + driverInstance.getClass().getName() + ".acceptsURL() - " + url, e);
            }
        }
        monitor.subTask("Connecting " + purpose);
        Connection connection;
        if (driverInstance == null) {
            connection = DriverManager.getConnection(url, connectProps);
        } else {
            connection = driverInstance.connect(url, connectProps);
        }
        if (connection == null) {
            throw new DBCException("Null connection returned");
        }
        // Set read-only flag
        if (container.isConnectionReadOnly() && !isConnectionReadOnlyBroken()) {
            connection.setReadOnly(true);
        }
        return connection;
    } catch (SQLException ex) {
        throw new DBCConnectException(ex.getMessage(), ex, this);
    } catch (DBCException ex) {
        throw ex;
    } catch (Throwable e) {
        throw new DBCConnectException("Unexpected driver error occurred while connecting to database", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) Properties(java.util.Properties) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) Map(java.util.Map)

Example 98 with DBPConnectionConfiguration

use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by serge-rider.

the class SSHTunnelImpl method initializeTunnel.

@Override
public DBPConnectionConfiguration initializeTunnel(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 dbHost = connectionInfo.getHostName();
    Map<String, String> properties = configuration.getProperties();
    String sshAuthType = properties.get(SSHConstants.PROP_AUTH_TYPE);
    String sshHost = properties.get(SSHConstants.PROP_HOST);
    String sshPort = properties.get(SSHConstants.PROP_PORT);
    String sshUser = configuration.getUserName();
    String aliveInterval = properties.get(SSHConstants.PROP_ALIVE_INTERVAL);
    String connectTimeoutString = properties.get(SSHConstants.PROP_CONNECT_TIMEOUT);
    //String aliveCount = properties.get(SSHConstants.PROP_ALIVE_COUNT);
    if (CommonUtils.isEmpty(sshHost)) {
        throw new DBException("SSH host not specified");
    }
    if (CommonUtils.isEmpty(sshPort)) {
        throw new DBException("SSH port not specified");
    }
    if (CommonUtils.isEmpty(sshUser)) {
        throw new DBException("SSH user not specified");
    }
    int sshPortNum;
    try {
        sshPortNum = Integer.parseInt(sshPort);
    } catch (NumberFormatException e) {
        throw new DBException("Invalid SSH port: " + sshPort);
    }
    SSHConstants.AuthType authType = SSHConstants.AuthType.PASSWORD;
    if (sshAuthType != null) {
        authType = SSHConstants.AuthType.valueOf(sshAuthType);
    }
    File privKeyFile = null;
    String privKeyPath = properties.get(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");
        }
    }
    int connectTimeout;
    try {
        connectTimeout = Integer.parseInt(connectTimeoutString);
    } catch (NumberFormatException e) {
        connectTimeout = SSHConstants.DEFAULT_CONNECT_TIMEOUT;
    }
    monitor.subTask("Initiating tunnel at '" + sshHost + "'");
    UserInfo ui = new UIUserInfo(configuration);
    int dbPort;
    try {
        dbPort = Integer.parseInt(dbPortString);
    } catch (NumberFormatException e) {
        throw new DBException("Bad database port number: " + dbPortString);
    }
    int localPort = savedLocalPort;
    if (platform != null) {
        localPort = findFreePort(platform);
    }
    try {
        if (jsch == null) {
            jsch = new JSch();
            JSch.setLogger(new LoggerProxy());
        }
        if (privKeyFile != null) {
            if (!CommonUtils.isEmpty(ui.getPassphrase())) {
                jsch.addIdentity(privKeyFile.getAbsolutePath(), ui.getPassphrase());
            } else {
                jsch.addIdentity(privKeyFile.getAbsolutePath());
            }
        }
        log.debug("Instantiate SSH tunnel");
        session = jsch.getSession(sshUser, sshHost, sshPortNum);
        session.setConfig("StrictHostKeyChecking", "no");
        //session.setConfig("PreferredAuthentications", "password,publickey,keyboard-interactive");
        session.setConfig("PreferredAuthentications", privKeyFile != null ? "publickey" : "password");
        session.setConfig("ConnectTimeout", String.valueOf(connectTimeout));
        session.setUserInfo(ui);
        if (!CommonUtils.isEmpty(aliveInterval)) {
            session.setServerAliveInterval(Integer.parseInt(aliveInterval));
        }
        log.debug("Connect to tunnel host");
        session.connect(connectTimeout);
        try {
            session.setPortForwardingL(localPort, dbHost, dbPort);
        } catch (JSchException e) {
            closeTunnel(monitor);
            throw e;
        }
    } catch (JSchException e) {
        throw new DBException("Cannot establish tunnel", e);
    }
    savedLocalPort = localPort;
    savedConfiguration = configuration;
    savedConnectionInfo = connectionInfo;
    connectionInfo = new DBPConnectionConfiguration(connectionInfo);
    String newPortValue = String.valueOf(localPort);
    // Replace database host/port and URL - let's use localhost
    connectionInfo.setHostName(LOCALHOST_NAME);
    connectionInfo.setHostPort(newPortValue);
    String newURL = configuration.getDriver().getDataSourceProvider().getConnectionURL(configuration.getDriver(), connectionInfo);
    connectionInfo.setUrl(newURL);
    return connectionInfo;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) File(java.io.File)

Example 99 with DBPConnectionConfiguration

use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by dbeaver.

the class ExasolWizardPageSettings method createSecurityGroup.

public void createSecurityGroup(Composite parent) {
    try {
        final SecuredPasswordEncrypter encrypter = new SecuredPasswordEncrypter();
        final DBPConnectionConfiguration connectionInfo = wizard.getConnectionInfo();
        final String authProperty = DBConstants.INTERNAL_PROP_PREFIX + "-auth-" + wizard.getObjectsName() + "@";
        String authUser = null;
        String authPassword = null;
        {
            String authValue = connectionInfo.getProperty(authProperty);
            if (authValue != null) {
                String authCredentials = encrypter.decrypt(authValue);
                int divPos = authCredentials.indexOf(':');
                if (divPos != -1) {
                    authUser = authCredentials.substring(0, divPos);
                    authPassword = authCredentials.substring(divPos + 1);
                }
            }
        }
        wizard.setToolUserName(authUser == null ? connectionInfo.getUserName() : authUser);
        wizard.setToolUserPassword(authPassword == null ? connectionInfo.getUserPassword() : authPassword);
        final boolean savePassword = authUser != null;
        Group securityGroup = UIUtils.createControlGroup(parent, "Security", 2, GridData.HORIZONTAL_ALIGN_BEGINNING, 0);
        Label infoLabel = new Label(securityGroup, SWT.NONE);
        infoLabel.setText("Override user credentials (" + wizard.getConnectionInfo().getUserName() + ") for objects '" + wizard.getObjectsName() + "'.\nExternal tools like 'mysqldump' may require different set of permissions.");
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 2;
        infoLabel.setLayoutData(gd);
        Button authButton = new Button(securityGroup, SWT.PUSH);
        authButton.setText("Authentication");
        authButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                BaseAuthDialog authDialog = new BaseAuthDialog(getShell(), "Authentication", false);
                authDialog.setUserName(wizard.getToolUserName());
                authDialog.setUserPassword(wizard.getToolUserPassword());
                authDialog.setSavePassword(savePassword);
                if (authDialog.open() == IDialogConstants.OK_ID) {
                    wizard.setToolUserName(authDialog.getUserName());
                    wizard.setToolUserPassword(authDialog.getUserPassword());
                    if (authDialog.isSavePassword()) {
                        try {
                            connectionInfo.setProperty(authProperty, encrypter.encrypt(wizard.getToolUserName() + ':' + wizard.getToolUserPassword()));
                        } catch (EncryptionException e1) {
                        // Never be here
                        }
                    }
                }
            }
        });
        Button resetButton = new Button(securityGroup, SWT.PUSH);
        resetButton.setText("Reset to default");
        resetButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                connectionInfo.getProperties().remove(authProperty);
                wizard.setToolUserName(connectionInfo.getUserName());
                wizard.setToolUserPassword(connectionInfo.getUserPassword());
            }
        });
    } catch (EncryptionException e) {
    // Never be here
    }
}
Also used : Group(org.eclipse.swt.widgets.Group) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) SecuredPasswordEncrypter(org.jkiss.dbeaver.registry.encode.SecuredPasswordEncrypter) Button(org.eclipse.swt.widgets.Button) BaseAuthDialog(org.jkiss.dbeaver.ui.dialogs.connection.BaseAuthDialog) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) EncryptionException(org.jkiss.dbeaver.registry.encode.EncryptionException)

Example 100 with DBPConnectionConfiguration

use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by dbeaver.

the class ExasolConnectionPage method saveSettings.

@Override
public void saveSettings(DBPDataSourceContainer dataSource) {
    DBPConnectionConfiguration connectionInfo = dataSource.getConnectionConfiguration();
    if (hostText != null) {
        connectionInfo.setHostName(hostText.getText().trim());
    }
    if (portText != null) {
        connectionInfo.setHostPort(portText.getText().trim());
    }
    if (usernameText != null) {
        connectionInfo.setUserName(usernameText.getText().trim());
    }
    if (passwordText != null) {
        connectionInfo.setUserPassword(passwordText.getText());
    }
    if (homesSelector != null) {
        connectionInfo.setClientHomeId(homesSelector.getSelectedHome());
    }
    connectionInfo.setProviderProperty(ExasolConstants.DRV_BACKUP_HOST_LIST, backupHostText.getText());
    if (this.encryptCommunication.getSelection())
        connectionInfo.setProviderProperty(ExasolConstants.DRV_ENCRYPT, "1");
    super.saveSettings(dataSource);
}
Also used : DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)

Aggregations

DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)172 DBException (org.jkiss.dbeaver.DBException)25 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)18 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)16 DBPDriver (org.jkiss.dbeaver.model.connection.DBPDriver)14 DBWHandlerConfiguration (org.jkiss.dbeaver.model.net.DBWHandlerConfiguration)12 ArrayList (java.util.ArrayList)10 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)10 SelectionEvent (org.eclipse.swt.events.SelectionEvent)10 GridData (org.eclipse.swt.layout.GridData)10 DBPDataSourceRegistry (org.jkiss.dbeaver.model.app.DBPDataSourceRegistry)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 Button (org.eclipse.swt.widgets.Button)8 Group (org.eclipse.swt.widgets.Group)8 Label (org.eclipse.swt.widgets.Label)8 DBPDataSourceFolder (org.jkiss.dbeaver.model.DBPDataSourceFolder)7 Map (java.util.Map)6 Properties (java.util.Properties)6 DriverDescriptor (org.jkiss.dbeaver.registry.driver.DriverDescriptor)6 File (java.io.File)5