Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wolf2019-06-11 05:04:52 -0400
committerMatthias Sohn2019-06-11 05:50:16 -0400
commit442cd079656ec49a921d76b9b7cccb985901ed78 (patch)
tree39a8e524e27b7f207eb72ae3bbd4bbbed5d440eb
parentacbd245305eab71839b2b8dbf2cd33737112a179 (diff)
downloadegit-442cd079656ec49a921d76b9b7cccb985901ed78.tar.gz
egit-442cd079656ec49a921d76b9b7cccb985901ed78.tar.xz
egit-442cd079656ec49a921d76b9b7cccb985901ed78.zip
Handle absolute paths to SSH identity files
The list of private keys in Eclipse may contain absolute paths. Only resolve relative paths relative to the configured SSH2 home directory. Bug: 548121 Change-Id: I7ace545e2736c451b625699d07cba438f0e3934b Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/SshPreferencesMirrorTest.java65
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/internal/SshPreferencesMirror.java18
2 files changed, 80 insertions, 3 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/SshPreferencesMirrorTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/SshPreferencesMirrorTest.java
new file mode 100644
index 000000000..7fed0e928
--- /dev/null
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/SshPreferencesMirrorTest.java
@@ -0,0 +1,65 @@
+/*******************************************************************************
+ * Copyright (C) 2019 Thomas Wolf <thomas.wolf@paranor.ch>
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *******************************************************************************/
+package org.eclipse.egit.core.internal;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class SshPreferencesMirrorTest {
+
+ @Rule
+ public TemporaryFolder tmp = new TemporaryFolder();
+
+ @Before
+ public void prepareMirror() throws Exception {
+ SshPreferencesMirror.INSTANCE.start();
+ }
+
+ @After
+ public void resetPreferences() throws Exception {
+ set("PRIVATEKEY", null);
+ }
+
+ private void set(String key, String value) throws Exception {
+ IEclipsePreferences preferences = InstanceScope.INSTANCE
+ .getNode("org.eclipse.jsch.core");
+ if (value == null) {
+ preferences.remove(key);
+ } else {
+ preferences.put(key, value);
+ }
+ preferences.flush();
+ }
+
+ @Test
+ public void testAbsoluteKeyPath() throws Exception {
+ File fakeSshHome = tmp.newFolder("faks_ssh_home");
+ File otherDirectory = tmp.newFolder("other");
+ File fakeDefaultKey = File.createTempFile("id_", "key", fakeSshHome);
+ File fakeOtherKey = File.createTempFile("other", "key", otherDirectory);
+ set("PRIVATEKEY", fakeDefaultKey.getName() + ','
+ + fakeOtherKey.getAbsolutePath());
+ assertEquals(
+ '[' + fakeDefaultKey.getAbsolutePath() + ", "
+ + fakeOtherKey.getAbsolutePath() + ']',
+ SshPreferencesMirror.INSTANCE.getDefaultIdentities(fakeSshHome)
+ .toString());
+ }
+}
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/SshPreferencesMirror.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/SshPreferencesMirror.java
index dcfafbca3..8c73aab60 100644
--- a/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/SshPreferencesMirror.java
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/SshPreferencesMirror.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch>
+ * Copyright (C) 2018, 2019 Thomas Wolf <thomas.wolf@paranor.ch>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -58,12 +58,18 @@ public class SshPreferencesMirror {
private String defaultMechanisms;
+ private boolean started;
+
private SshPreferencesMirror() {
// This is a singleton.
}
/** Starts mirroring the ssh preferences. */
public void start() {
+ if (started) {
+ return;
+ }
+ started = true;
preferences = InstanceScope.INSTANCE.getNode(PREFERENCES_NODE);
if (preferences != null) {
preferences.addPreferenceChangeListener(listener);
@@ -73,6 +79,7 @@ public class SshPreferencesMirror {
/** Stops mirroring the ssh preferences. */
public void stop() {
+ started = false;
if (preferences != null) {
preferences.removePreferenceChangeListener(listener);
}
@@ -168,8 +175,13 @@ public class SshPreferencesMirror {
return null;
}
return defaultIdentities.stream()
- .map(s -> new File(sshDir, s).toPath())
- .filter(Files::exists).collect(Collectors.toList());
+ .map(s -> {
+ File f = new File(s);
+ if (!f.isAbsolute()) {
+ f = new File(sshDir, s);
+ }
+ return f.toPath();
+ }).filter(Files::exists).collect(Collectors.toList());
}
}

Back to the top