Search in sources :

Example 1 with SecuredPasswordEncrypter

use of org.jkiss.dbeaver.runtime.encode.SecuredPasswordEncrypter in project dbeaver by dbeaver.

the class PostgreToolWizardPageSettings method createSecurityGroup.

public void createSecurityGroup(Composite parent) {
    try {
        final SecuredPasswordEncrypter encrypter = new SecuredPasswordEncrypter();
        final DBPConnectionConfiguration connectionInfo = wizard.getSettings().getDataSourceContainer().getActualConnectionConfiguration();
        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);
                }
            }
        }
        final boolean savePassword = authUser != null;
        Group securityGroup = UIUtils.createControlGroup(parent, PostgreMessages.wizard_backup_page_setting_group_security, 2, GridData.HORIZONTAL_ALIGN_BEGINNING, 0);
        Label infoLabel = new Label(securityGroup, SWT.NONE);
        infoLabel.setText(NLS.bind(PostgreMessages.wizard_backup_page_setting_group_security_label_info, connectionInfo.getUserName(), wizard.getObjectsName()));
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 2;
        infoLabel.setLayoutData(gd);
        Button authButton = new Button(securityGroup, SWT.PUSH);
        authButton.setText(PostgreMessages.wizard_backup_page_setting_group_security_btn_authentication);
        authButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                BaseAuthDialog authDialog = new BaseAuthDialog(getShell(), PostgreMessages.wizard_backup_page_setting_group_security_btn_authentication, false, true);
                authDialog.setUserName(wizard.getSettings().getToolUserName());
                authDialog.setUserPassword(wizard.getSettings().getToolUserPassword());
                authDialog.setSavePassword(savePassword);
                authDialog.setSavePasswordText(PostgreMessages.wizard_backup_page_setting_authentication_save_password);
                authDialog.setSavePasswordToolTipText(PostgreMessages.wizard_backup_page_setting_authentication_save_password_tip);
                if (authDialog.open() == IDialogConstants.OK_ID) {
                    wizard.getSettings().setToolUserName(authDialog.getUserName());
                    wizard.getSettings().setToolUserPassword(authDialog.getUserPassword());
                }
            }
        });
        Button resetButton = new Button(securityGroup, SWT.PUSH);
        resetButton.setText(PostgreMessages.wizard_backup_page_setting_group_security_btn_reset_default);
        resetButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                connectionInfo.getProviderProperties().remove(authProperty);
                wizard.getSettings().setToolUserName(null);
                wizard.getSettings().setToolUserPassword(null);
            }
        });
    } 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.runtime.encode.SecuredPasswordEncrypter) Button(org.eclipse.swt.widgets.Button) BaseAuthDialog(org.jkiss.dbeaver.ui.dialogs.BaseAuthDialog) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) EncryptionException(org.jkiss.dbeaver.runtime.encode.EncryptionException)

Example 2 with SecuredPasswordEncrypter

use of org.jkiss.dbeaver.runtime.encode.SecuredPasswordEncrypter in project dbeaver by dbeaver.

the class AbstractNativeToolSettings method loadSettings.

