IgorShare Thoughts and Ideas

Consulting and Training

Adding custom credentials to the SVN activity in TFS build workflow

Posted by Igor Moochnick on 03/14/2011

Other posts in the series:

  1. Can TFS be useful if the sources are in Subversion (SVN)? Or how to run integration and nightly builds on TFS from SVN?
  2. Teaching TFS custom activity to work with the Svn, Cvs, Git and other source control command line tools

Obviously you don’t want to expose your password to everyone who can see the build definitions. For this I can suggest 2 separate solutions – the simple one and the right one.

If you want it quick – implement Password as a string workflow parameter but do not put the value in the build definition (this is visible to everyone) but, in the metadata, set it’s availability to “Always Show the Parameter”. By this you’ll ensure that the password will be available for you to set not only during the build definition but, as well, when anyone will be scheduling it for the execution.

The better way, as I found out, is to create a custom “credentials” construct (type). This allows you to have your own editor for the password (where you can have it masked) and to control how it appears on the build definition.

The final result should look like this (notice the “Credentials” parameter):

image

Continue reading for the implementation details …

For starters let’s define a container for the user’s credentials:

public class SvnCredentials
{
    public string Username { get; set; }
    public string Password { get; set; }

    public override string ToString()
    {
        return string.Format("{{{0}{1}}}", Username, string.IsNullOrEmpty(Password) ? "" : "/********");
    }
}

Notice here the “ToString” function – this is how I’d like to present the value of the credentials on the build configuration page so it’ll look like on the picture above.

Now we’re going to add a property of this type to our custom activity:

public sealed class SvnActivity : CodeActivity
{
...
    [RequiredArgument]
    public InArgument<svncredentials> SvnCredentials { get; set; }
...
}

TFS, out of the box, doesn’t know anything about this property type but it provides a nice way to add custom editors. So let’s get our hands dirty.

Create a simple WinForm dialog with a password box (the one that doesn’t show your password in the plain text):

image

The code-behind can look as simple as this:

public partial class CredentialsDialog : Form
{
    public CredentialsDialog()
    {
        InitializeComponent();
    }

    public string Username
    {
        get { return txtUsername.Text; }
        set { txtUsername.Text = value;  }
    }

    public string Password
    {
        get { return txtPassword.Text; }
        set { txtPassword.Text = value; }
    }
}

And to tie things up we need to create a custom editor that inherits from UITypeEditor:

public class CredentialsEditor : UITypeEditor
{
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        if (provider != null)
        {
            var editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (editorService != null)
            {
                var svnCredentials = value as SvnCredentials ?? new SvnCredentials();

                using (var dialog = new CredentialsDialog())
                {
                    dialog.Username = svnCredentials.Username;
                    dialog.Password = svnCredentials.Password;

                    if (editorService.ShowDialog(dialog) == DialogResult.OK)
                    {
                        svnCredentials.Username = dialog.Username;
                        svnCredentials.Password = dialog.Password;
                    }
                }
            }
        }

        return value;
    }

    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }
}

That’s it – the only thing left is to tie all the pieces together.

Let the system know how users will be editing this “credentials” custom data type. In the Workflow’s Arguments section open the Metadata collection:

image

Add the SvnCredentials parameter while filling the Editor field with the full path to the Credentials Editor like this “SvnActivityLib.CredentialsEditor,SvnActivityLib”:

image

That’s it.

And, as well, you’re welcome to tap into the full source code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: