Skip to contentSkip to conversation

Correctly honor IdentityFile from ~/.ssh/config

PATCH New Shawn O. Pearce 1 participant 1 message Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Correctly honor IdentityFile from ~/.ssh/config

JSch offers all identities available within the JSch object used to create a Session. In order to offer only a specific IdentityFile to the remote server we must create a unique JSch with only that one identity loaded. However, we also want to cache the key once it has been decrypted, so that repeated uses of a single file from the same JVM do not cause repeated passphrase prompts.

The SshConfigSessionFactory now loads only the default SSH keys (~/.ssh/identity for SSH1, ~/.ssh/{id_rsa,id_dsa} for SSH2) into the default JSch. If a Host block in ~/.ssh/config selects some other IdentityFile then we create a unique JSch instance for the key, and copy over the same known hosts table.

Fix issue 35 (Correctly honor IdentityFile in ~/.ssh/config)

---
 .../spearce/egit/ui/EclipseSshSessionFactory.java  |   36 +++++--
 .../jgit/transport/SshConfigSessionFactory.java    |  100 ++++++++++++--------
 2 files changed, 89 insertions(+), 47 deletions(-)
org.spearce.egit.ui/src/org/spearce/egit/ui/EclipseSshSessionFactory.java +27 -9
index 2cafdfe..04600ea 100644
@@ -1,6 +1,7 @@
 /*******************************************************************************
  * Copyright (C) 2008, Robin Rosenberg <[email protected]>
  * Copyright (C) 2008, Shawn O. Pearce <[email protected]>
+ * Copyright (C) 2009, Google, Inc.
  *
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -10,8 +11,8 @@
 
 import org.eclipse.jsch.core.IJSchService;
 import org.eclipse.jsch.ui.UserInfoPrompter;
+import org.spearce.jgit.transport.OpenSshConfig;
 import org.spearce.jgit.transport.SshConfigSessionFactory;
-import org.spearce.jgit.transport.OpenSshConfig.Host;
 
 import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.JSchException;
@@ -25,19 +26,36 @@ EclipseSshSessionFactory(final IJSchService p) {
 	}
 
 	@Override
-	protected Session createSession(final String user, final String host,
-			final int port) throws JSchException {
-		return provider.createSession(host, port, user);
+	protected JSch createDefaultJSch() throws JSchException {
+		// Forcing a dummy session to be created will cause the known hosts
+		// and configured private keys to be initialized. This is needed by
+		// our parent class in case non-default JSch instances need to be made.
+		//
+		provider.createSession("127.0.0.1", 0, "eclipse");
+		return provider.getJSch();
 	}
 
 	@Override
-	protected void configure(final Host hc, final Session session) {
-		if (!hc.isBatchMode())
-			new UserInfoPrompter(session);
+	protected Session createSession(final OpenSshConfig.Host hc,
+			final String user, final String host, final int port)
+			throws JSchException {
+		final JSch jsch = getJSch(hc);
+		if (jsch == provider.getJSch()) {
+			// If its the default JSch desired, let the provider
+			// manage the session creation for us.
+			//
+			return provider.createSession(host, port, user);
+		} else {
+			// This host configuration is using a different IdentityFile,
+			// one that is not available through the default JSch.
+			//
+			return jsch.getSession(user, host, port);
+		}
 	}
 
 	@Override
-	protected JSch getUserJSch() throws JSchException {
-		return provider.getJSch();
+	protected void configure(final OpenSshConfig.Host hc, final Session session) {
+		if (!hc.isBatchMode())
+			new UserInfoPrompter(session);
 	}
 }
org.spearce.jgit/src/org/spearce/jgit/transport/SshConfigSessionFactory.java +62 -38

172 lines not on this page · raw message

Signature · 1 line
1.6.3.rc0.200.g4086