2023-01-24 15:29:31 +01:00
|
|
|
|
/* Copyright (c) Cloud Software Group, Inc.
|
2015-05-22 11:29:53 +02:00
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
|
* that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2015-08-24 13:51:46 +02:00
|
|
|
|
using XenAPI;
|
2017-11-17 02:04:45 +01:00
|
|
|
|
using XenCenterLib;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
|
|
|
|
namespace XenServerHealthCheck
|
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
public class ServerInfo
|
|
|
|
|
{
|
|
|
|
|
public string HostName { get; set; }
|
|
|
|
|
public string UserName { get; set; }
|
|
|
|
|
public string Password { get; set; }
|
2015-08-24 13:51:46 +02:00
|
|
|
|
public System.Threading.Tasks.Task task { get; set; }
|
2015-07-01 08:29:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
public class ServerListHelper
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
private ServerListHelper() { }
|
|
|
|
|
public static readonly ServerListHelper instance = new ServerListHelper();
|
2015-05-22 11:29:53 +02:00
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
2015-07-01 08:29:27 +02:00
|
|
|
|
private List<ServerInfo> serverList;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
private static readonly Object serverListLock = new Object();
|
2015-07-01 08:29:27 +02:00
|
|
|
|
public List<ServerInfo> GetServerList()
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
List<ServerInfo> currentList;
|
2015-06-26 07:36:30 +02:00
|
|
|
|
lock (serverListLock)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
currentList = new List<ServerInfo>(serverList);
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
2015-06-26 07:36:30 +02:00
|
|
|
|
return currentList;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
public void Init()
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
lock (serverListLock)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
serverList = new List<ServerInfo>();
|
2015-06-26 07:36:30 +02:00
|
|
|
|
string[] encServerList = Properties.Settings.Default.ServerList ?? new string[0];
|
|
|
|
|
foreach (string encServer in encServerList)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2020-10-22 01:49:13 +02:00
|
|
|
|
string decryptCredential;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
decryptCredential = EncryptionUtils.Unprotect(encServer);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
log.Warn("Could not unprotect server list.", e);
|
|
|
|
|
return; //return and not continue; same behaviour as XenCenter settings
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
string[] decryptCredentialComps = decryptCredential.Split(SEPARATOR);
|
2020-10-22 01:49:13 +02:00
|
|
|
|
if (decryptCredentialComps.Length < 3)
|
|
|
|
|
{
|
|
|
|
|
log.Warn("Server list was not decrypted to valid entries.");
|
|
|
|
|
return; //return and not continue; same behaviour as XenCenter settings
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var connection = new ServerInfo
|
|
|
|
|
{
|
|
|
|
|
HostName = decryptCredentialComps[0],
|
|
|
|
|
UserName = decryptCredentialComps[1],
|
|
|
|
|
Password = decryptCredentialComps[2],
|
|
|
|
|
task = null
|
|
|
|
|
};
|
2015-06-26 07:36:30 +02:00
|
|
|
|
serverList.Add(connection);
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-04 12:52:45 +02:00
|
|
|
|
public const char SEPARATOR = '\x202f'; // narrow non-breaking space.
|
2015-07-01 08:29:27 +02:00
|
|
|
|
private string ProtectCredential(ServerInfo connection)
|
2015-06-26 07:36:30 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
string Host = connection.HostName ?? string.Empty;
|
|
|
|
|
string username = connection.UserName ?? string.Empty;
|
2015-06-26 07:36:30 +02:00
|
|
|
|
string passwordSecret = connection.Password ?? string.Empty;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
return EncryptionUtils.Protect(String.Join(SEPARATOR.ToString(), new[] { Host, username, passwordSecret }));
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-01 08:29:27 +02:00
|
|
|
|
public void removeServerCredential(string HostName)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
lock (serverListLock)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
foreach (ServerInfo con in serverList)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
if (HostName == con.HostName)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
serverList.Remove(con);
|
2015-07-01 08:29:27 +02:00
|
|
|
|
updateServerList();
|
|
|
|
|
return;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
public bool UpdateServerCredential(ServerInfo server, string coordinatorName)
|
2015-11-03 05:26:30 +01:00
|
|
|
|
{
|
|
|
|
|
lock (serverListLock)
|
|
|
|
|
{
|
|
|
|
|
bool needRefresh = true;
|
|
|
|
|
foreach (ServerInfo con in serverList)
|
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
if (coordinatorName == con.HostName)
|
2015-11-03 05:26:30 +01:00
|
|
|
|
{
|
|
|
|
|
needRefresh = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serverList.Remove(server);
|
|
|
|
|
if (needRefresh)
|
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
server.HostName = coordinatorName;
|
2015-11-03 05:26:30 +01:00
|
|
|
|
serverList.Add(server);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateServerList();
|
|
|
|
|
return needRefresh;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
private void updateServerList()
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
List<string> encList = new List<string>();
|
2015-07-01 08:29:27 +02:00
|
|
|
|
foreach (ServerInfo connection in serverList)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
encList.Add(ProtectCredential(connection));
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
2015-06-26 07:36:30 +02:00
|
|
|
|
Properties.Settings.Default.ServerList = encList.ToArray();
|
|
|
|
|
Properties.Settings.Default.Save();
|
|
|
|
|
}
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
2015-07-01 08:29:27 +02:00
|
|
|
|
public bool UpdateServerInfo(ServerInfo server)
|
|
|
|
|
{
|
|
|
|
|
lock (serverListLock)
|
|
|
|
|
{
|
|
|
|
|
foreach (ServerInfo con in serverList)
|
|
|
|
|
{
|
|
|
|
|
if (server.HostName == con.HostName)
|
|
|
|
|
{
|
|
|
|
|
serverList.Remove(con);
|
|
|
|
|
serverList.Add(server);
|
|
|
|
|
updateServerList();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-25 15:45:58 +02:00
|
|
|
|
public void ClearServerList()
|
|
|
|
|
{
|
|
|
|
|
lock (serverListLock)
|
|
|
|
|
{
|
|
|
|
|
serverList.Clear();
|
|
|
|
|
updateServerList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
public void UpdateServerCredential(string credential)
|
|
|
|
|
{
|
|
|
|
|
log.Info("Receive credential update message");
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
2020-10-22 01:49:13 +02:00
|
|
|
|
string decryptCredential;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
decryptCredential = EncryptionUtils.UnprotectForLocalMachine(credential);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
log.Warn("Could not unprotect credential.", e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
string[] decryptCredentialComps = decryptCredential.Split(SEPARATOR);
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
2017-05-26 17:54:38 +02:00
|
|
|
|
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
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
lock (serverListLock)
|
|
|
|
|
{
|
|
|
|
|
if (decryptCredentialComps.Length == 3)
|
|
|
|
|
{//Add credential
|
2015-08-24 13:51:46 +02:00
|
|
|
|
System.Threading.Tasks.Task originalTask = null;
|
2015-07-01 08:29:27 +02:00
|
|
|
|
foreach (ServerInfo connection in serverList)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
if (connection.HostName == decryptCredentialComps[0])
|
2015-06-26 07:36:30 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
originalTask = connection.task;
|
2015-06-26 07:36:30 +02:00
|
|
|
|
serverList.Remove(connection);
|
|
|
|
|
log.Info("Refresh credential");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
2015-07-01 08:29:27 +02:00
|
|
|
|
ServerInfo newConnection = new ServerInfo();
|
|
|
|
|
newConnection.HostName = decryptCredentialComps[0];
|
|
|
|
|
newConnection.UserName = decryptCredentialComps[1];
|
2015-06-26 07:36:30 +02:00
|
|
|
|
newConnection.Password = decryptCredentialComps[2];
|
2015-07-01 08:29:27 +02:00
|
|
|
|
newConnection.task = originalTask;
|
2015-06-26 07:36:30 +02:00
|
|
|
|
serverList.Add(newConnection);
|
|
|
|
|
log.InfoFormat("Add credential, current credential size is {0}", serverList.Count);
|
|
|
|
|
}
|
|
|
|
|
else if (decryptCredentialComps.Length == 1)
|
|
|
|
|
{//remove credential
|
2015-07-01 08:29:27 +02:00
|
|
|
|
foreach (ServerInfo connection in serverList)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
if (connection.HostName == decryptCredentialComps[0])
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
serverList.Remove(connection);
|
2015-07-01 04:35:59 +02:00
|
|
|
|
log.InfoFormat("Remove credential, current credential size is {0}", serverList.Count);
|
2015-05-22 11:29:53 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-26 07:36:30 +02:00
|
|
|
|
|
|
|
|
|
updateServerList();
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-24 13:51:46 +02:00
|
|
|
|
|
|
|
|
|
public void UpdateProxy(string proxy)
|
|
|
|
|
{
|
|
|
|
|
log.Info("Receive proxy update message");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string[] proxySettings = proxy.Split(SEPARATOR);
|
2017-06-26 23:06:45 +02:00
|
|
|
|
if (proxySettings.Length < 2)
|
|
|
|
|
return;
|
|
|
|
|
|
2015-08-24 13:51:46 +02:00
|
|
|
|
HTTPHelper.ProxyStyle proxyStyle = (HTTPHelper.ProxyStyle)Int32.Parse(proxySettings[1]);
|
|
|
|
|
|
|
|
|
|
switch (proxyStyle)
|
|
|
|
|
{
|
|
|
|
|
case HTTPHelper.ProxyStyle.SpecifiedProxy:
|
|
|
|
|
Properties.Settings.Default.ProxySetting = (Int32)proxyStyle;
|
|
|
|
|
Properties.Settings.Default.ProxyAddress = proxySettings[2];
|
|
|
|
|
Properties.Settings.Default.ProxyPort = Int32.Parse(proxySettings[3]);
|
|
|
|
|
Properties.Settings.Default.ConnectionTimeout = Int32.Parse(proxySettings[4]);
|
2016-06-22 14:05:21 +02:00
|
|
|
|
Properties.Settings.Default.BypassProxyForServers = bool.Parse(proxySettings[5]);
|
|
|
|
|
Properties.Settings.Default.ProvideProxyAuthentication = bool.Parse(proxySettings[6]);
|
2017-05-23 17:57:38 +02:00
|
|
|
|
Properties.Settings.Default.ProxyUsername = EncryptionUtils.Protect(EncryptionUtils.UnprotectForLocalMachine(proxySettings[7]));
|
|
|
|
|
Properties.Settings.Default.ProxyPassword = EncryptionUtils.Protect(EncryptionUtils.UnprotectForLocalMachine(proxySettings[8]));
|
2016-10-12 20:09:35 +02:00
|
|
|
|
Properties.Settings.Default.ProxyAuthenticationMethod = Int32.Parse(proxySettings[9]);
|
2016-06-22 14:05:21 +02:00
|
|
|
|
break;
|
2015-08-24 13:51:46 +02:00
|
|
|
|
|
|
|
|
|
case HTTPHelper.ProxyStyle.SystemProxy:
|
|
|
|
|
Properties.Settings.Default.ProxySetting = (Int32)proxyStyle;
|
2016-06-22 14:05:21 +02:00
|
|
|
|
break;
|
2015-08-24 13:51:46 +02:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
Properties.Settings.Default.ProxySetting = (Int32)proxyStyle;
|
2016-06-22 14:05:21 +02:00
|
|
|
|
break;
|
2015-08-24 13:51:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-22 14:05:21 +02:00
|
|
|
|
Properties.Settings.Default.Save();
|
|
|
|
|
XenServerHealthCheckService.ReconfigureConnectionSettings();
|
2015-08-24 13:51:46 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
log.Error("Error parsing 'ProxySetting' from XenCenter", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-16 14:59:18 +02:00
|
|
|
|
|
|
|
|
|
public void UpdateXenCenterMetadata(string message)
|
|
|
|
|
{
|
|
|
|
|
log.Info("Receive XenCenter metadata update message");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string[] metadata = message.Split(SEPARATOR);
|
2017-06-26 23:06:45 +02:00
|
|
|
|
if (metadata.Length != 2)
|
|
|
|
|
return;
|
2017-07-03 15:28:05 +02:00
|
|
|
|
Properties.Settings.Default.XenCenterMetadata = EncryptionUtils.UnprotectForLocalMachine(metadata[1]);
|
2017-06-26 23:06:45 +02:00
|
|
|
|
Properties.Settings.Default.Save();
|
2017-06-16 14:59:18 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
log.Error("Error parsing 'XenCenterMetadata' from XenCenter", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-04 12:52:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string XenCenterMetadata
|
|
|
|
|
{
|
|
|
|
|
get { return Properties.Settings.Default.XenCenterMetadata; }
|
|
|
|
|
}
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
2015-08-24 13:51:46 +02:00
|
|
|
|
}
|