Search in sources :

Example 1 with DBASecureStorage

use of org.jkiss.dbeaver.model.app.DBASecureStorage 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 2 with DBASecureStorage

use of org.jkiss.dbeaver.model.app.DBASecureStorage 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)

Example 3 with DBASecureStorage

use of org.jkiss.dbeaver.model.app.DBASecureStorage in project dbeaver by serge-rider.

the class DataSourceSerializerModern method readSecuredCredentials.

private SecureCredentials readSecuredCredentials(@Nullable DataSourceDescriptor dataSource, @Nullable DBPConfigurationProfile profile, @Nullable String subNode) {
    assert dataSource != null || profile != null;
    SecureCredentials creds = new SecureCredentials();
    final DBASecureStorage secureStorage = dataSource == null ? registry.getProject().getSecureStorage() : dataSource.getProject().getSecureStorage();
    {
        try {
            if (secureStorage.useSecurePreferences()) {
                ISecurePreferences prefNode = dataSource == null ? secureStorage.getSecurePreferences() : dataSource.getSecurePreferences();
                if (subNode != null) {
                    for (String nodeName : subNode.split("/")) {
                        prefNode = prefNode.node(nodeName);
                    }
                }
                for (String key : prefNode.keys()) {
                    switch(key) {
                        case RegistryConstants.ATTR_USER:
                            creds.setUserName(prefNode.get(key, null));
                            break;
                        case RegistryConstants.ATTR_PASSWORD:
                            creds.setUserPassword(prefNode.get(key, null));
                            break;
                        default:
                            creds.setSecureProp(key, prefNode.get(key, null));
                            break;
                    }
                }
            }
        } catch (Throwable e) {
            // Most likely user canceled master password enter of failed by some other reason.
            // Anyhow we won't try it again
            log.error("Can't read password from secure storage", e);
            passwordReadCanceled = true;
        }
    }
    String topNodeId = profile != null ? "profile:" + profile.getProfileId() : dataSource.getId();
    if (subNode == null)
        subNode = NODE_CONNECTION;
    Map<String, Map<String, String>> subMap = secureProperties.get(topNodeId);
    if (subMap != null) {
        Map<String, String> propMap = subMap.get(subNode);
        if (propMap != null) {
            for (Map.Entry<String, String> prop : propMap.entrySet()) {
                switch(prop.getKey()) {
                    case RegistryConstants.ATTR_USER:
                        creds.setUserName(prop.getValue());
                        break;
                    case RegistryConstants.ATTR_PASSWORD:
                        creds.setUserPassword(prop.getValue());
                        break;
                    default:
                        creds.setSecureProp(prop.getKey(), prop.getValue());
                        break;
                }
            }
        }
    }
    return creds;
}
Also used : ISecurePreferences(org.eclipse.equinox.security.storage.ISecurePreferences) DBASecureStorage(org.jkiss.dbeaver.model.app.DBASecureStorage)

Aggregations

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