Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to provide username/password for remote administrator account #58

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions product/dropkick.tests/Tasks/WinService/WinTestsWithAuthentication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using dropkick.DeploymentModel;
using dropkick.Tasks.WinService;
using dropkick.Wmi;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace dropkick.tests.Tasks.WinService
{
[TestFixture]
[Category("Integration")]
public class WinTestsWithAuthentication
{
public class WmiAuthenticationInfo
{
public string MachineName { get; set; }
public string WmiUserName { get; set; }
public string WmiPassword { get; set; }
public string ServiceUserName { get; set; }
public string ServicePassword { get; set; }
}

private WmiAuthenticationInfo GetAuthenticationInfo()
{
string path = System.IO.Path.GetFullPath("WmiAuthenticationInfo.xml");
if (!System.IO.File.Exists(path))
{
throw new Exception("Please create a settings file first at: " + path);
}
var serializer = new XmlSerializer(typeof(WmiAuthenticationInfo));
using (var reader = new System.IO.StreamReader(path))
{
return (WmiAuthenticationInfo)serializer.Deserialize(reader);
}
}

[Test]
[Explicit]
[Category("Integration")]
public void Start()
{
var authInfo = GetAuthenticationInfo();

WmiService.WithAuthentication(authInfo.WmiUserName, authInfo.WmiPassword);

var t = new WinServiceStopTask(authInfo.MachineName, "IISADMIN");
var verifyStopResult = t.VerifyCanRun();
Log(verifyStopResult);
AssertSuccess(verifyStopResult);

var stopResult = t.Execute();
Log(stopResult);
AssertSuccess(stopResult);

var t2 = new WinServiceStartTask(authInfo.MachineName, "IISADMIN");
var verifyStartResult = t2.VerifyCanRun();
Log(verifyStartResult);
AssertSuccess(verifyStartResult);

var startResult = t2.Execute();
Log(startResult);
AssertSuccess(startResult);
}

[Test]
[Explicit]
public void RemoteCreate()
{
var authInfo = GetAuthenticationInfo();

WmiService.WithAuthentication(authInfo.WmiUserName, authInfo.WmiPassword);
var t = new WinServiceCreateTask(authInfo.MachineName, "DropKicKTestService");

t.ServiceLocation = "C:\\Test\\TestService.exe";
t.StartMode = ServiceStartMode.Automatic;
t.UserName = authInfo.ServiceUserName;
t.Password = authInfo.ServicePassword;

DeploymentResult o = t.VerifyCanRun();
AssertSuccess(o);
var result = t.Execute();
Log(result);
AssertSuccess(result);
}

[Test]
[Explicit]
[Category("Integration")]
public void RemoteDelete()
{
var authInfo = GetAuthenticationInfo();

WmiService.WithAuthentication(authInfo.ServiceUserName, authInfo.ServicePassword);

var t = new WinServiceDeleteTask(authInfo.MachineName, "DropkicKTestService");

DeploymentResult o = t.VerifyCanRun();
Log(o);
AssertSuccess(o);
var result = t.Execute();
Log(result);
AssertSuccess(result);
}

private void AssertSuccess(DeploymentResult result)
{
Assert.IsFalse(result.Any(i => i.Status == DeploymentItemStatus.Alert || i.Status == DeploymentItemStatus.Error));
}

private void Log(DeploymentResult result)
{
if (result != null)
{
foreach (var item in result)
{
Debug.WriteLine(item.Message);
}
}
}

}
}
1 change: 1 addition & 0 deletions product/dropkick.tests/dropkick.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<Compile Include="Tasks\Security\LogOnAsServiceTaskTest.cs" />
<Compile Include="Tasks\Security\Msmq\SetSensibleMsmqDefaultsTest.cs" />
<Compile Include="Tasks\Security\Msmq\MsmqGrantSecuritySpecs.cs" />
<Compile Include="Tasks\WinService\WinTestsWithAuthentication.cs" />
<Compile Include="Tasks\Xml\Context\XmlContextResult.cs" />
<Compile Include="Tasks\WinService\PasswordPromptSpecs.cs" />
<Compile Include="Tasks\Xml\XmlPokeTaskSpecs.cs" />
Expand Down
21 changes: 21 additions & 0 deletions product/dropkick/Configuration/Dsl/Authentication/Extension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dropkick.Wmi;
using dropkick.StringInterpolation;

