git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: Robin Rosenberg <robin.rosenberg@dewire.com>
Cc: git@vger.kernel.org
Subject: [JGIT PATCH 2/2] Add support for ~/.ssh/config BatchMode
Date: Tue, 16 Sep 2008 08:44:29 -0700	[thread overview]
Message-ID: <1221579869-27835-2-git-send-email-spearce@spearce.org> (raw)
In-Reply-To: <1221579869-27835-1-git-send-email-spearce@spearce.org>

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 <spearce@spearce.org>
---
 .../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 67c5f16..098d234 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 @@ public Session getSession(String user, String pass, String host, int port)
 			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 1a71d08..959b6b7 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 @@ config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
 		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 b6f58f0..89beab7 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 @@ public synchronized Session getSession(String user, String pass,
 			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 2f41e56..df38e18 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 @@ else if (sp < 0)
 				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 @@ private static String nows(final String value) {
 		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 @@ private File toFile(final String path) {
 
 		String preferredAuthentications;
 
+		Boolean batchMode;
+
 		void copyFrom(final Host src) {
 			if (hostName == null)
 				hostName = src.hostName;
@@ -304,6 +316,8 @@ void copyFrom(final Host src) {
 				user = src.user;
 			if (preferredAuthentications == null)
 				preferredAuthentications = src.preferredAuthentications;
+			if (batchMode == null)
+				batchMode = src.batchMode;
 		}
 
 		/**
@@ -342,5 +356,13 @@ public String getUser() {
 		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();
+		}
 	}
 }
-- 
1.6.0.2.389.g421e0

  reply	other threads:[~2008-09-16 15:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-16 15:44 [JGIT PATCH 1/2 v2] Add support for ~/.ssh/config PreferredAuthentications Shawn O. Pearce
2008-09-16 15:44 ` Shawn O. Pearce [this message]
2008-09-16 22:46   ` [JGIT PATCH 2/2] Add support for ~/.ssh/config BatchMode Robin Rosenberg
2008-09-16 22:52     ` Shawn O. Pearce

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1221579869-27835-2-git-send-email-spearce@spearce.org \
    --to=spearce@spearce.org \
    --cc=git@vger.kernel.org \
    --cc=robin.rosenberg@dewire.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).