Hiding password input in Ant

When writting Ant scripts I always have Ant prompt for passwords be it for SVN, SFTP, or anything else that needs to be logged into. Typically would have the following:

<input message="Please enter SVN password:" addproperty="svn.password" />

The only problem is that it echos the password back at you, there has to be a better way. So of to Google I went and found this with an interesting Ant script fragment:

<input message="secure-input:" addproperty="the.password">
    <handler classname="org.apache.tools.ant.input.SecureInputHandler" />
</input>

I did not know there could be nested elements in <input>. Apparently Ant 1.7 added <handler> nested element for <input>. But nowhere is there any documentation on "SecureInputHandler".

So I tried out the fragment and as the post noted there are a few requirements to get it to work:

  1. Ant 1.7.1
  2. Java 6, if it is run on Java 5 it will fallback to a "normal" <input>

One downside, I have not been able to get it to work from within Eclipse 3.5 (since Ant 1.7.1 is bundled with it) and using Java 6 runtime. Eclipse will just hang when it gets to the "SecureInputHandler" and you have to stop the build script. It does work great from command line which is how I run all my Ant scripts anyway so it is not a real issue for me, but might be for others.

Posted by David Epler on 11/12/2009 at 12:40 PM | Categories: Ant -

7 Comments

Jim Priest

Jim Priest wrote on 11/12/09 1:30 PM

Very cool! For another solution check out this older blog post:

http://www.thecrumb.com/2007/02/07/using-ant-while-hiding-your-svn-password/
David Epler

David Epler wrote on 11/12/09 3:03 PM

@Jim, I did read that post but I was looking for a solution that required no additional libraries/taskdefs.

The other solution I did find was:
<input message="Enter Password:" addproperty="password"/>
<exec executable="sh">
<arg line="-c 'stty echo isig < /dev/tty'"/>
</exec>

The above worked and fit my requirements, but just felt like more of a hack.

Also thanks for the Ant Wiki that you maintain, it is an excellent resource.
PapisTemp

PapisTemp wrote on 10/18/10 2:45 PM

I have this error when i use the "sh" way .. ->
The value of attribute "line" associated with an element type "arg" must not contain the '<' character.

i tried the solution of "sh" because i tried to avoid to include new jar of "org.apache.tools.ant.input.SecureInputHandler"
It seems not to be included by default in my system
David Epler

David Epler wrote on 10/20/10 7:57 PM

@PapisTemp

The <arg> line should be:

<arg line="-c 'stty echo isig &lt; /dev/tty'"/>

Some reason the < did not stay escaped. It does work. As for the use of SecureInputHandler there are no additional jar files, it is included inside Ant 1.7.1+.
Sylvain Bellemare

Sylvain Bellemare wrote on 02/14/11 5:16 PM

Thanks for the tip regarding the required Java 6!

By the way,
the following snippet also works fine:


<target name="hide">
<input message="Enter message to hide ..." addproperty="msg">
<handler type="secure"/>
</input>
<echo message="The hidden message is: ${msg}"/>
</target>
thirupathi

thirupathi wrote on 10/12/11 1:41 PM

Thanks a lot for maintaing this great blog. It solved my problem in seconds.
robin

robin wrote on 11/25/11 10:42 AM

I knocked this up. It works in Eclipse. You'll need to stick the class in a jar (e.g. ant-logindialog.jar) and put in on your ant config classpath.

   <taskdef name="logindialog" classname="antlogindialog.PasswordTask" />
   <logindialog message="Login Details : " username="deploy.user" password="deploy.password" />



package antlogindialog;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
* Ant task to show a Password Dialog.
* (c) Under Apache 2.0 License
*/
public class PasswordTask extends Task
{
   private String message;
   private String password;
   private String username;

   public void setMessage(String message)
   {
      this.message = message;
   }

   public void setPassword(String password)
   {
      this.password = password;
   }

   public void setUsername(String username)
   {
      this.username = username;
   }
   
   public void execute() throws BuildException
   {
      JTextField usernameField = new JTextField();
      JPasswordField passwordField = new JPasswordField();
      
      final JComponent[] inputs = new JComponent[]
      {
            new JLabel("Username"), usernameField,
            new JLabel("Password"), passwordField
      };
      
      JOptionPane.showMessageDialog(null, inputs, message, JOptionPane.PLAIN_MESSAGE);
      
      getProject().setProperty(username, usernameField.getText());
      getProject().setProperty(password, new String(passwordField.getPassword()));
   }
   
   public static void main(String[] args) { new PasswordTask().execute(); }
}

Write your comment





Leave this field empty: