diff options
author | Sam Davis | 2014-01-30 21:55:41 +0000 |
---|---|---|
committer | Sam Davis | 2014-02-18 22:47:19 +0000 |
commit | bc7ac9f9f49fb5d672ab6845faf1223b8f09eac5 (patch) | |
tree | 97f696bc5205d237213eb57c4584c88ff56702b6 /org.eclipse.mylyn.tasks.core | |
parent | e5b2bc7f021b1195509905f3397bb5086cecb167 (diff) | |
download | org.eclipse.mylyn.tasks-bc7ac9f9f49fb5d672ab6845faf1223b8f09eac5.tar.gz org.eclipse.mylyn.tasks-bc7ac9f9f49fb5d672ab6845faf1223b8f09eac5.tar.xz org.eclipse.mylyn.tasks-bc7ac9f9f49fb5d672ab6845faf1223b8f09eac5.zip |
278474: perform migration of credentials to secure storage
Change-Id: I590cabb6f09c9bdfe28532d47ba5dc04aa15bff2
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=278474
Diffstat (limited to 'org.eclipse.mylyn.tasks.core')
3 files changed, 237 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/KeyringMigrator.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/KeyringMigrator.java new file mode 100644 index 000000000..5ccd8765e --- /dev/null +++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/KeyringMigrator.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * Copyright (c) 2014 Tasktop Technologies and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tasktop Technologies - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.internal.tasks.core.util; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collection; +import java.util.Map; +import java.util.Map.Entry; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; +import org.eclipse.mylyn.commons.core.StatusHandler; +import org.eclipse.mylyn.commons.repositories.core.ILocationService; +import org.eclipse.mylyn.commons.repositories.core.auth.ICredentialsStore; +import org.eclipse.mylyn.internal.commons.repositories.core.LocationService; +import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants; + +/** + * Reads properties from the deprecated Eclipse keyring and writes them to the {@link ICredentialsStore} provided by the + * {@link ILocationService}. + * + * @author Sam Davis + */ +public abstract class KeyringMigrator<T> { + protected final ILocationService service = LocationService.getDefault(); + + protected final String authRealm; + + protected final String authScheme; + + public KeyringMigrator(String authRealm, String authScheme) { + this.authRealm = authRealm; + this.authScheme = authScheme; + } + + /** + * Migrate credentials for the given locations + */ + public void migrateCredentials(Collection<T> locations) { + for (T location : locations) { + migrateCredentials(location); + } + } + + @SuppressWarnings({ "unchecked", "deprecation" }) + protected void migrateCredentials(T location) { + try { + Map<String, String> properties = getAuthorizationInfo(getUrl(location)); + putProperties(properties, location); + } catch (MalformedURLException e) { + StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, + "Error migrating keyring credentials for " + getUrl(location), e)); //$NON-NLS-1$ + } + } + + protected Map<String, String> getAuthorizationInfo(String url) throws MalformedURLException { + return Platform.getAuthorizationInfo(new URL(url), authRealm, authScheme); + } + + protected void putProperties(Map<String, String> properties, T location) { + if (properties != null) { + ICredentialsStore store = service.getCredentialsStore(getUrl(location)); + for (Entry<String, String> entry : properties.entrySet()) { + putKeyValue(location, entry.getKey(), entry.getValue(), store); + } + } + } + + protected void putKeyValue(T location, String key, String value, ICredentialsStore store) { + store.put(key, value, key.endsWith(".password")); //$NON-NLS-1$ + } + + protected abstract String getUrl(T location); +} diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/TaskRepositoryKeyringMigrator.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/TaskRepositoryKeyringMigrator.java new file mode 100644 index 000000000..d86d87832 --- /dev/null +++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/TaskRepositoryKeyringMigrator.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2014 Tasktop Technologies and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tasktop Technologies - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.internal.tasks.core.util; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collection; +import java.util.Map; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; +import org.eclipse.mylyn.commons.core.StatusHandler; +import org.eclipse.mylyn.commons.repositories.core.auth.ICredentialsStore; +import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants; +import org.eclipse.mylyn.tasks.core.TaskRepository; + +/** + * Extends {@link KeyringMigrator} to migrate TaskRepository credentials, including usernames which are stored in the + * repository properties. + * + * @author Sam Davis + */ +public class TaskRepositoryKeyringMigrator extends KeyringMigrator<TaskRepository> { + private static final String KEY_USERNAME = "org.eclipse.mylyn.tasklist.repositories.username"; //$NON-NLS-1$ + + protected static URL defaultUrl; + static { + try { + defaultUrl = new URL("http://eclipse.org/mylyn"); //$NON-NLS-1$ + } catch (MalformedURLException e) { + StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, e.getMessage(), e)); + } + } + + public TaskRepositoryKeyringMigrator(String authRealm, String authScheme) { + super(authRealm, authScheme); + } + + @Override + public void migrateCredentials(Collection<TaskRepository> locations) { + StatusHandler.log(new Status(IStatus.INFO, ITasksCoreConstants.ID_PLUGIN, + "Migrating task repository credentials from keyring.")); //$NON-NLS-1$ + super.migrateCredentials(locations); + } + + @Override + protected Map<String, String> getAuthorizationInfo(String url) throws MalformedURLException { + try { + return super.getAuthorizationInfo(url); + } catch (MalformedURLException e) { + return Platform.getAuthorizationInfo(defaultUrl, url, authScheme); + } + } + + @Override + protected String getUrl(TaskRepository location) { + return location.getRepositoryUrl(); + } + + @Override + protected void putKeyValue(TaskRepository location, String key, String value, ICredentialsStore store) { + if (KEY_USERNAME.equals(key)) { + location.setProperty(key, value); + } else { + super.putKeyValue(location, key, value, store); + } + } +} diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/TaskRepositorySecureStoreMigrator.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/TaskRepositorySecureStoreMigrator.java new file mode 100644 index 000000000..4d43bdee8 --- /dev/null +++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/TaskRepositorySecureStoreMigrator.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2014 Tasktop Technologies and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tasktop Technologies - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.internal.tasks.core.util; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.equinox.security.storage.EncodingUtils; +import org.eclipse.equinox.security.storage.ISecurePreferences; +import org.eclipse.equinox.security.storage.SecurePreferencesFactory; +import org.eclipse.equinox.security.storage.StorageException; +import org.eclipse.mylyn.commons.core.StatusHandler; +import org.eclipse.mylyn.commons.repositories.core.ILocationService; +import org.eclipse.mylyn.commons.repositories.core.auth.ICredentialsStore; +import org.eclipse.mylyn.internal.commons.repositories.core.LocationService; +import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants; +import org.eclipse.mylyn.tasks.core.TaskRepository; + +/** + * Reads all data from the old "org.eclipse.mylyn.tasks.core" secure store node and writes it to the + * {@link ICredentialsStore} provided by the {@link ILocationService}. + * + * @author Sam Davis + */ +public class TaskRepositorySecureStoreMigrator { + private final ILocationService service = LocationService.getDefault(); + + public void migrateCredentials(Collection<TaskRepository> repositories) { + if (!SecurePreferencesFactory.getDefault().nodeExists(ITasksCoreConstants.ID_PLUGIN)) { + // check that the old node exists so that we don't create an empty node + return; + } + StatusHandler.log(new Status(IStatus.INFO, ITasksCoreConstants.ID_PLUGIN, + "Migrating task repository credentials from old secure store node.")); //$NON-NLS-1$ + Set<String> repositoryUrls = getRepositoryUrls(repositories); + ISecurePreferences oldRootNode = SecurePreferencesFactory.getDefault().node(ITasksCoreConstants.ID_PLUGIN); + for (String child : oldRootNode.childrenNames()) { + ISecurePreferences repositoryNode = oldRootNode.node(child); + String repositoryUrl = EncodingUtils.decodeSlashes(repositoryNode.name()); + if (repositoryUrls.contains(repositoryUrl)) { + ICredentialsStore store = service.getCredentialsStore(repositoryUrl); + for (String key : repositoryNode.keys()) { + try { + String value = repositoryNode.get(key, null); + store.put(key, value, repositoryNode.isEncrypted(key)); + } catch (StorageException e) { + StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, + "Error migrating secure store credentials for " + repositoryUrl, e)); //$NON-NLS-1$ + } + } + } + } + } + + private Set<String> getRepositoryUrls(Collection<TaskRepository> repositories) { + Set<String> repositoryUrls = new HashSet<String>(); + for (TaskRepository taskRepository : repositories) { + repositoryUrls.add(taskRepository.getRepositoryUrl()); + } + return repositoryUrls; + } +} |