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 3 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
121 changes: 121 additions & 0 deletions product/dropkick.tests/Tasks/WinService/WinTestsWithAuthentication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
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();

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

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

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

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

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

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

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();

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

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
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 @@ -33,11 +33,21 @@ public class ProtoWinServiceCreateTask :
ServiceStartMode _startMode;
string _userName;
Path _path;
string _wmiUserName;
string _wmiPassword;

public ProtoWinServiceCreateTask(Path path, string serviceName)
public ProtoWinServiceCreateTask(Path path, string serviceName, string wmiUserName, string wmiPassword)
{
_path = path;
_serviceName = ReplaceTokens(serviceName);
if(!string.IsNullOrEmpty(wmiUserName))
{
_wmiUserName = ReplaceTokens(wmiUserName);
}
if(!string.IsNullOrEmpty(wmiPassword))
{
_wmiPassword = ReplaceTokens(wmiPassword);
}
}

public WinServiceCreateOptions WithDisplayName(string displayName)
Expand Down Expand Up @@ -76,9 +86,9 @@ public WinServiceCreateOptions AddDependency(string name)
public override void RegisterRealTasks(PhysicalServer site)
{
string serviceLocation = _installPath;
serviceLocation = _path.GetPhysicalPath(site, _installPath, true);
serviceLocation = _path.GetPhysicalPath(site, _installPath, true, _wmiUserName, _wmiPassword);

site.AddTask(new WinServiceCreateTask(site.Name, _serviceName)
site.AddTask(new WinServiceCreateTask(site.Name, _serviceName, _wmiUserName, _wmiPassword)
{
Dependencies = _dependencies.ToArray(),
UserName = _userName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ public class ProtoWinServiceDeleteTask :
BaseProtoTask
{
readonly string _serviceName;
readonly string _wmiUserName;
readonly string _wmiPassword;

public ProtoWinServiceDeleteTask(string serviceName)
public ProtoWinServiceDeleteTask(string serviceName, string wmiUserName=null, string wmiPassword=null)
{
_serviceName = ReplaceTokens(serviceName);
if(!string.IsNullOrEmpty(wmiUserName))
{
_wmiUserName = ReplaceTokens(wmiUserName);
}
if(!string.IsNullOrEmpty(wmiPassword))
{
_wmiPassword = ReplaceTokens(wmiPassword);
}
}

public override void RegisterRealTasks(PhysicalServer site)
{
site.AddTask(new WinServiceDeleteTask(site.Name, _serviceName));
site.AddTask(new WinServiceDeleteTask(site.Name, _serviceName, _wmiUserName, _wmiPassword));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ public class ProtoWinServiceStartTask :
BaseProtoTask
{
readonly string _serviceName;
readonly string _wmiUserName;
readonly string _wmiPassword;

public ProtoWinServiceStartTask(string serviceName)
public ProtoWinServiceStartTask(string serviceName, string wmiUserName=null, string wmiPassword=null)
{
_serviceName = ReplaceTokens(serviceName);
if (!string.IsNullOrEmpty(wmiUserName))
{
_wmiUserName = ReplaceTokens(wmiUserName);
}
if (!string.IsNullOrEmpty(wmiPassword))
{
_wmiPassword = ReplaceTokens(wmiPassword);
}
}

public override void RegisterRealTasks(PhysicalServer s)
{
s.AddTask(new WinServiceStartTask(s.Name, _serviceName));
s.AddTask(new WinServiceStartTask(s.Name, _serviceName, _wmiUserName, _wmiPassword));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ public class ProtoWinServiceStopTask :
BaseProtoTask
{
readonly string _serviceName;
readonly string _wmiUserName;
readonly string _wmiPassword;

public ProtoWinServiceStopTask(string serviceName)
public ProtoWinServiceStopTask(string serviceName, string wmiUserName=null, string wmiPassword=null)
{
_serviceName = ReplaceTokens(serviceName);
if (!string.IsNullOrEmpty(wmiUserName))
{
_wmiUserName = ReplaceTokens(wmiUserName);
}
if (!string.IsNullOrEmpty(wmiPassword))
{
_wmiPassword = ReplaceTokens(wmiPassword);
}
}

public override void RegisterRealTasks(PhysicalServer s)
{
s.AddTask(new WinServiceStopTask(s.Name, _serviceName));
s.AddTask(new WinServiceStopTask(s.Name, _serviceName, _wmiUserName, _wmiPassword));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,55 @@ public class ProtoWinServiceTask :
{
readonly ProtoServer _protoServer;
readonly string _serviceName;
string _wmiUserName;
string _wmiPassword;

public ProtoWinServiceTask(ProtoServer protoServer, string serviceName)
{
_protoServer = protoServer;
_serviceName = serviceName;
}

public WinServiceOptions WithAuthentication(string wmiUserName, string wmiPassword)
{
_wmiUserName = wmiUserName;
_wmiPassword = wmiPassword;
return this;
}

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

//child task
registerAdditionalActions(_protoServer);

_protoServer.RegisterProtoTask(new ProtoWinServiceStartTask(_serviceName));

_protoServer.RegisterProtoTask(new ProtoWinServiceStartTask(_serviceName, _wmiUserName, _wmiPassword));

return this;
}

public void Start()
{
_protoServer.RegisterProtoTask(new ProtoWinServiceStartTask(_serviceName));
_protoServer.RegisterProtoTask(new ProtoWinServiceStartTask(_serviceName, _wmiUserName, _wmiPassword));
}

public void Stop()
{
_protoServer.RegisterProtoTask(new ProtoWinServiceStopTask(_serviceName));
_protoServer.RegisterProtoTask(new ProtoWinServiceStopTask(_serviceName, _wmiUserName, _wmiPassword));
}

public WinServiceCreateOptions Create()
{
var proto = new ProtoWinServiceCreateTask(new DotNetPath(), _serviceName);
var proto = new ProtoWinServiceCreateTask(new DotNetPath(), _serviceName, _wmiUserName, _wmiPassword);
_protoServer.RegisterProtoTask(proto);
return proto;
}

public void Delete()
{
_protoServer.RegisterProtoTask(new ProtoWinServiceDeleteTask(_serviceName));
_protoServer.RegisterProtoTask(new ProtoWinServiceDeleteTask(_serviceName, _wmiUserName, _wmiPassword));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public interface WinServiceOptions
void Stop();
WinServiceCreateOptions Create();
void Delete();
WinServiceOptions WithAuthentication(string userName, string password);
WinServiceOptions Do(Action<ProtoServer> registerAdditionalActions);
}
}
Loading