Fix the HealthCheck tests - cont.

- restore the "empty credential" test and update HealthCheck to ignore null or empty username and password
- also update XenCenter so it doesn't send null or empty username and password

Signed-off-by: Mihaela Stoica <mihaela.stoica@citrix.com>
This commit is contained in:
Mihaela Stoica 2017-05-26 16:54:38 +01:00
parent aecf992938
commit f9b7f71b18
3 changed files with 32 additions and 24 deletions

View File

@ -59,16 +59,26 @@ namespace XenAdminTests.HealthCheckTests
string UserName = "User1";
string Password = "password1";
//1. Empty list
// Empty list
ServerListHelper.instance.ClearServerList();
int conSize = 0;
List<ServerInfo> con = ServerListHelper.instance.GetServerList();
Assert.IsTrue(con.Count == conSize);
//2. Send credential and check result
//1. Empty credential
NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", HealthCheckSettings.HEALTH_CHECK_PIPE, PipeDirection.Out);
pipeClient.Connect();
string credential = EncryptionUtils.ProtectForLocalMachine(String.Join(SEPARATOR.ToString(), new[] { HostName, UserName, Password }));
string credential = EncryptionUtils.ProtectForLocalMachine(String.Join(SEPARATOR.ToString(), new[] { HostName, null, null }));
pipeClient.Write(Encoding.UTF8.GetBytes(credential), 0, credential.Length);
pipeClient.Close();
System.Threading.Thread.Sleep(1000);
con = ServerListHelper.instance.GetServerList();
Assert.IsTrue(con.Count == conSize);
//2. Send credential and check result
pipeClient = new NamedPipeClientStream(".", HealthCheckSettings.HEALTH_CHECK_PIPE, PipeDirection.Out);
pipeClient.Connect();
credential = EncryptionUtils.ProtectForLocalMachine(String.Join(SEPARATOR.ToString(), new[] { HostName, UserName, Password }));
pipeClient.Write(Encoding.UTF8.GetBytes(credential), 0, credential.Length);
pipeClient.Close();
System.Threading.Thread.Sleep(1000);

View File

@ -58,18 +58,17 @@ namespace XenAdmin.Actions
}
private const char SEPARATOR = '\x202f'; // narrow non-breaking space.
private string ProtectCredential(string Host, string username, string passwordSecret)
{
if (username == string.Empty || password == string.Empty)
return EncryptionUtils.ProtectForLocalMachine(String.Join(SEPARATOR.ToString(), new[] { Host }));
else
return EncryptionUtils.ProtectForLocalMachine(String.Join(SEPARATOR.ToString(), new[] { Host, username, passwordSecret }));
}
private const string HEALTHCHECKSERVICENAME = "XenServerHealthCheck";
protected override void Run()
{
var host = Helpers.GetMaster(pool.Connection);
if (host == null)
return;
if (healthCheckSettings.Status == HealthCheckStatus.Enabled && (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)))
return; // do not send empty/null username or password (when the Health Check is enabled), as they will be ignored
ServiceController sc = new ServiceController(HEALTHCHECKSERVICENAME);
try
{
@ -101,19 +100,12 @@ namespace XenAdmin.Actions
}
} while (!pipeClient.IsConnected && retryCount-- != 0);
foreach (Host host in pool.Connection.Cache.Hosts)
{
if (host.IsMaster())
{
string credential;
if (healthCheckSettings.Status == HealthCheckStatus.Enabled)
credential = ProtectCredential(host.address, username, password);
else
credential = ProtectCredential(host.address, string.Empty, string.Empty);
pipeClient.Write(Encoding.UTF8.GetBytes(credential), 0, (Encoding.UTF8.GetBytes(credential)).Length);
break;
}
}
var credential = healthCheckSettings.Status == HealthCheckStatus.Enabled
? String.Join(SEPARATOR.ToString(), host.address, username, password)
: String.Join(SEPARATOR.ToString(), host.address);
var encryptedCredential = EncryptionUtils.ProtectForLocalMachine(credential);
pipeClient.Write(Encoding.UTF8.GetBytes(encryptedCredential), 0, (Encoding.UTF8.GetBytes(encryptedCredential)).Length);
pipeClient.Write(Encoding.UTF8.GetBytes(HealthCheckSettings.HEALTH_CHECK_PIPE_END_MESSAGE), 0, HealthCheckSettings.HEALTH_CHECK_PIPE_END_MESSAGE.Length);
pipeClient.Close();

View File

@ -181,6 +181,12 @@ namespace XenServerHealthCheck
string decryptCredential = EncryptionUtils.UnprotectForLocalMachine(credential);
string[] decryptCredentialComps = decryptCredential.Split(SEPARATOR);
if (decryptCredentialComps.Length != 1 && decryptCredentialComps.Length != 3)
return;
if (decryptCredentialComps.Length == 3 && (string.IsNullOrEmpty(decryptCredentialComps[1]) || string.IsNullOrEmpty(decryptCredentialComps[2])))
return; //ignore null or empty username and password
lock (serverListLock)
{
if (decryptCredentialComps.Length == 3)