Search in sources :

Example 1 with StorageException

use of org.eclipse.equinox.security.storage.StorageException 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)

Aggregations

ISecurePreferences (org.eclipse.equinox.security.storage.ISecurePreferences)1 StorageException (org.eclipse.equinox.security.storage.StorageException)1 DBASecureStorage (org.jkiss.dbeaver.model.app.DBASecureStorage)1 EncryptionException (org.jkiss.dbeaver.registry.encode.EncryptionException)1