From 6706c8601103c5a719dfd3a96f2f9087e5b358a1 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Tue, 16 Sep 2008 08:44:29 -0700 Subject: [PATCH] Add support for ~/.ssh/config BatchMode Connections created through batch processes (e.g. those started by cron) don't have a terminal to interact with a user through. A common way to disable password prompting with OpenSSH is to setup a Host block in ~/.ssh/config with "BatchMode yes" enabled, thus telling the client not to prompt for passphases or passwords. Signed-off-by: Shawn O. Pearce Signed-off-by: Robin Rosenberg --- .../spearce/egit/ui/EclipseSshSessionFactory.java | 2 +- .../spearce/jgit/transport/OpenSshConfigTest.java | 21 +++++++++++++++++++++ .../jgit/transport/DefaultSshSessionFactory.java | 2 +- .../org/spearce/jgit/transport/OpenSshConfig.java | 22 ++++++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/EclipseSshSessionFactory.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/EclipseSshSessionFactory.java index 67c5f165..098d2347 100644 --- a/org.spearce.egit.ui/src/org/spearce/egit/ui/EclipseSshSessionFactory.java +++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/EclipseSshSessionFactory.java @@ -48,7 +48,7 @@ class EclipseSshSessionFactory extends SshSessionFactory { addIdentity(hc.getIdentityFile()); if (pass != null) session.setPassword(pass); - else + else if (!hc.isBatchMode()) new UserInfoPrompter(session); final String pauth = hc.getPreferredAuthentications(); diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/transport/OpenSshConfigTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/transport/OpenSshConfigTest.java index 1a71d082..959b6b76 100644 --- a/org.spearce.jgit.test/tst/org/spearce/jgit/transport/OpenSshConfigTest.java +++ b/org.spearce.jgit.test/tst/org/spearce/jgit/transport/OpenSshConfigTest.java @@ -148,4 +148,25 @@ public class OpenSshConfigTest extends RepositoryTestCase { assertNotNull(h); assertEquals("publickey,hostbased", h.getPreferredAuthentications()); } + + public void testAlias_BatchModeDefault() throws Exception { + final Host h = osc.lookup("orcz"); + assertNotNull(h); + assertEquals(false, h.isBatchMode()); + } + + public void testAlias_BatchModeYes() throws Exception { + config("Host orcz\n" + "\tBatchMode yes\n"); + final Host h = osc.lookup("orcz"); + assertNotNull(h); + assertEquals(true, h.isBatchMode()); + } + + public void testAlias_InheritBatchMode() throws Exception { + config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n" + + "\tBatchMode yes\n"); + final Host h = osc.lookup("orcz"); + assertNotNull(h); + assertEquals(true, h.isBatchMode()); + } } diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java b/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java index b6f58f0b..89beab7e 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java @@ -101,7 +101,7 @@ class DefaultSshSessionFactory extends SshSessionFactory { addIdentity(hc.getIdentityFile()); if (pass != null) session.setPassword(pass); - else + else if (!hc.isBatchMode()) session.setUserInfo(new AWT_UserInfo()); final String pauth = hc.getPreferredAuthentications(); diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java b/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java index 2f41e562..df38e189 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java @@ -224,6 +224,10 @@ public class OpenSshConfig { for (final Host c : current) if (c.preferredAuthentications == null) c.preferredAuthentications = nows(dequote(argValue)); + } else if ("BatchMode".equalsIgnoreCase(keyword)) { + for (final Host c : current) + if (c.batchMode == null) + c.batchMode = yesno(dequote(argValue)); } } @@ -260,6 +264,12 @@ public class OpenSshConfig { return b.toString(); } + private static Boolean yesno(final String value) { + if ("yes".equalsIgnoreCase(value)) + return Boolean.TRUE; + return Boolean.FALSE; + } + private File toFile(final String path) { if (path.startsWith("~/")) return new File(home, path.substring(2)); @@ -293,6 +303,8 @@ public class OpenSshConfig { String preferredAuthentications; + Boolean batchMode; + void copyFrom(final Host src) { if (hostName == null) hostName = src.hostName; @@ -304,6 +316,8 @@ public class OpenSshConfig { user = src.user; if (preferredAuthentications == null) preferredAuthentications = src.preferredAuthentications; + if (batchMode == null) + batchMode = src.batchMode; } /** @@ -342,5 +356,13 @@ public class OpenSshConfig { public String getPreferredAuthentications() { return preferredAuthentications; } + + /** + * @return true if batch (non-interactive) mode is preferred for this + * host connection. + */ + public boolean isBatchMode() { + return batchMode != null && batchMode.booleanValue(); + } } } -- 2.11.4.GIT