Search in sources :

Example 1 with ISecurePreferences

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

use of org.eclipse.equinox.security.storage.ISecurePreferences in project linuxtools by eclipse.

the class DefaultDockerConnectionStorageManager method loadConnections.

@Override
public List<IDockerConnection> loadConnections() {
    final List<IDockerConnection> connections = new ArrayList<>();
    final IPath stateLocation = Activator.getDefault().getStateLocation();
    final File connectionFile = stateLocation.append(CONNECTIONS_FILE_NAME).toFile();
    final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        if (connectionFile.exists()) {
            Document d = db.parse(connectionFile);
            Element e = d.getDocumentElement();
            // Get the stored configuration data
            // $NON-NLS-1$
            NodeList connectionNodes = e.getElementsByTagName("connection");
            for (int x = 0; x < connectionNodes.getLength(); ++x) {
                Node n = connectionNodes.item(x);
                NamedNodeMap attrs = n.getAttributes();
                // $NON-NLS-1$
                Node nameNode = attrs.getNamedItem("name");
                // $NON-NLS-1$
                Node uriNode = attrs.getNamedItem("uri");
                // $NON-NLS-1$
                Node usernameNode = attrs.getNamedItem("username");
                // $NON-NLS-1$
                Node certNode = attrs.getNamedItem("cert");
                if (uriNode != null) {
                    String uri = uriNode.getNodeValue();
                    String name = nameNode.getNodeValue();
                    if (usernameNode != null) {
                        String username = usernameNode.getNodeValue();
                        String key = DockerConnection.getPreferencesKey(uri, username);
                        ISecurePreferences root = SecurePreferencesFactory.getDefault();
                        ISecurePreferences node = root.node(key);
                        @SuppressWarnings("unused") String password;
                        try {
                            // $NON-NLS-1$
                            password = node.get("password", null);
                        } catch (StorageException e1) {
                            e1.printStackTrace();
                        }
                    }
                    final DockerConnection.Builder builder = new DockerConnection.Builder().name(name);
                    if (uri.startsWith("unix:")) {
                        // $NON-NLS-1$
                        final DockerConnection connection = builder.unixSocketConnection(new UnixSocketConnectionSettings(uri));
                        connections.add(connection);
                    } else {
                        final String pathToCertificates = certNode != null ? certNode.getNodeValue() : null;
                        final DockerConnection connection = builder.tcpConnection(new TCPConnectionSettings(uri, pathToCertificates));
                        connections.add(connection);
                    }
                }
            }
        }
    } catch (ParserConfigurationException | SAXException | IOException e) {
        Activator.log(e);
    }
    return connections;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) ISecurePreferences(org.eclipse.equinox.security.storage.ISecurePreferences) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) NamedNodeMap(org.w3c.dom.NamedNodeMap) IPath(org.eclipse.core.runtime.IPath) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) StorageException(org.eclipse.equinox.security.storage.StorageException)

Example 3 with ISecurePreferences

use of org.eclipse.equinox.security.storage.ISecurePreferences in project linuxtools by eclipse.

the class RegistryAccountInfo method getPassword.

@Override
public char[] getPassword() {
    if (password != null) {
        return password;
    }
    char[] password = null;
    final ISecurePreferences preferences = SecurePreferencesFactory.getDefault();
    final ISecurePreferences dockerNode = preferences.node(// $NON-NLS-1$
    "org.eclipse.linuxtools.docker.ui.accounts");
    final String key = getServerAddress() + "," + getUsername() + "," + getEmail();
    try {
        password = dockerNode.get(key, null) != null ? dockerNode.get(key, null).toCharArray() : null;
    } catch (StorageException e) {
    }
    return password;
}
Also used : ISecurePreferences(org.eclipse.equinox.security.storage.ISecurePreferences) StorageException(org.eclipse.equinox.security.storage.StorageException)

Example 4 with ISecurePreferences

use of org.eclipse.equinox.security.storage.ISecurePreferences in project linuxtools by eclipse.

the class RegistryAccountStorageManager method getAccounts.

public List<IRegistryAccount> getAccounts() {
    final List<IRegistryAccount> accounts = new ArrayList<>();
    final ISecurePreferences preferences = SecurePreferencesFactory.getDefault();
    final ISecurePreferences dockerNode = preferences.node(// $NON-NLS-1$
    "org.eclipse.linuxtools.docker.ui.accounts");
    for (String key : dockerNode.keys()) {
        // $NON-NLS-1$
        final String[] tokens = key.split(",");
        if (tokens.length > 1) {
            final String serverAddress = tokens[0];
            final String username = tokens[1];
            // $NON-NLS-1$
            final String email = tokens.length > 2 ? tokens[2] : "";
            final RegistryAccountInfo account = new RegistryAccountInfo(serverAddress, username, email, null, false);
            accounts.add(account);
        }
    }
    return accounts;
}
Also used : ArrayList(java.util.ArrayList) IRegistryAccount(org.eclipse.linuxtools.docker.core.IRegistryAccount) ISecurePreferences(org.eclipse.equinox.security.storage.ISecurePreferences)

Example 5 with ISecurePreferences

use of org.eclipse.equinox.security.storage.ISecurePreferences in project linuxtools by eclipse.

the class RegistryAccountStorageManager method add.

public void add(IRegistryAccount info) {
    final ISecurePreferences preferences = getDockerNode();
    final char[] password = info.getPassword();
    final String key = getKeyFor(info);
    try {
        preferences.put(key, (password != null ? new String(password) : null), true);
    } catch (StorageException e) {
        Activator.logErrorMessage("Failed to store Docker registry password", e);
    }
}
Also used : ISecurePreferences(org.eclipse.equinox.security.storage.ISecurePreferences) StorageException(org.eclipse.equinox.security.storage.StorageException)

Aggregations

ISecurePreferences (org.eclipse.equinox.security.storage.ISecurePreferences)46 StorageException (org.eclipse.equinox.security.storage.StorageException)13 Preferences (org.osgi.service.prefs.Preferences)5 IOException (java.io.IOException)4 DBASecureStorage (org.jkiss.dbeaver.model.app.DBASecureStorage)4 BackingStoreException (org.osgi.service.prefs.BackingStoreException)4 ArrayList (java.util.ArrayList)3 IStatus (org.eclipse.core.runtime.IStatus)3 Status (org.eclipse.core.runtime.Status)3 MdwSecurityException (com.centurylink.mdw.auth.MdwSecurityException)2 MdwAuthenticator (com.centurylink.mdw.designer.auth.MdwAuthenticator)2 User (com.centurylink.mdw.plugin.User)2 DiscoveryException (com.centurylink.mdw.plugin.designer.DiscoveryException)2 XmlException (org.apache.xmlbeans.XmlException)2 ResourceException (org.eclipse.core.internal.resources.ResourceException)2 CoreException (org.eclipse.core.runtime.CoreException)2 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)2 IDStoreException (org.eclipse.ecf.storage.IDStoreException)2 IIDEntry (org.eclipse.ecf.storage.IIDEntry)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2