Exemple #1
0
        public static StackHashSettings Load(String fileName)
        {
            bool forceASave = false;

            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            if (!File.Exists(fileName))
            {
                throw new ArgumentException("File does not exist", "fileName");
            }

            // Simply deserializes the specified data from the XML file.
            FileStream xmlFile = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            StackHashSettings stackHashSettings = null;

            try
            {
                stackHashSettings = s_Serializer.Deserialize(xmlFile) as StackHashSettings;

                if (String.IsNullOrEmpty(stackHashSettings.ServiceGuid))
                {
                    stackHashSettings.ServiceGuid = Guid.NewGuid().ToString();
                    forceASave = true;
                }

                if (stackHashSettings.ClientTimeoutInSeconds == 0)
                {
                    stackHashSettings.ClientTimeoutInSeconds = DefaultClientTimeoutInSeconds;
                }

                // If the next context ID = 0 but there exists at least 1 context then this is probably
                // an old settings - pre NextContextId field. This does not affect the field.
                if (stackHashSettings.NextContextId == 0)
                {
                    forceASave = true;
                    foreach (StackHashContextSettings contextSettings in stackHashSettings.ContextCollection)
                    {
                        if (contextSettings.Id >= stackHashSettings.NextContextId)
                        {
                            stackHashSettings.NextContextId = contextSettings.Id + 1;
                        }
                    }
                }


                // Decrypt the passwords if encrypted.
                foreach (StackHashContextSettings contextSettings in stackHashSettings.ContextCollection)
                {
                    if (contextSettings.EmailSettings == null)
                    {
                        contextSettings.EmailSettings = new StackHashEmailSettings();
                    }
                    else
                    {
                        // Decrypt the email username and password.
                        contextSettings.EmailSettings.SmtpSettings.SmtpUsername = SecurityUtils.DecryptStringWithUserCredentials(contextSettings.EmailSettings.SmtpSettings.SmtpUsername);
                        contextSettings.EmailSettings.SmtpSettings.SmtpPassword = SecurityUtils.DecryptStringWithUserCredentials(contextSettings.EmailSettings.SmtpSettings.SmtpPassword);
                    }

                    if (contextSettings.WinQualSettings.Password.Contains('-'))
                    {
                        contextSettings.WinQualSettings.Password =
                            SecurityUtils.DecryptStringWithUserCredentials(contextSettings.WinQualSettings.Password);
                        forceASave = true;
                    }

                    if ((contextSettings.SqlSettings != null) &&
                        (contextSettings.SqlSettings.ConnectionString != null) &&
                        (contextSettings.SqlSettings.ConnectionString.Length > 3) &&
                        (contextSettings.SqlSettings.ConnectionString[2] == '-'))
                    {
                        contextSettings.SqlSettings.ConnectionString =
                            SecurityUtils.DecryptStringWithUserCredentials(contextSettings.SqlSettings.ConnectionString);
                        forceASave = true;
                    }

                    if (contextSettings.WinQualSettings.ProductsToSynchronize == null)
                    {
                        contextSettings.WinQualSettings.ProductsToSynchronize = new StackHashProductSyncDataCollection();
                        forceASave = true;
                    }

                    // Added in beta 6.
                    if (contextSettings.WinQualSettings.SyncsBeforeResync == 0)
                    {
                        contextSettings.WinQualSettings.SyncsBeforeResync = WinQualSettings.DefaultSyncsBeforeResync;
                    }

                    // Version added in beta 6.
                    if (stackHashSettings.Version == 0)
                    {
                        // This condition only required to support intermediate beta releases.
                        if (contextSettings.WinQualSettings.RequestRetryCount == 0)
                        {
                            contextSettings.WinQualSettings.RequestRetryCount = 5;
                        }

                        if (contextSettings.WinQualSettings.RequestTimeout == 0)
                        {
                            contextSettings.WinQualSettings.RequestTimeout = 300000;
                        }

                        contextSettings.WinQualSettings.RetryAfterError           = true;
                        contextSettings.WinQualSettings.DelayBeforeRetryInSeconds = 30 * 60; // 30 minutes.

                        contextSettings.WinQualSettings.MaxCabDownloadFailuresBeforeAbort = 5;
                        forceASave = true;
                    }

                    if (contextSettings.PurgeOptionsCollection == null)
                    {
                        contextSettings.PurgeOptionsCollection = new StackHashPurgeOptionsCollection();
                        contextSettings.PurgeOptionsCollection.Add(new StackHashPurgeOptions());
                        contextSettings.PurgeOptionsCollection[0].AgeToPurge     = 180;
                        contextSettings.PurgeOptionsCollection[0].PurgeObject    = StackHashPurgeObject.PurgeGlobal;
                        contextSettings.PurgeOptionsCollection[0].PurgeCabFiles  = true;
                        contextSettings.PurgeOptionsCollection[0].PurgeDumpFiles = true;
                        forceASave = true;
                    }

                    if ((contextSettings.CollectionPolicy == null) || (contextSettings.CollectionPolicy.Count == 0))
                    {
                        contextSettings.CollectionPolicy = StackHashCollectionPolicyCollection.Default;
                        forceASave = true;
                    }

                    if (contextSettings.SqlSettings == null)
                    {
                        contextSettings.SqlSettings = StackHashSqlConfiguration.Default;
                    }

                    if ((contextSettings.ErrorIndexSettings != null) &&
                        (contextSettings.ErrorIndexSettings.Folder != null) &&
                        (contextSettings.ErrorIndexSettings.Name != null))
                    {
                        // Set the location if not already set.
                        if ((contextSettings.ErrorIndexSettings.Status == ErrorIndexStatus.Created) &&
                            (contextSettings.ErrorIndexSettings.Location == StackHashErrorIndexLocation.Unknown))
                        {
                            String indexFolder = Path.Combine(contextSettings.ErrorIndexSettings.Folder, contextSettings.ErrorIndexSettings.Name);
                            if (Directory.Exists(indexFolder))
                            {
                                String[] databaseFiles = Directory.GetFiles(indexFolder, "*.mdf");

                                if (databaseFiles.Length > 0)
                                {
                                    contextSettings.ErrorIndexSettings.Location = StackHashErrorIndexLocation.InCabFolder;
                                }
                                else
                                {
                                    contextSettings.ErrorIndexSettings.Location = StackHashErrorIndexLocation.OnSqlServer;
                                }

                                forceASave = true;
                            }
                        }
                    }

                    // Initialise the bug tracker settings.
                    if (contextSettings.BugTrackerSettings == null)
                    {
                        contextSettings.BugTrackerSettings = new StackHashBugTrackerPlugInSettings();
                        forceASave = true;
                    }
                    if (contextSettings.BugTrackerSettings.PlugInSettings == null)
                    {
                        contextSettings.BugTrackerSettings.PlugInSettings = new StackHashBugTrackerPlugInCollection();
                        forceASave = true;
                    }

                    foreach (StackHashBugTrackerPlugIn bugTrackerSettings in contextSettings.BugTrackerSettings.PlugInSettings)
                    {
                        if (bugTrackerSettings.Properties != null)
                        {
                            foreach (StackHashNameValuePair nameValuePair in bugTrackerSettings.Properties)
                            {
                                bool doEncrypt = false;
                                if (nameValuePair.Name != null)
                                {
                                    bool isEncryptedString = false;

                                    if ((nameValuePair.Name.Length > 3) && (nameValuePair.Name[2] == '-'))
                                    {
                                        isEncryptedString = true;
                                    }

                                    // In dev version of beta 9 the settings were not encrypted.
                                    if (doEncrypt || isEncryptedString)
                                    {
                                        try
                                        {
                                            nameValuePair.Name = SecurityUtils.DecryptStringWithUserCredentials(nameValuePair.Name);
                                            doEncrypt          = true;
                                        }
                                        catch (System.Exception ex)
                                        {
                                            // Might happen if the user has switched service login account.
                                            DiagnosticsHelper.LogException(DiagSeverity.Warning, "Unable to decrypt settings", ex);
                                        }
                                    }
                                }
                                if (nameValuePair.Value != null)
                                {
                                    if (doEncrypt)
                                    {
                                        nameValuePair.Value = SecurityUtils.DecryptStringWithUserCredentials(nameValuePair.Value);
                                    }
                                }
                            }
                        }
                    }
                }

                if (stackHashSettings.ProxySettings == null)
                {
                    stackHashSettings.ProxySettings = new StackHashProxySettings(false, false, null, 0, null, null, null);
                    forceASave = true;
                }

                // Decrypt the username and password settings for the proxy.
                if ((stackHashSettings.ProxySettings != null) && (stackHashSettings.ProxySettings.ProxyUserName != null))
                {
                    stackHashSettings.ProxySettings.ProxyUserName = SecurityUtils.DecryptStringWithUserCredentials(stackHashSettings.ProxySettings.ProxyUserName);
                }
                if ((stackHashSettings.ProxySettings != null) && (stackHashSettings.ProxySettings.ProxyPassword != null))
                {
                    stackHashSettings.ProxySettings.ProxyPassword = SecurityUtils.DecryptStringWithUserCredentials(stackHashSettings.ProxySettings.ProxyPassword);
                }
                if ((stackHashSettings.ProxySettings != null) && (stackHashSettings.ProxySettings.ProxyDomain != null))
                {
                    stackHashSettings.ProxySettings.ProxyDomain = SecurityUtils.DecryptStringWithUserCredentials(stackHashSettings.ProxySettings.ProxyDomain);
                }

                // Check the settings seem valid - if not disable the proxy.
                try
                {
                    if (stackHashSettings.ProxySettings.UseProxy)
                    {
                        new WebProxy(stackHashSettings.ProxySettings.ProxyHost, stackHashSettings.ProxySettings.ProxyPort);
                    }
                }
                catch (System.Exception ex)
                {
                    DiagnosticsHelper.LogException(DiagSeverity.Warning, "Invalid proxy settings - disabling", ex);
                    stackHashSettings.ProxySettings.UseProxy = false;
                    forceASave = true;
                }
            }
            finally
            {
                xmlFile.Close();
            }

            if (forceASave)
            {
                Save(stackHashSettings, fileName);
            }

            return(stackHashSettings);
        }
