From f9b7f71b18e764d80ca25105f8cbb93b5226cb15 Mon Sep 17 00:00:00 2001 From: Mihaela Stoica Date: Fri, 26 May 2017 16:54:38 +0100 Subject: [PATCH] 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 --- .../HealthCheckTests/CredentialTests.cs | 16 +++++++-- .../TransferHealthCheckSettingAction.cs | 34 +++++++------------ XenServerHealthCheck/ServerListHelper.cs | 6 ++++ 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/XenAdminTests/HealthCheckTests/CredentialTests.cs b/XenAdminTests/HealthCheckTests/CredentialTests.cs index d5f2a68be..aabd9cd95 100755 --- a/XenAdminTests/HealthCheckTests/CredentialTests.cs +++ b/XenAdminTests/HealthCheckTests/CredentialTests.cs @@ -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 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); diff --git a/XenModel/Actions/HealthCheck/TransferHealthCheckSettingAction.cs b/XenModel/Actions/HealthCheck/TransferHealthCheckSettingAction.cs index ec63dad6a..a06541c11 100755 --- a/XenModel/Actions/HealthCheck/TransferHealthCheckSettingAction.cs +++ b/XenModel/Actions/HealthCheck/TransferHealthCheckSettingAction.cs @@ -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(); diff --git a/XenServerHealthCheck/ServerListHelper.cs b/XenServerHealthCheck/ServerListHelper.cs index efebb9562..9cf36cb8d 100755 --- a/XenServerHealthCheck/ServerListHelper.cs +++ b/XenServerHealthCheck/ServerListHelper.cs @@ -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)