public void loadSettings(DBRRunnableContext runnableContext, DBPPreferenceStore preferenceStore) throws DBException {
    if (dataSourceContainer == null && !CommonUtils.isEmpty(databaseObjects)) {
        BASE_OBJECT baseObject = databaseObjects.get(0);
        dataSourceContainer = baseObject instanceof DBPDataSourceContainer ? (DBPDataSourceContainer) baseObject : baseObject.getDataSource().getContainer();
    }
    if (dataSourceContainer == null) {
        String dsID = preferenceStore.getString("dataSource");
        if (!CommonUtils.isEmpty(dsID)) {
            String projectName = preferenceStore.getString("project");
            DBPProject project = CommonUtils.isEmpty(projectName) ? null : DBWorkbench.getPlatform().getWorkspace().getProject(projectName);
            if (project == null) {
                if (!CommonUtils.isEmpty(projectName)) {
                    log.error("Can't find project '" + projectName + "' for tool configuration");
                }
                project = DBWorkbench.getPlatform().getWorkspace().getActiveProject();
            }
            dataSourceContainer = project.getDataSourceRegistry().getDataSource(dsID);
            if (dataSourceContainer == null) {
                log.error("Can't find datasource '" + dsID + "' in project '" + project.getName() + "' for tool configuration");
            }
        }
    }
    if (preferenceStore instanceof DBPPreferenceMap && dataSourceContainer != null) {
        List<String> databaseObjectList = ((DBPPreferenceMap) preferenceStore).getObject("databaseObjects");
        if (!CommonUtils.isEmpty(databaseObjectList)) {
            DBPProject finalProject = dataSourceContainer.getProject();
            try {
                runnableContext.run(true, true, monitor -> {
                    monitor.beginTask("Load database object list", databaseObjectList.size());
                    try {
                        for (String objectId : databaseObjectList) {
                            monitor.subTask("Load " + objectId);
                            try {
                                DBSObject object = DBUtils.findObjectById(monitor, finalProject, objectId);
                                if (object != null) {
                                    databaseObjects.add((BASE_OBJECT) object);
                                    dataSourceContainer = object instanceof DBPDataSourceContainer ? (DBPDataSourceContainer) object : object.getDataSource().getContainer();
                                }
                            } catch (Throwable e) {
                                throw new DBException("Can't find database object '" + objectId + "' in project '" + finalProject.getName() + "' for task configuration", e);
                            }
                            monitor.worked(1);
                        }
                    } catch (Exception e) {
                        throw new InvocationTargetException(e);
                    } finally {
                        monitor.done();
                    }
                });
            } catch (InvocationTargetException e) {
                throw new DBException("Error loading objects configuration", e.getTargetException());
            } catch (InterruptedException e) {
            // Ignore
            }
        }
    }
    extraCommandArgs = preferenceStore.getString(PROP_NAME_EXTRA_ARGS);
    clientHomeName = preferenceStore.getString("clientHomeName");
    if (preferenceStore instanceof DBPPreferenceMap) {
        toolUserName = preferenceStore.getString("tool.user");
        toolUserPassword = preferenceStore.getString("tool.password");
        try {
            final SecuredPasswordEncrypter encrypter = new SecuredPasswordEncrypter();
            if (!CommonUtils.isEmpty(toolUserName))
                toolUserName = encrypter.decrypt(toolUserName);
            if (!CommonUtils.isEmpty(toolUserPassword))
                toolUserPassword = encrypter.decrypt(toolUserPassword);
        } catch (Exception e) {
            throw new DBException("Error decrypting user credentials", e);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPPreferenceMap(org.jkiss.dbeaver.model.preferences.DBPPreferenceMap) SecuredPasswordEncrypter(org.jkiss.dbeaver.runtime.encode.SecuredPasswordEncrypter) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 3 with SecuredPasswordEncrypter

use of org.jkiss.dbeaver.runtime.encode.SecuredPasswordEncrypter in project dbeaver by dbeaver.

the class AbstractNativeToolSettings method saveSettings.

public void saveSettings(DBRRunnableContext runnableContext, DBPPreferenceStore preferenceStore) {
    preferenceStore.setValue("project", getProject().getName());
    if (dataSourceContainer != null) {
        preferenceStore.setValue("dataSource", dataSourceContainer.getId());
    }
    if (preferenceStore instanceof DBPPreferenceMap) {
        // Save input objects to task properties
        Map<String, Object> propertyMap = ((DBPPreferenceMap) preferenceStore).getPropertyMap();
        List<String> objectList = new ArrayList<>();
        for (BASE_OBJECT object : databaseObjects) {
            objectList.add(DBUtils.getObjectFullId(object));
        }
        propertyMap.put("databaseObjects", objectList);
        try {
            final SecuredPasswordEncrypter encrypter = new SecuredPasswordEncrypter();
            if (!CommonUtils.isEmpty(toolUserName)) {
                propertyMap.put("tool.user", encrypter.encrypt(toolUserName));
            } else {
                propertyMap.put("tool.user", "");
            }
            if (!CommonUtils.isEmpty(toolUserPassword)) {
                propertyMap.put("tool.password", encrypter.encrypt(toolUserPassword));
            } else {
                propertyMap.put("tool.password", "");
            }
        } catch (Exception e) {
            log.debug(e);
        }
    }
    preferenceStore.setValue(PROP_NAME_EXTRA_ARGS, extraCommandArgs);
    if (clientHomeName != null) {
        preferenceStore.setValue("clientHomeName", clientHomeName);
    }
}
Also used : DBPPreferenceMap(org.jkiss.dbeaver.model.preferences.DBPPreferenceMap) SecuredPasswordEncrypter(org.jkiss.dbeaver.runtime.encode.SecuredPasswordEncrypter) ArrayList(java.util.ArrayList) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException)

Example 4 with SecuredPasswordEncrypter

use of org.jkiss.dbeaver.runtime.encode.SecuredPasswordEncrypter in project dbeaver by serge-rider.

the class AbstractNativeToolSettings method saveSettings.

public void saveSettings(DBRRunnableContext runnableContext, DBPPreferenceStore preferenceStore) {
    preferenceStore.setValue("project", getProject().getName());
    if (dataSourceContainer != null) {
        preferenceStore.setValue("dataSource", dataSourceContainer.getId());
    }
    if (preferenceStore instanceof DBPPreferenceMap) {
        // Save input objects to task properties
        Map<String, Object> propertyMap = ((DBPPreferenceMap) preferenceStore).getPropertyMap();
        List<String> objectList = new ArrayList<>();
        for (BASE_OBJECT object : databaseObjects) {
            objectList.add(DBUtils.getObjectFullId(object));
        }
        propertyMap.put("databaseObjects", objectList);
        try {
            final SecuredPasswordEncrypter encrypter = new SecuredPasswordEncrypter();
            if (!CommonUtils.isEmpty(toolUserName)) {
                propertyMap.put("tool.user", encrypter.encrypt(toolUserName));
            } else {
                propertyMap.put("tool.user", "");
            }
            if (!CommonUtils.isEmpty(toolUserPassword)) {
                propertyMap.put("tool.password", encrypter.encrypt(toolUserPassword));
            } else {
                propertyMap.put("tool.password", "");
            }
        } catch (Exception e) {
            log.debug(e);
        }
    }
    preferenceStore.setValue(PROP_NAME_EXTRA_ARGS, extraCommandArgs);
    if (clientHomeName != null) {
        preferenceStore.setValue("clientHomeName", clientHomeName);
    }
}
Also used : DBPPreferenceMap(org.jkiss.dbeaver.model.preferences.DBPPreferenceMap) SecuredPasswordEncrypter(org.jkiss.dbeaver.runtime.encode.SecuredPasswordEncrypter) ArrayList(java.util.ArrayList) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException)

Example 5 with SecuredPasswordEncrypter

use of org.jkiss.dbeaver.runtime.encode.SecuredPasswordEncrypter in project dbeaver by serge-rider.

the class AbstractNativeToolSettings method loadSettings.

public void loadSettings(DBRRunnableContext runnableContext, DBPPreferenceStore preferenceStore) throws DBException {
    if (dataSourceContainer == null && !CommonUtils.isEmpty(databaseObjects)) {
        BASE_OBJECT baseObject = databaseObjects.get(0);
        dataSourceContainer = baseObject instanceof DBPDataSourceContainer ? (DBPDataSourceContainer) baseObject : baseObject.getDataSource().getContainer();
    }
    if (dataSourceContainer == null) {
        String dsID = preferenceStore.getString("dataSource");
        if (!CommonUtils.isEmpty(dsID)) {
            String projectName = preferenceStore.getString("project");
            DBPProject project = CommonUtils.isEmpty(projectName) ? null : DBWorkbench.getPlatform().getWorkspace().getProject(projectName);
            if (project == null) {
                if (!CommonUtils.isEmpty(projectName)) {
                    log.error("Can't find project '" + projectName + "' for tool configuration");
                }
                project = DBWorkbench.getPlatform().getWorkspace().getActiveProject();
            }
            dataSourceContainer = project.getDataSourceRegistry().getDataSource(dsID);
            if (dataSourceContainer == null) {
                log.error("Can't find datasource '" + dsID + "' in project '" + project.getName() + "' for tool configuration");
            }
        }
    }
    if (preferenceStore instanceof DBPPreferenceMap && dataSourceContainer != null) {
        List<String> databaseObjectList = ((DBPPreferenceMap) preferenceStore).getObject("databaseObjects");
        if (!CommonUtils.isEmpty(databaseObjectList)) {
            DBPProject finalProject = dataSourceContainer.getProject();
            try {
                runnableContext.run(true, true, monitor -> {
                    monitor.beginTask("Load database object list", databaseObjectList.size());
                    try {
                        for (String objectId : databaseObjectList) {
                            monitor.subTask("Load " + objectId);
                            try {
                                DBSObject object = DBUtils.findObjectById(monitor, finalProject, objectId);
                                if (object != null) {
                                    databaseObjects.add((BASE_OBJECT) object);
                                    dataSourceContainer = object instanceof DBPDataSourceContainer ? (DBPDataSourceContainer) object : object.getDataSource().getContainer();
                                }
                            } catch (Throwable e) {
                                throw new DBException("Can't find database object '" + objectId + "' in project '" + finalProject.getName() + "' for task configuration", e);
                            }
                            monitor.worked(1);
                        }
                    } catch (Exception e) {
                        throw new InvocationTargetException(e);
                    } finally {
                        monitor.done();
                    }
                });
            } catch (InvocationTargetException e) {
                throw new DBException("Error loading objects configuration", e.getTargetException());
            } catch (InterruptedException e) {
            // Ignore
            }
        }
    }
    extraCommandArgs = preferenceStore.getString(PROP_NAME_EXTRA_ARGS);
    clientHomeName = preferenceStore.getString("clientHomeName");
    if (preferenceStore instanceof DBPPreferenceMap) {
        toolUserName = preferenceStore.getString("tool.user");
        toolUserPassword = preferenceStore.getString("tool.password");
        try {
            final SecuredPasswordEncrypter encrypter = new SecuredPasswordEncrypter();
            if (!CommonUtils.isEmpty(toolUserName))
                toolUserName = encrypter.decrypt(toolUserName);
            if (!CommonUtils.isEmpty(toolUserPassword))
                toolUserPassword = encrypter.decrypt(toolUserPassword);
        } catch (Exception e) {
            throw new DBException("Error decrypting user credentials", e);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPPreferenceMap(org.jkiss.dbeaver.model.preferences.DBPPreferenceMap) SecuredPasswordEncrypter(org.jkiss.dbeaver.runtime.encode.SecuredPasswordEncrypter) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Aggregations

SecuredPasswordEncrypter (org.jkiss.dbeaver.runtime.encode.SecuredPasswordEncrypter)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 DBException (org.jkiss.dbeaver.DBException)4 DBPPreferenceMap (org.jkiss.dbeaver.model.preferences.DBPPreferenceMap)4 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)4 ArrayList (java.util.ArrayList)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 GridData (org.eclipse.swt.layout.GridData)2 Button (org.eclipse.swt.widgets.Button)2 Group (org.eclipse.swt.widgets.Group)2 Label (org.eclipse.swt.widgets.Label)2 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)2 DBPProject (org.jkiss.dbeaver.model.app.DBPProject)2 DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)2 EncryptionException (org.jkiss.dbeaver.runtime.encode.EncryptionException)2 BaseAuthDialog (org.jkiss.dbeaver.ui.dialogs.BaseAuthDialog)2