Ignore unreadable SSH private keys when autoloading identities
PATCH 1/2 Ignore unreadable SSH private keys when autoloading identities
During SSH startup we read all keys in the user's ~/.ssh, even if we may not need them for this particular transport session.
If a file is not really a key, or it contains a key that JSch doesn't recognize we shouldn't crash the transport. Instead we should skip the file and move on. Later on we just don't have that identity available to us, or we'll crash if we try to add that identity file explicitly from ~/.ssh/config.
--- .../jgit/transport/DefaultSshSessionFactory.java | 13 +++++++++++-- 1 files changed, 11 insertions(+), 2 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java +11 -2
@@ -165,14 +165,23 @@ private void identities() throws JSchException { final File k = new File(sshdir, n.substring(0, n.length() - 4)); if (!k.isFile()) continue; - addIdentity(k); + + try { + addIdentity(k); + } catch (JSchException e) { + if (e.getMessage().startsWith("invalid privatekey: ")) + continue; + throw e; + } } } private void addIdentity(final File identityFile) throws JSchException { final String path = identityFile.getAbsolutePath(); - if (loadedIdentities.add(path)) + if (!loadedIdentities.contains(path)) { userJSch.addIdentity(path); + loadedIdentities.add(path); + } } private static class AWT_UserInfo implements UserInfo,
Signature · 1 line
1.6.0.174.gd789c
PATCH 2/2 pgm.push: Ensure SSH connections are closed
If we don't close the transport when we are done with it the SSH session stored within the Transport will still be opened to the remote side. JSch may have created one or more background user threads to handle that connection, which means the JVM won't terminate cleanly when we are done with our work.
We must close each and every transport we opened.
--- .../src/org/spearce/jgit/pgm/Push.java | 24 ++++++++++++------- 1 files changed, 15 insertions(+), 9 deletions(-)
org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Push.java +15 -9
@@ -50,6 +50,7 @@ import org.spearce.jgit.transport.RefSpec; import org.spearce.jgit.transport.RemoteRefUpdate; import org.spearce.jgit.transport.Transport; +import org.spearce.jgit.transport.URIish; import org.spearce.jgit.transport.RemoteRefUpdate.Status; @Command(common = true, usage = "Update remote repository from local refs")
@@ -111,13 +112,18 @@ protected void run() throws Exception { final Collection<RemoteRefUpdate> toPush = transport .findRemoteRefUpdatesFor(refSpecs); - final PushResult result = transport.push(new TextProgressMonitor(), - toPush); - printPushResult(transport, result); + final URIish uri = transport.getURI(); + final PushResult result; + try { + result = transport.push(new TextProgressMonitor(), toPush); + } finally { + transport.close(); + } + printPushResult(uri, result); } } - private void printPushResult(final Transport transport, + private void printPushResult(final URIish uri, final PushResult result) { shownURI = false; boolean everythingUpToDate = true;
@@ -126,7 +132,7 @@ private void printPushResult(final Transport transport, for (final RemoteRefUpdate rru : result.getRemoteUpdates()) { if (rru.getStatus() == Status.UP_TO_DATE) { if (verbose) - printRefUpdateResult(transport, result, rru); + printRefUpdateResult(uri, result, rru); } else everythingUpToDate = false; }
@@ -134,25 +140,25 @@ private void printPushResult(final Transport transport, for (final RemoteRefUpdate rru : result.getRemoteUpdates()) { // ...then successful updates... if (rru.getStatus() == Status.OK) - printRefUpdateResult(transport, result, rru); + printRefUpdateResult(uri, result, rru); } for (final RemoteRefUpdate rru : result.getRemoteUpdates()) { // ...finally, others (problematic) if (rru.getStatus() != Status.OK && rru.getStatus() != Status.UP_TO_DATE) - printRefUpdateResult(transport, result, rru); + printRefUpdateResult(uri, result, rru); } if (everythingUpToDate) out.println("Everything up-to-date"); } - private void printRefUpdateResult(final Transport transport, + private void printRefUpdateResult(final URIish uri, final PushResult result, final RemoteRefUpdate rru) { if (!shownURI) { shownURI = true; - out.format("To %s\n", transport.getURI()); + out.format("To %s\n", uri); } final String remoteName = rru.getRemoteName();
Signature · 1 line
1.6.0.174.gd789c
Quoted 1 line · Shawn O. Pearce
org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java +0
-0
(...)
org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java private void identities() throws JSchException { · line 175
Quoted 7 lines
org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java +7
-0+ try { + addIdentity(k); + } catch (JSchException e) { + if (e.getMessage().startsWith("invalid privatekey: ")) + continue; + throw e; + }
That's extreme error handling with JSch;) Do you really think it's better to rely on internal error message instead of continuing in any case? Which other exceptions we would like to pass level up?
Signature · 2 lines
Marek Zawirski [zawir] [email protected]
Quoted 14 lines · Marek Zawirski
Quoted 1 line · Shawn O. Pearce
org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java +0
-0(...)
org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java private void identities() throws JSchException { · line 175
Quoted 7 lines
org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java +7
-0+ try { + addIdentity(k); + } catch (JSchException e) { + if (e.getMessage().startsWith("invalid privatekey: ")) + continue; + throw e; + }That's extreme error handling with JSch;) Do you really think it's better to rely on internal error message instead of continuing in any case? Which other exceptions we would like to pass level up?
Oh, that's a good question. In this particular code we're just trying to prime the list of known keys so there's a chance we could later prompt you for a passphrase during the handshaking. So we probably could get away with just ignoring all JSchExceptions at this stage and treat the key as though it wasn't present...
I can't imagine what else we'd get back. A FileNotFoundException just means the user deleted the key before we could actually read it (no big deal); an IOException because the key isn't readable isn't an issue either.
I guess I can just change this to ignore everything.
Signature · 1 line
Shawn.
PATCH v2 1/2 Ignore unreadable SSH private keys when autoloading identities
During SSH startup we read all keys in the user's ~/.ssh, even if we may not need them for this particular transport session.
If a file is not really a key, or it contains a key that JSch doesn't recognize we shouldn't crash the transport. Instead we should skip the file and move on. Later on we just don't have that identity available to us, or we'll crash if we try to add that identity file explicitly from ~/.ssh/config.
---
Quoted 16 lines · Shawn O. Pearce
Quoted 14 lines · Marek Zawirski
Quoted 1 line · Shawn O. Pearce
org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java +0
-0(...)
org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java
Quoted 7 lines
org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java +7
-0+ try { + addIdentity(k); + } catch (JSchException e) { + if (e.getMessage().startsWith("invalid privatekey: ")) + continue; + throw e; + }That's extreme error handling with JSch;) Do you really think it's better to rely on internal error message instead of continuing in any case? Which other exceptions we would like to pass level up?
Quoted 1 line
I guess I can just change this to ignore everything.
.../jgit/transport/DefaultSshSessionFactory.java | 11 +++++++++-- 1 files changed, 9 insertions(+), 2 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java +9 -2
@@ -165,14 +165,21 @@ private void identities() throws JSchException { final File k = new File(sshdir, n.substring(0, n.length() - 4)); if (!k.isFile()) continue; - addIdentity(k); + + try { + addIdentity(k); + } catch (JSchException e) { + continue; + } } } private void addIdentity(final File identityFile) throws JSchException { final String path = identityFile.getAbsolutePath(); - if (loadedIdentities.add(path)) + if (!loadedIdentities.contains(path)) { userJSch.addIdentity(path); + loadedIdentities.add(path); + } } private static class AWT_UserInfo implements UserInfo,
Signature · 1 line
1.6.0.174.gd789c