Exemple #2
0
        public static void Save(StackHashSettings settings, String fileName)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            settings.Version = CurrentVersion;

            if ((settings.ProxySettings != null) && (settings.ProxySettings.ProxyUserName != null))
            {
                settings.ProxySettings.ProxyUserName = SecurityUtils.EncryptStringWithUserCredentials(settings.ProxySettings.ProxyUserName);
            }
            if ((settings.ProxySettings != null) && (settings.ProxySettings.ProxyPassword != null))
            {
                settings.ProxySettings.ProxyPassword = SecurityUtils.EncryptStringWithUserCredentials(settings.ProxySettings.ProxyPassword);
            }
            if ((settings.ProxySettings != null) && (settings.ProxySettings.ProxyDomain != null))
            {
                settings.ProxySettings.ProxyDomain = SecurityUtils.EncryptStringWithUserCredentials(settings.ProxySettings.ProxyDomain);
            }


            // Encrypt the passwords and plug-in settings.
            foreach (StackHashContextSettings contextSettings in settings.ContextCollection)
            {
                contextSettings.WinQualSettings.Password = SecurityUtils.EncryptStringWithUserCredentials(contextSettings.WinQualSettings.Password);

                if ((contextSettings.BugTrackerSettings != null) && (contextSettings.BugTrackerSettings.PlugInSettings != null))
                {
                    foreach (StackHashBugTrackerPlugIn bugTrackerSettings in contextSettings.BugTrackerSettings.PlugInSettings)
                    {
                        if (bugTrackerSettings.Properties != null)
                        {
                            foreach (StackHashNameValuePair nameValuePair in bugTrackerSettings.Properties)
                            {
                                if (nameValuePair.Name != null)
                                {
                                    nameValuePair.Name = SecurityUtils.EncryptStringWithUserCredentials(nameValuePair.Name);
                                }
                                if (nameValuePair.Value != null)
                                {
                                    nameValuePair.Value = SecurityUtils.EncryptStringWithUserCredentials(nameValuePair.Value);
                                }
                            }
                        }
                    }
                }

                if ((contextSettings.SqlSettings != null) &&
                    (contextSettings.SqlSettings.ConnectionString != null))
                {
                    contextSettings.SqlSettings.ConnectionString =
                        SecurityUtils.EncryptStringWithUserCredentials(contextSettings.SqlSettings.ConnectionString);
                }

                if (contextSettings.EmailSettings == null)
                {
                    contextSettings.EmailSettings = new StackHashEmailSettings();
                }

                // Encrypt the email username and password.
                contextSettings.EmailSettings.SmtpSettings.SmtpUsername = SecurityUtils.EncryptStringWithUserCredentials(contextSettings.EmailSettings.SmtpSettings.SmtpUsername);
                contextSettings.EmailSettings.SmtpSettings.SmtpPassword = SecurityUtils.EncryptStringWithUserCredentials(contextSettings.EmailSettings.SmtpSettings.SmtpPassword);
            }

            // Simply serializes the specified data to an XML file.
            FileStream xmlFile = null;

            try
            {
                xmlFile = new FileStream(fileName, FileMode.Create, FileAccess.Write);

                s_Serializer.Serialize(xmlFile, settings);
                xmlFile.Flush();
            }
            finally
            {
                if (xmlFile != null)
                {
                    xmlFile.Close();
                }

                // Decrypt any settings that were encrypted.
                foreach (StackHashContextSettings contextSettings in settings.ContextCollection)
                {
                    contextSettings.WinQualSettings.Password = SecurityUtils.DecryptStringWithUserCredentials(contextSettings.WinQualSettings.Password);

                    if ((contextSettings.BugTrackerSettings != null) && (contextSettings.BugTrackerSettings.PlugInSettings != null))
                    {
                        foreach (StackHashBugTrackerPlugIn bugTrackerSettings in contextSettings.BugTrackerSettings.PlugInSettings)
                        {
                            if (bugTrackerSettings.Properties != null)
                            {
                                foreach (StackHashNameValuePair nameValuePair in bugTrackerSettings.Properties)
                                {
                                    if (nameValuePair.Name != null)
                                    {
                                        nameValuePair.Name = SecurityUtils.DecryptStringWithUserCredentials(nameValuePair.Name);
                                    }
                                    if (nameValuePair.Value != null)
                                    {
                                        nameValuePair.Value = SecurityUtils.DecryptStringWithUserCredentials(nameValuePair.Value);
                                    }
                                }
                            }
                        }
                    }

                    if ((contextSettings.SqlSettings != null) &&
                        (contextSettings.SqlSettings.ConnectionString != null))
                    {
                        contextSettings.SqlSettings.ConnectionString =
                            SecurityUtils.DecryptStringWithUserCredentials(contextSettings.SqlSettings.ConnectionString);
                    }

                    // Decrypt the email username and password.
                    contextSettings.EmailSettings.SmtpSettings.SmtpUsername = SecurityUtils.DecryptStringWithUserCredentials(contextSettings.EmailSettings.SmtpSettings.SmtpUsername);
                    contextSettings.EmailSettings.SmtpSettings.SmtpPassword = SecurityUtils.DecryptStringWithUserCredentials(contextSettings.EmailSettings.SmtpSettings.SmtpPassword);
                }

                if ((settings.ProxySettings != null) && (settings.ProxySettings.ProxyUserName != null))
                {
                    settings.ProxySettings.ProxyUserName = SecurityUtils.DecryptStringWithUserCredentials(settings.ProxySettings.ProxyUserName);
                }
                if ((settings.ProxySettings != null) && (settings.ProxySettings.ProxyPassword != null))
                {
                    settings.ProxySettings.ProxyPassword = SecurityUtils.DecryptStringWithUserCredentials(settings.ProxySettings.ProxyPassword);
                }
                if ((settings.ProxySettings != null) && (settings.ProxySettings.ProxyDomain != null))
                {
                    settings.ProxySettings.ProxyDomain = SecurityUtils.DecryptStringWithUserCredentials(settings.ProxySettings.ProxyDomain);
                }
            }
        }