From 10261a85e9a39fc862b08bd906df043b3b705e7e Mon Sep 17 00:00:00 2001 From: Frezzle Date: Mon, 4 Jul 2016 13:47:24 +0100 Subject: [PATCH 1/2] [CP-17933] Add feature flag for proxy authentication Added Registry.ProxyAuthenticationEnabled boolean property to enable/disable proxy authentication UI controls in Connection Options page. Cleaned up event handler logic in Connection Options page to return from multiple/unnecessary calls to event handlers when manually changing controls through code. Signed-off-by: Frezzle --- XenAdmin/Core/Registry.cs | 9 ++ .../OptionsPages/ConnectionOptionsPage.cs | 112 +++++++++++------- XenAdmin/Program.cs | 5 + 3 files changed, 86 insertions(+), 40 deletions(-) diff --git a/XenAdmin/Core/Registry.cs b/XenAdmin/Core/Registry.cs index 76e79e5d2..f2bf973b4 100644 --- a/XenAdmin/Core/Registry.cs +++ b/XenAdmin/Core/Registry.cs @@ -80,6 +80,14 @@ namespace XenAdmin.Core } } + internal static bool ProxyAuthenticationEnabled + { + get + { + return ReadBool(PROXY_AUTHENTICATION_ENABLED, false); + } + } + public static SSLCertificateTypes AlwaysShowSSLCertificates { get @@ -402,6 +410,7 @@ namespace XenAdmin.Core private const string HEALTH_CHECK_PRODUCT_KEY = "HealthCheckProductKey"; private const string HIDDEN_FEATURES = "HiddenFeatures"; private const string ADDITIONAL_FEATURES = "AdditionalFeatures"; + private const string PROXY_AUTHENTICATION_ENABLED = "ProxyAuthenticationEnabled"; } public enum SSLCertificateTypes { None, Changed, All } diff --git a/XenAdmin/Dialogs/OptionsPages/ConnectionOptionsPage.cs b/XenAdmin/Dialogs/OptionsPages/ConnectionOptionsPage.cs index 036df80c8..1748dd137 100644 --- a/XenAdmin/Dialogs/OptionsPages/ConnectionOptionsPage.cs +++ b/XenAdmin/Dialogs/OptionsPages/ConnectionOptionsPage.cs @@ -51,8 +51,8 @@ namespace XenAdmin.Dialogs.OptionsPages private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private OptionsDialog optionsDialog; - // used for preventing the event handlers (mainly the SelectUseThisProxyServer function) from being called when loading the settings into the text/check boxes - private bool built = false; + // used for preventing the event handlers from doing anything when changing controls through code + private bool freezeEventHandling = true; public ConnectionOptionsPage() { @@ -88,17 +88,30 @@ namespace XenAdmin.Dialogs.OptionsPages ProxyAddressTextBox.Text = Properties.Settings.Default.ProxyAddress; ProxyPortTextBox.Text = Properties.Settings.Default.ProxyPort.ToString(); BypassForServersCheckbox.Checked = Properties.Settings.Default.BypassProxyForServers; - AuthenticationCheckBox.Checked = Properties.Settings.Default.ProvideProxyAuthentication; - // checks for empty default username/password which starts out unencrypted - string protectedUsername = Properties.Settings.Default.ProxyUsername; - ProxyUsernameTextBox.Text = string.IsNullOrEmpty(protectedUsername) ? "" : EncryptionUtils.Unprotect(Properties.Settings.Default.ProxyUsername); - string protectedPassword = Properties.Settings.Default.ProxyPassword; - ProxyPasswordTextBox.Text = string.IsNullOrEmpty(protectedPassword) ? "" : EncryptionUtils.Unprotect(Properties.Settings.Default.ProxyPassword); + if (Registry.ProxyAuthenticationEnabled) + { + AuthenticationCheckBox.Checked = Properties.Settings.Default.ProvideProxyAuthentication; + // checks for empty default username/password which starts out unencrypted + string protectedUsername = Properties.Settings.Default.ProxyUsername; + ProxyUsernameTextBox.Text = string.IsNullOrEmpty(protectedUsername) ? "" : EncryptionUtils.Unprotect(Properties.Settings.Default.ProxyUsername); + string protectedPassword = Properties.Settings.Default.ProxyPassword; + ProxyPasswordTextBox.Text = string.IsNullOrEmpty(protectedPassword) ? "" : EncryptionUtils.Unprotect(Properties.Settings.Default.ProxyPassword); + } + else + { + // hide controls + AuthenticationCheckBox.Visible = false; + ProxyUsernameLabel.Visible = false; + ProxyUsernameTextBox.Visible = false; + ProxyPasswordLabel.Visible = false; + ProxyPasswordTextBox.Visible = false; + } + ConnectionTimeoutNud.Value = Properties.Settings.Default.ConnectionTimeout / 1000; - built = true; + freezeEventHandling = false; } private void UseProxyRadioButton_CheckedChanged(object sender, EventArgs e) @@ -108,56 +121,74 @@ namespace XenAdmin.Dialogs.OptionsPages private void AuthenticationCheckBox_CheckedChanged(object sender, EventArgs e) { - if (!AuthenticationCheckBox.Checked) - { - ProxyUsernameTextBox.Clear(); - ProxyPasswordTextBox.Clear(); - AuthenticationCheckBox.Checked = false; // have to redo this as the 2 Clears above cause the checkbox to recheck - } - - SelectUseThisProxyServer(); - - enableOK(); + CallWithFreezeAwareness(AuthenticationCheckBoxChanged); } private void ProxyAddressTextBox_TextChanged(object sender, EventArgs e) { - SelectUseThisProxyServer(); - enableOK(); + CallWithFreezeAwareness(SelectUseThisProxyServer); } private void ProxyPortTextBox_TextChanged(object sender, EventArgs e) { - SelectUseThisProxyServer(); - enableOK(); + CallWithFreezeAwareness(SelectUseThisProxyServer); } private void ProxyUsernameTextBox_TextChanged(object sender, EventArgs e) { - SelectUseThisProxyServer(); - if (!AuthenticationCheckBox.Checked) - AuthenticationCheckBox.Checked = true; - enableOK(); + CallWithFreezeAwareness(SelectProvideCredentials); } private void ProxyPasswordTextBox_TextChanged(object sender, EventArgs e) { - SelectUseThisProxyServer(); - if (!AuthenticationCheckBox.Checked) - AuthenticationCheckBox.Checked = true; - enableOK(); + CallWithFreezeAwareness(SelectProvideCredentials); } private void BypassForServersCheckbox_CheckedChanged(object sender, EventArgs e) { - SelectUseThisProxyServer(); - enableOK(); + CallWithFreezeAwareness(SelectUseThisProxyServer); } private void SelectUseThisProxyServer() { - if (!UseProxyRadioButton.Checked && built) - UseProxyRadioButton.Checked = true; + UseProxyRadioButton.Checked = true; + } + + private void SelectProvideCredentials() + { + SelectUseThisProxyServer(); + AuthenticationCheckBox.Checked = true; + } + + private void AuthenticationCheckBoxChanged() + { + SelectUseThisProxyServer(); + if (!AuthenticationCheckBox.Checked) + { + ProxyUsernameTextBox.Clear(); + ProxyPasswordTextBox.Clear(); + } + } + + private void CallWithFreezeAwareness(Action func) + { + if (freezeEventHandling) + return; + freezeEventHandling = true; + + try + { + func(); + } + catch + { + throw; + } + finally + { + freezeEventHandling = false; + enableOK(); + } } private void enableOK() @@ -223,8 +254,12 @@ namespace XenAdmin.Dialogs.OptionsPages if (ProxyAddressTextBox.Text != Properties.Settings.Default.ProxyAddress && !string.IsNullOrEmpty(ProxyAddressTextBox.Text)) Properties.Settings.Default.ProxyAddress = ProxyAddressTextBox.Text; - Properties.Settings.Default.ProxyUsername = EncryptionUtils.Protect(ProxyUsernameTextBox.Text); - Properties.Settings.Default.ProxyPassword = EncryptionUtils.Protect(ProxyPasswordTextBox.Text); + if (Registry.ProxyAuthenticationEnabled) + { + Properties.Settings.Default.ProxyUsername = EncryptionUtils.Protect(ProxyUsernameTextBox.Text); + Properties.Settings.Default.ProxyPassword = EncryptionUtils.Protect(ProxyPasswordTextBox.Text); + Properties.Settings.Default.ProvideProxyAuthentication = AuthenticationCheckBox.Checked; + } try { @@ -240,9 +275,6 @@ namespace XenAdmin.Dialogs.OptionsPages if (BypassForServersCheckbox.Checked != Properties.Settings.Default.BypassProxyForServers) Properties.Settings.Default.BypassProxyForServers = BypassForServersCheckbox.Checked; - if (AuthenticationCheckBox.Checked != Properties.Settings.Default.ProvideProxyAuthentication) - Properties.Settings.Default.ProvideProxyAuthentication = AuthenticationCheckBox.Checked; - // timeout settings int timeout = (int)ConnectionTimeoutNud.Value; if (timeout * 1000 != Properties.Settings.Default.ConnectionTimeout) diff --git a/XenAdmin/Program.cs b/XenAdmin/Program.cs index ef7a2accf..86b1c02c1 100644 --- a/XenAdmin/Program.cs +++ b/XenAdmin/Program.cs @@ -985,6 +985,11 @@ namespace XenAdmin public static void ReconfigureConnectionSettings() { + if (!Registry.ProxyAuthenticationEnabled) + { + Properties.Settings.Default.ProvideProxyAuthentication = false; + Properties.Settings.Default.Save(); + } XenAPI.Session.Proxy = XenAdminConfigManager.Provider.GetProxyFromSettings(null); } From 5f412549b2900ccae2b99b6a11ede500136ba522 Mon Sep 17 00:00:00 2001 From: Frezzle Date: Tue, 5 Jul 2016 10:40:21 +0100 Subject: [PATCH 2/2] [CP-17933] Add feature flag for proxy authentication Made event handler logic clearer and more readable. Signed-off-by: Frezzle --- .../OptionsPages/ConnectionOptionsPage.cs | 91 +++++++++++-------- 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/XenAdmin/Dialogs/OptionsPages/ConnectionOptionsPage.cs b/XenAdmin/Dialogs/OptionsPages/ConnectionOptionsPage.cs index 1748dd137..6f77155c2 100644 --- a/XenAdmin/Dialogs/OptionsPages/ConnectionOptionsPage.cs +++ b/XenAdmin/Dialogs/OptionsPages/ConnectionOptionsPage.cs @@ -52,7 +52,7 @@ namespace XenAdmin.Dialogs.OptionsPages private OptionsDialog optionsDialog; // used for preventing the event handlers from doing anything when changing controls through code - private bool freezeEventHandling = true; + private bool eventsDisabled = true; public ConnectionOptionsPage() { @@ -111,42 +111,84 @@ namespace XenAdmin.Dialogs.OptionsPages ConnectionTimeoutNud.Value = Properties.Settings.Default.ConnectionTimeout / 1000; - freezeEventHandling = false; + eventsDisabled = false; } private void UseProxyRadioButton_CheckedChanged(object sender, EventArgs e) { + if (eventsDisabled) + return; + enableOK(); } private void AuthenticationCheckBox_CheckedChanged(object sender, EventArgs e) { - CallWithFreezeAwareness(AuthenticationCheckBoxChanged); + if (eventsDisabled) + return; + + eventsDisabled = true; + + if (!AuthenticationCheckBox.Checked) + { + ProxyUsernameTextBox.Clear(); + ProxyPasswordTextBox.Clear(); + } + SelectUseThisProxyServer(); + + eventsDisabled = false; + + enableOK(); } private void ProxyAddressTextBox_TextChanged(object sender, EventArgs e) { - CallWithFreezeAwareness(SelectUseThisProxyServer); + if (eventsDisabled) + return; + + SelectUseThisProxyServer(); + + enableOK(); } private void ProxyPortTextBox_TextChanged(object sender, EventArgs e) { - CallWithFreezeAwareness(SelectUseThisProxyServer); + if (eventsDisabled) + return; + + SelectUseThisProxyServer(); + + enableOK(); } private void ProxyUsernameTextBox_TextChanged(object sender, EventArgs e) { - CallWithFreezeAwareness(SelectProvideCredentials); + if (eventsDisabled) + return; + + SelectProvideCredentials(); + + enableOK(); } private void ProxyPasswordTextBox_TextChanged(object sender, EventArgs e) { - CallWithFreezeAwareness(SelectProvideCredentials); + if (eventsDisabled) + return; + + SelectProvideCredentials(); + + enableOK(); } private void BypassForServersCheckbox_CheckedChanged(object sender, EventArgs e) { - CallWithFreezeAwareness(SelectUseThisProxyServer); + if (eventsDisabled) + return; + + SelectUseThisProxyServer(); + + enableOK(); } private void SelectUseThisProxyServer() @@ -156,39 +198,8 @@ namespace XenAdmin.Dialogs.OptionsPages private void SelectProvideCredentials() { - SelectUseThisProxyServer(); AuthenticationCheckBox.Checked = true; - } - - private void AuthenticationCheckBoxChanged() - { - SelectUseThisProxyServer(); - if (!AuthenticationCheckBox.Checked) - { - ProxyUsernameTextBox.Clear(); - ProxyPasswordTextBox.Clear(); - } - } - - private void CallWithFreezeAwareness(Action func) - { - if (freezeEventHandling) - return; - freezeEventHandling = true; - - try - { - func(); - } - catch - { - throw; - } - finally - { - freezeEventHandling = false; - enableOK(); - } + UseProxyRadioButton.Checked = true; } private void enableOK()