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);
}
}
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations