Search in sources :

Example 1 with EncryptionException

use of org.jkiss.dbeaver.registry.encode.EncryptionException in project dbeaver by serge-rider.

the class PostgreWizardPageSettings 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.getProviderProperty(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 pg_dump'.");
        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.setProviderProperty(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.getProviderProperties().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 2 with EncryptionException

use of org.jkiss.dbeaver.registry.encode.EncryptionException in project dbeaver by serge-rider.

the class DataSourceRegistry method saveSecuredCredentials.

private void saveSecuredCredentials(XMLBuilder xml, DataSourceDescriptor dataSource, String subNode, String userName, String password) throws IOException {
    boolean saved = false;
    final DBASecureStorage secureStorage = getPlatform().getSecureStorage();
    {
        try {
            ISecurePreferences prefNode = dataSource.getSecurePreferences();
            if (!secureStorage.useSecurePreferences()) {
                prefNode.removeNode();
            } else {
                if (subNode != null) {
                    for (String nodeName : subNode.split("/")) {
                        prefNode = prefNode.node(nodeName);
                    }
                }
                prefNode.put("name", dataSource.getName(), false);
                if (!CommonUtils.isEmpty(userName)) {
                    prefNode.put(RegistryConstants.ATTR_USER, userName, true);
                    saved = true;
                } else {
                    prefNode.remove(RegistryConstants.ATTR_USER);
                }
                if (!CommonUtils.isEmpty(password)) {
                    prefNode.put(RegistryConstants.ATTR_PASSWORD, password, true);
                    saved = true;
                } else {
                    prefNode.remove(RegistryConstants.ATTR_PASSWORD);
                }
            }
        } catch (StorageException e) {
            log.error("Can't save password in secure storage", e);
        }
    }
    if (!saved) {
        try {
            if (!CommonUtils.isEmpty(userName)) {
                xml.addAttribute(RegistryConstants.ATTR_USER, CommonUtils.notEmpty(userName));
            }
            if (!CommonUtils.isEmpty(password)) {
                xml.addAttribute(RegistryConstants.ATTR_PASSWORD, ENCRYPTOR.encrypt(password));
            }
        } catch (EncryptionException e) {
            log.error("Error encrypting password", e);
        }
    }
}
Also used : EncryptionException(org.jkiss.dbeaver.registry.encode.EncryptionException) ISecurePreferences(org.eclipse.equinox.security.storage.ISecurePreferences) DBASecureStorage(org.jkiss.dbeaver.model.app.DBASecureStorage) StorageException(org.eclipse.equinox.security.storage.StorageException)

Example 3 with EncryptionException

use of org.jkiss.dbeaver.registry.encode.EncryptionException in project dbeaver by dbeaver.

the class MySQLWizardPageSettings 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.getProviderProperty(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.setProviderProperty(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 4 with EncryptionException

use of org.jkiss.dbeaver.registry.encode.EncryptionException in project dbeaver by dbeaver.

the class PrefPageDrivers method performDefaults.

@Override
protected void performDefaults() {
    DBPPreferenceStore store = DBeaverCore.getGlobalPreferenceStore();
    versionUpdateCheck.setSelection(store.getBoolean(DBeaverPreferences.UI_DRIVERS_VERSION_UPDATE));
    proxyHostText.setText(store.getString(DBeaverPreferences.UI_PROXY_HOST));
    proxyPortSpinner.setSelection(store.getInt(DBeaverPreferences.UI_PROXY_PORT));
    proxyUserText.setText(store.getString(DBeaverPreferences.UI_PROXY_USER));
    // Load and decrypt password
    String passwordString = store.getString(DBeaverPreferences.UI_PROXY_PASSWORD);
    if (!CommonUtils.isEmpty(passwordString) && encrypter != null) {
        try {
            passwordString = encrypter.decrypt(passwordString);
        } catch (EncryptionException e) {
            log.warn(e);
        }
    }
    proxyPasswordText.setText(passwordString);
    customDriversHome.setText(DriverDescriptor.getCustomDriversHome().getAbsolutePath());
    for (String source : DriverDescriptor.getDriversSources()) {
        sourceList.add(source);
    }
    super.performDefaults();
}
Also used : EncryptionException(org.jkiss.dbeaver.registry.encode.EncryptionException) DBPPreferenceStore(org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)

Example 5 with EncryptionException

use of org.jkiss.dbeaver.registry.encode.EncryptionException in project dbeaver by dbeaver.

the class DataSourceRegistry method saveSecuredCredentials.

private void saveSecuredCredentials(XMLBuilder xml, DataSourceDescriptor dataSource, String subNode, String userName, String password) throws IOException {
    boolean saved = false;
    final DBASecureStorage secureStorage = getPlatform().getSecureStorage();
    {
        try {
            ISecurePreferences prefNode = dataSource.getSecurePreferences();
            if (!secureStorage.useSecurePreferences()) {
                prefNode.removeNode();
            } else {
                if (subNode != null) {
                    for (String nodeName : subNode.split("/")) {
                        prefNode = prefNode.node(nodeName);
                    }
                }
                prefNode.put("name", dataSource.getName(), false);
                if (!CommonUtils.isEmpty(userName)) {
                    prefNode.put(RegistryConstants.ATTR_USER, userName, true);
                    saved = true;
                } else {
                    prefNode.remove(RegistryConstants.ATTR_USER);
                }
                if (!CommonUtils.isEmpty(password)) {
                    prefNode.put(RegistryConstants.ATTR_PASSWORD, password, true);
                    saved = true;
                } else {
                    prefNode.remove(RegistryConstants.ATTR_PASSWORD);
                }
            }
        } catch (Throwable e) {
            log.error("Can't save password in secure storage", e);
        }
    }
    if (!saved) {
        try {
            if (!CommonUtils.isEmpty(userName)) {
                xml.addAttribute(RegistryConstants.ATTR_USER, CommonUtils.notEmpty(userName));
            }
            if (!CommonUtils.isEmpty(password)) {
                xml.addAttribute(RegistryConstants.ATTR_PASSWORD, ENCRYPTOR.encrypt(password));
            }
        } catch (EncryptionException e) {
            log.error("Error encrypting password", e);
        }
    }
}
Also used : EncryptionException(org.jkiss.dbeaver.registry.encode.EncryptionException) ISecurePreferences(org.eclipse.equinox.security.storage.ISecurePreferences) DBASecureStorage(org.jkiss.dbeaver.model.app.DBASecureStorage)

Aggregations

EncryptionException (org.jkiss.dbeaver.registry.encode.EncryptionException)9 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)5 SelectionEvent (org.eclipse.swt.events.SelectionEvent)5 GridData (org.eclipse.swt.layout.GridData)5 Button (org.eclipse.swt.widgets.Button)5 Group (org.eclipse.swt.widgets.Group)5 Label (org.eclipse.swt.widgets.Label)5 DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)5 SecuredPasswordEncrypter (org.jkiss.dbeaver.registry.encode.SecuredPasswordEncrypter)5 BaseAuthDialog (org.jkiss.dbeaver.ui.dialogs.connection.BaseAuthDialog)5 ISecurePreferences (org.eclipse.equinox.security.storage.ISecurePreferences)2 DBASecureStorage (org.jkiss.dbeaver.model.app.DBASecureStorage)2 DBPPreferenceStore (org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)2 StorageException (org.eclipse.equinox.security.storage.StorageException)1