namespace dropkick.Configuration.Dsl.Authentication
{
public static class Extension
{
public static ProtoServer WithAuthentication(this ProtoServer server, string remoteUserName, string remotePassword)
{
var interpolator = new CaseInsensitiveInterpolator();
remoteUserName = interpolator.ReplaceTokens(HUB.Settings, remoteUserName);
remotePassword = interpolator.ReplaceTokens(HUB.Settings, remotePassword);
WmiService.WithAuthentication(remoteUserName, remotePassword);
return server;
}
}
}
6 changes: 6 additions & 0 deletions product/dropkick/Configuration/Dsl/Files/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,11 @@ public static ExistsOptions Exists(this ProtoServer protoserver, string reason,
protoserver.RegisterProtoTask(proto);
return proto;
}

public static void OpenFolderShareWithAuthentication(this ProtoServer protoServer, string folderName, string userName, string password)
{
var task = new OpenFolderShareAuthenticationProtoTask(folderName, userName, password);
protoServer.RegisterProtoTask(task);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using dropkick.Tasks;
using dropkick.Tasks.Files;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dropkick.Configuration.Dsl.Files
{
public class OpenFolderShareAuthenticationProtoTask : BaseProtoTask
{
private readonly string _folderName;
private readonly string _userName;
private readonly string _password;

public OpenFolderShareAuthenticationProtoTask(string folderName, string userName, string password)
{
_folderName = ReplaceTokens(folderName);
_userName = userName;
_password = password;
}

public override void RegisterRealTasks(dropkick.DeploymentModel.PhysicalServer server)
{
string to = server.MapPath(_folderName);

var task = new OpenFolderShareAuthenticationTask(to, _userName, _password);
server.AddTask(task);
}
}
}
2 changes: 1 addition & 1 deletion product/dropkick/Configuration/Dsl/WinService/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public static class Extension
public static WinServiceOptions WinService(this ProtoServer protoServer, string serviceName)
{
return new ProtoWinServiceTask(protoServer, serviceName);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ public ProtoWinServiceTask(ProtoServer protoServer, string serviceName)
_serviceName = serviceName;
}


public WinServiceOptions Do(Action<ProtoServer> registerAdditionalActions)
{
_protoServer.RegisterProtoTask(new ProtoWinServiceStopTask(_serviceName));

//child task
registerAdditionalActions(_protoServer);


_protoServer.RegisterProtoTask(new ProtoWinServiceStartTask(_serviceName));

return this;
Expand Down
2 changes: 1 addition & 1 deletion product/dropkick/FileSystem/DotNetPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class DotNetPath : Path
{
#region Path Members

public string GetPhysicalPath(PhysicalServer site, string path,bool forceLocalPath)
public string GetPhysicalPath(PhysicalServer site, string path, bool forceLocalPath)
{
var standardizedPath = path;
if (!IsUncPath(standardizedPath))
Expand Down
49 changes: 49 additions & 0 deletions product/dropkick/FileSystem/FileShareAuthenticator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dropkick.FileSystem
{
public static class FileShareAuthenticator
{
public static FileShareAuthenticationContext BeginFileShareAuthentication(string remoteUnc, string userName, string password)
{
string error = PinvokeWindowsNetworking.connectToRemote(remoteUnc, userName, password);
if (!string.IsNullOrEmpty(error))
{
throw new Exception("Error calling PinvokeWindowsNetworking.connectToRemote: " + error);
}
return new FileShareAuthenticationContext(remoteUnc, userName, password);
}

public class FileShareAuthenticationContext : IDisposable
{
private readonly string _remoteUnc;
private readonly string _userName;
private readonly string _password;
private bool _active;

public FileShareAuthenticationContext(string remoteUnc, string userName, string password)
{
_remoteUnc = remoteUnc;
_userName = userName;
_password = password;
_active = true;
}

public void Dispose()
{
if (_active)
{
var error = PinvokeWindowsNetworking.disconnectRemote(_remoteUnc);
if (!string.IsNullOrEmpty(error))
{
throw new Exception("PinvokeWindowsNetworking.disconnectRemote failed: " + error);
}
_active = false;
}
}
}
}
}
Loading