private SettingValue ReadSettingsValue(XElement element, bool isPath) { var keyAttribute = element.Attribute(ConfigurationContants.KeyAttribute); var valueAttribute = element.Attribute(ConfigurationContants.ValueAttribute); if (keyAttribute == null || String.IsNullOrEmpty(keyAttribute.Value) || valueAttribute == null) { throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, Resources.UserSettings_UnableToParseConfigFile, ConfigFilePath)); } var value = valueAttribute.Value; Uri uri; if (isPath && Uri.TryCreate(value, UriKind.Relative, out uri)) { string configDirectory = Path.GetDirectoryName(ConfigFilePath); value = Path.Combine(Root, Path.Combine(configDirectory, value)); } var settingValue = new SettingValue(keyAttribute.Value, value, IsMachineWideSettings, _priority); foreach (var attribute in element.Attributes()) { // Add all attributes other than ConfigurationContants.KeyAttribute and ConfigurationContants.ValueAttribute to AdditionalValues if (!string.Equals(attribute.Name.LocalName, ConfigurationContants.KeyAttribute, StringComparison.Ordinal) && !string.Equals(attribute.Name.LocalName, ConfigurationContants.ValueAttribute, StringComparison.Ordinal)) { settingValue.AdditionalData[attribute.Name.LocalName] = attribute.Value; } } return(settingValue); }
private PackageSource ReadPackageSource(SettingValue setting, bool isEnabled) { var name = setting.Key; var packageSource = new PackageSource(setting.Value, name, isEnabled) { IsMachineWide = setting.IsMachineWide, MaxHttpRequestsPerSource = SettingsUtility.GetMaxHttpRequest(Settings) }; var credentials = ReadCredential(name); if (credentials != null) { packageSource.Credentials = credentials; } var trustedSource = ReadTrustedSource(name); if (trustedSource != null) { packageSource.TrustedSource = trustedSource; } packageSource.ProtocolVersion = ReadProtocolVersion(setting); packageSource.Origin = setting.Origin; return(packageSource); }
private static int ReadProtocolVersion(SettingValue setting) { string protocolVersionString; int protocolVersion; if (setting.AdditionalData.TryGetValue(ConfigurationContants.ProtocolVersionAttribute, out protocolVersionString) && int.TryParse(protocolVersionString, out protocolVersion)) { return(protocolVersion); } return(PackageSource.DefaultProtocolVersion); }
private PackageSource ReadPackageSource(SettingValue setting, bool isEnabled) { var name = setting.Key; var packageSource = new PackageSource(setting.Value, name, isEnabled) { IsMachineWide = setting.IsMachineWide }; var credentials = ReadCredential(name); if (credentials != null) { packageSource.Credentials = credentials; } packageSource.ProtocolVersion = ReadProtocolVersion(setting); packageSource.Origin = setting.Origin; return(packageSource); }
private PackageSource ReadPackageSource(SettingValue setting, bool isEnabled) { var name = setting.Key; var packageSource = new PackageSource(setting.Value, name, isEnabled) { IsMachineWide = setting.IsMachineWide }; var credentials = ReadCredential(name); if (credentials != null) { packageSource.UserName = credentials.Username; packageSource.Password = credentials.Password; packageSource.IsPasswordClearText = credentials.IsPasswordClearText; } packageSource.ProtocolVersion = ReadProtocolVersion(setting); return(packageSource); }
private void SaveTrustedSource(TrustedSource source, IEnumerable <TrustedSource> existingSources) { var matchingSource = existingSources .Where(s => string.Equals(s.SourceName, source.SourceName, StringComparison.OrdinalIgnoreCase)) .FirstOrDefault(); var settingValues = new List <SettingValue>(); if (source.ServiceIndex != null) { // use existing priority if present var priority = matchingSource?.ServiceIndex?.Priority ?? source.ServiceIndex.Priority ?? 0; var settingValue = new SettingValue(ConfigurationConstants.ServiceIndex, source.ServiceIndex.Value, isMachineWide: false, priority: priority); settingValues.Add(settingValue); } foreach (var cert in source.Certificates) { // use existing priority if present var priority = matchingSource?.Certificates.FirstOrDefault(c => c.Fingerprint == cert.Fingerprint)?.Priority ?? cert.Priority ?? 0; // cant save to machine wide settings var settingValue = new SettingValue(cert.Fingerprint, cert.SubjectName, isMachineWide: false, priority: priority); settingValue.AdditionalData.Add(ConfigurationConstants.FingerprintAlgorithm, cert.FingerprintAlgorithm.ToString()); settingValues.Add(settingValue); } if (matchingSource != null) { _settings.UpdateSubsections(ConfigurationConstants.TrustedSources, source.SourceName, settingValues); } else { _settings.SetNestedSettingValues(ConfigurationConstants.TrustedSources, source.SourceName, settingValues); } }
public void SavePackageSources(IEnumerable <PackageSource> sources) { // clear the old values // and write the new ones var sourcesToWrite = sources.Where(s => !s.IsMachineWide && s.IsPersistable); var existingSettings = (Settings.GetSettingValues(ConfigurationContants.PackageSources, isPath: true) ?? Enumerable.Empty <SettingValue>()).Where(setting => !setting.IsMachineWide).ToList(); existingSettings.RemoveAll(setting => !sources.Any(s => string.Equals(s.Name, setting.Key, StringComparison.OrdinalIgnoreCase))); var existingSettingsLookup = existingSettings.ToLookup(setting => setting.Key, StringComparer.OrdinalIgnoreCase); var existingDisabledSources = Settings.GetSettingValues(ConfigurationContants.DisabledPackageSources) ?? Enumerable.Empty <SettingValue>(); var existingDisabledSourcesLookup = existingDisabledSources.ToLookup(setting => setting.Key, StringComparer.OrdinalIgnoreCase); var sourceSettings = new List <SettingValue>(); var sourcesToDisable = new List <SettingValue>(); foreach (var source in sourcesToWrite) { var foundSettingWithSourcePriority = false; var settingPriority = 0; var existingSettingForSource = existingSettingsLookup[source.Name]; // Preserve packageSource entries from low priority settings. foreach (var existingSetting in existingSettingForSource) { settingPriority = Math.Max(settingPriority, existingSetting.Priority); // Write all settings other than the currently written one to the current NuGet.config. if (ReadProtocolVersion(existingSetting) == source.ProtocolVersion) { // Update the source value of all settings with the same protocol version. existingSetting.Value = source.Source; foundSettingWithSourcePriority = true; } sourceSettings.Add(existingSetting); } if (!foundSettingWithSourcePriority) { // This is a new source, add it to the Setting with the lowest priority. var settingValue = new SettingValue(source.Name, source.Source, isMachineWide: false); if (source.ProtocolVersion != PackageSource.DefaultProtocolVersion) { settingValue.AdditionalData[ConfigurationContants.ProtocolVersionAttribute] = source.ProtocolVersion.ToString(CultureInfo.InvariantCulture); } sourceSettings.Add(settingValue); } // settingValue contains the setting with the highest priority. var existingDisabledSettings = existingDisabledSourcesLookup[source.Name]; // Preserve disabledPackageSource entries from low priority settings. foreach (var setting in existingDisabledSettings.Where(s => s.Priority < settingPriority)) { sourcesToDisable.Add(setting); } if (!source.IsEnabled) { // Add an entry to the disabledPackageSource in the file that contains sourcesToDisable.Add(new SettingValue(source.Name, "true", isMachineWide: false, priority: settingPriority)); } } // Write the updates to the nearest settings file. Settings.UpdateSections(ConfigurationContants.PackageSources, sourceSettings); // overwrite new values for the <disabledPackageSources> section Settings.UpdateSections(ConfigurationContants.DisabledPackageSources, sourcesToDisable); // Overwrite the <packageSourceCredentials> section Settings.DeleteSection(ConfigurationContants.CredentialsSectionName); var sourceWithCredentials = sources.Where(s => !String.IsNullOrEmpty(s.UserName) && !String.IsNullOrEmpty(s.Password)); foreach (var source in sourceWithCredentials) { Settings.SetNestedValues(ConfigurationContants.CredentialsSectionName, source.Name, new[] { new KeyValuePair <string, string>(ConfigurationContants.UsernameToken, source.UserName), ReadPasswordValues(source) }); } OnPackageSourcesChanged(); }
public void SavePackageSources(IEnumerable <PackageSource> sources) { // clear the old values // and write the new ones var sourcesToWrite = sources.Where(s => !s.IsMachineWide && s.IsPersistable); var existingSettings = (Settings.GetSettingValues(ConfigurationConstants.PackageSources, isPath: true) ?? Enumerable.Empty <SettingValue>()).Where(setting => !setting.IsMachineWide).ToList(); var minPriority = 0; // get lowest priority in existingSetting if (existingSettings.Count > 0) { minPriority = existingSettings.Min(setting => setting.Priority); } existingSettings.RemoveAll(setting => !sources.Any(s => string.Equals(s.Name, setting.Key, StringComparison.OrdinalIgnoreCase))); var existingSettingsLookup = existingSettings.ToLookup(setting => setting.Key, StringComparer.OrdinalIgnoreCase); var existingDisabledSources = Settings.GetSettingValues(ConfigurationConstants.DisabledPackageSources) ?? Enumerable.Empty <SettingValue>(); var existingDisabledSourcesLookup = existingDisabledSources.ToLookup(setting => setting.Key, StringComparer.OrdinalIgnoreCase); var sourceSettings = new List <SettingValue>(); var sourcesToDisable = new List <SettingValue>(); foreach (var source in sourcesToWrite) { var foundSettingWithSourcePriority = false; var settingPriority = 0; var existingSettingForSource = existingSettingsLookup[source.Name]; // Preserve packageSource entries from low priority settings. foreach (var existingSetting in existingSettingForSource) { settingPriority = Math.Max(settingPriority, existingSetting.Priority); // Write all settings other than the currently written one to the current NuGet.config. if (ReadProtocolVersion(existingSetting) == source.ProtocolVersion) { // Update the source value of all settings with the same protocol version. foundSettingWithSourcePriority = true; // if the existing source changed, update the setting value if (!existingSetting.Value.Equals(source.Source)) { existingSetting.Value = source.Source; existingSetting.OriginalValue = source.Source; } } sourceSettings.Add(existingSetting); } if (!foundSettingWithSourcePriority) { // This is a new source, add it to the Setting with the lowest priority. // if there is a clear tag in one config file, new source will be cleared // we should set new source priority to lowest existingSetting priority // NOTE: origin can be null here because it isn't ever used when saving. var settingValue = new SettingValue(source.Name, source.Source, origin: null, isMachineWide: false, priority: minPriority); if (source.ProtocolVersion != PackageSource.DefaultProtocolVersion) { settingValue.AdditionalData[ConfigurationConstants.ProtocolVersionAttribute] = source.ProtocolVersion.ToString(CultureInfo.InvariantCulture); } sourceSettings.Add(settingValue); } // settingValue contains the setting with the highest priority. var existingDisabledSettings = existingDisabledSourcesLookup[source.Name]; // Preserve disabledPackageSource entries from low priority settings. foreach (var setting in existingDisabledSettings.Where(s => s.Priority < settingPriority)) { sourcesToDisable.Add(setting); } if (!source.IsEnabled) { // Add an entry to the disabledPackageSource in the file that contains sourcesToDisable.Add(new SettingValue(source.Name, "true", origin: null, isMachineWide: false, priority: settingPriority)); } } // add entries to the disabledPackageSource for machine wide setting foreach (var source in sources.Where(s => s.IsMachineWide && !s.IsEnabled)) { sourcesToDisable.Add(new SettingValue(source.Name, "true", origin: null, isMachineWide: true, priority: 0)); } // add entries to the disablePackageSource for disabled package sources that are not in loaded 'sources' foreach (var setting in existingDisabledSources) { // The following code ensures that we do not miss to mark an existing disabled source as disabled. // However, ONLY mark an existing disable source setting as disabled, if, // 1) it is not in the list of loaded package sources, or, // 2) it is not already in the list of sources to disable. if (!sources.Any(s => string.Equals(s.Name, setting.Key, StringComparison.OrdinalIgnoreCase)) && !sourcesToDisable.Any(s => string.Equals(s.Key, setting.Key, StringComparison.OrdinalIgnoreCase) && s.Priority == setting.Priority)) { sourcesToDisable.Add(setting); } } // Write the updates to the nearest settings file. Settings.UpdateSections(ConfigurationConstants.PackageSources, sourceSettings); // overwrite new values for the <disabledPackageSources> section Settings.UpdateSections(ConfigurationConstants.DisabledPackageSources, sourcesToDisable); // Overwrite the <packageSourceCredentials> section Settings.DeleteSection(ConfigurationConstants.CredentialsSectionName); foreach (var source in sources.Where(s => s.Credentials != null && s.Credentials.IsValid())) { Settings.SetNestedValues( ConfigurationConstants.CredentialsSectionName, source.Name, GetCredentialValues(source.Credentials)); } // Update/Add trusted sources // Deletion of a trusted source should be done separately using TrustedSourceProvider.DeleteSource() var trustedSources = sources .Where(s => s.TrustedSource != null) .Select(s => s.TrustedSource); if (trustedSources.Any()) { _trustedSourceProvider.SaveTrustedSources(trustedSources); } OnPackageSourcesChanged(); }
private static void LoadUserSpecificSettings( List <Settings> validSettingFiles, string root, string configFileName, IMachineWideSettings machineWideSettings, bool useTestingGlobalPath ) { if (root == null) { // Path.Combine is performed with root so it should not be null // However, it is legal for it be empty in this method root = String.Empty; } // for the default location, allow case where file does not exist, in which case it'll end // up being created if needed Settings appDataSettings = null; if (configFileName == null) { var defaultSettingsFilePath = String.Empty; if (useTestingGlobalPath) { defaultSettingsFilePath = Path.Combine(root, "TestingGlobalPath", DefaultSettingsFileName); } else { var userSettingsDir = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory); // If there is no user settings directory, return no appdata settings if (userSettingsDir == null) { return; } defaultSettingsFilePath = Path.Combine(userSettingsDir, DefaultSettingsFileName); } if (!File.Exists(defaultSettingsFilePath) && machineWideSettings != null) { // Since defaultSettingsFilePath is a full path, so it doesn't matter what value is // used as root for the PhysicalFileSystem. appDataSettings = ReadSettings( root, defaultSettingsFilePath); // Disable machinewide sources to improve perf var disabledSources = new List <SettingValue>(); foreach (var setting in machineWideSettings.Settings) { var values = setting.GetSettingValues(ConfigurationConstants.PackageSources, isPath: true); foreach (var value in values) { var packageSource = new PackageSource(value.Value); // if the machine wide package source is http source, disable it by default if (packageSource.IsHttp) { disabledSources.Add(new SettingValue(value.Key, "true", origin: setting, isMachineWide: true, priority: 0)); } } } appDataSettings.UpdateSections(ConfigurationConstants.DisabledPackageSources, disabledSources); } else { appDataSettings = ReadSettings(root, defaultSettingsFilePath); bool IsEmptyConfig = !appDataSettings.GetSettingValues(ConfigurationConstants.PackageSources).Any(); if (IsEmptyConfig) { var trackFilePath = Path.Combine(Path.GetDirectoryName(defaultSettingsFilePath), NuGetConstants.AddV3TrackFile); if (!File.Exists(trackFilePath)) { File.Create(trackFilePath).Dispose(); var defaultPackageSource = new SettingValue(NuGetConstants.FeedName, NuGetConstants.V3FeedUrl, isMachineWide: false); defaultPackageSource.AdditionalData.Add(ConfigurationConstants.ProtocolVersionAttribute, "3"); appDataSettings.UpdateSections(ConfigurationConstants.PackageSources, new List <SettingValue> { defaultPackageSource }); } } } } else { if (!FileSystemUtility.DoesFileExistIn(root, configFileName)) { var message = String.Format(CultureInfo.CurrentCulture, Resources.FileDoesNotExist, Path.Combine(root, configFileName)); throw new InvalidOperationException(message); } appDataSettings = ReadSettings(root, configFileName); } if (appDataSettings != null) { validSettingFiles.Add(appDataSettings); } }
public void SavePackageSources(IEnumerable<PackageSource> sources) { // clear the old values // and write the new ones var sourcesToWrite = sources.Where(s => !s.IsMachineWide && s.IsPersistable); var existingSettings = (Settings.GetSettingValues(ConfigurationContants.PackageSources, isPath: true) ?? Enumerable.Empty<SettingValue>()).Where(setting => !setting.IsMachineWide).ToList(); existingSettings.RemoveAll(setting => !sources.Any(s => string.Equals(s.Name, setting.Key, StringComparison.OrdinalIgnoreCase))); var existingSettingsLookup = existingSettings.ToLookup(setting => setting.Key, StringComparer.OrdinalIgnoreCase); var existingDisabledSources = Settings.GetSettingValues(ConfigurationContants.DisabledPackageSources) ?? Enumerable.Empty<SettingValue>(); var existingDisabledSourcesLookup = existingDisabledSources.ToLookup(setting => setting.Key, StringComparer.OrdinalIgnoreCase); var sourceSettings = new List<SettingValue>(); var sourcesToDisable = new List<SettingValue>(); foreach (var source in sourcesToWrite) { var foundSettingWithSourcePriority = false; var settingPriority = 0; var existingSettingForSource = existingSettingsLookup[source.Name]; // Preserve packageSource entries from low priority settings. foreach (var existingSetting in existingSettingForSource) { settingPriority = Math.Max(settingPriority, existingSetting.Priority); // Write all settings other than the currently written one to the current NuGet.config. if (ReadProtocolVersion(existingSetting) == source.ProtocolVersion) { // Update the source value of all settings with the same protocol version. existingSetting.Value = source.Source; foundSettingWithSourcePriority = true; } sourceSettings.Add(existingSetting); } if (!foundSettingWithSourcePriority) { // This is a new source, add it to the Setting with the lowest priority. var settingValue = new SettingValue(source.Name, source.Source, isMachineWide: false); if (source.ProtocolVersion != PackageSource.DefaultProtocolVersion) { settingValue.AdditionalData[ConfigurationContants.ProtocolVersionAttribute] = source.ProtocolVersion.ToString(CultureInfo.InvariantCulture); } sourceSettings.Add(settingValue); } // settingValue contains the setting with the highest priority. var existingDisabledSettings = existingDisabledSourcesLookup[source.Name]; // Preserve disabledPackageSource entries from low priority settings. foreach (var setting in existingDisabledSettings.Where(s => s.Priority < settingPriority)) { sourcesToDisable.Add(setting); } if (!source.IsEnabled) { // Add an entry to the disabledPackageSource in the file that contains sourcesToDisable.Add(new SettingValue(source.Name, "true", isMachineWide: false, priority: settingPriority)); } } // Write the updates to the nearest settings file. Settings.UpdateSections(ConfigurationContants.PackageSources, sourceSettings); // overwrite new values for the <disabledPackageSources> section Settings.UpdateSections(ConfigurationContants.DisabledPackageSources, sourcesToDisable); // Overwrite the <packageSourceCredentials> section Settings.DeleteSection(ConfigurationContants.CredentialsSectionName); var sourceWithCredentials = sources.Where(s => !String.IsNullOrEmpty(s.UserName) && !String.IsNullOrEmpty(s.Password)); foreach (var source in sourceWithCredentials) { Settings.SetNestedValues(ConfigurationContants.CredentialsSectionName, source.Name, new[] { new KeyValuePair<string, string>(ConfigurationContants.UsernameToken, source.UserName), ReadPasswordValues(source) }); } OnPackageSourcesChanged(); }
private static int ReadProtocolVersion(SettingValue setting) { string protocolVersionString; int protocolVersion; if (setting.AdditionalData.TryGetValue(ConfigurationContants.ProtocolVersionAttribute, out protocolVersionString) && int.TryParse(protocolVersionString, out protocolVersion)) { return protocolVersion; } return PackageSource.DefaultProtocolVersion; }
private PackageSource ReadPackageSource(SettingValue setting, bool isEnabled) { var name = setting.Key; var packageSource = new PackageSource(setting.Value, name, isEnabled) { IsMachineWide = setting.IsMachineWide }; var credentials = ReadCredential(name); if (credentials != null) { packageSource.UserName = credentials.Username; packageSource.Password = credentials.Password; packageSource.IsPasswordClearText = credentials.IsPasswordClearText; } packageSource.ProtocolVersion = ReadProtocolVersion(setting); return packageSource; }
private SettingValue ReadSettingsValue(XElement element, bool isPath) { var keyAttribute = element.Attribute(ConfigurationContants.KeyAttribute); var valueAttribute = element.Attribute(ConfigurationContants.ValueAttribute); if (keyAttribute == null || String.IsNullOrEmpty(keyAttribute.Value) || valueAttribute == null) { throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, Resources.UserSettings_UnableToParseConfigFile, ConfigFilePath)); } var value = valueAttribute.Value; Uri uri; if (isPath && Uri.TryCreate(value, UriKind.Relative, out uri)) { var configDirectory = Path.GetDirectoryName(ConfigFilePath); value = Path.Combine(Root, Path.Combine(configDirectory, value)); } var settingValue = new SettingValue(keyAttribute.Value, value, IsMachineWideSettings, _priority); foreach (var attribute in element.Attributes()) { // Add all attributes other than ConfigurationContants.KeyAttribute and ConfigurationContants.ValueAttribute to AdditionalValues if (!string.Equals(attribute.Name.LocalName, ConfigurationContants.KeyAttribute, StringComparison.Ordinal) && !string.Equals(attribute.Name.LocalName, ConfigurationContants.ValueAttribute, StringComparison.Ordinal)) { settingValue.AdditionalData[attribute.Name.LocalName] = attribute.Value; } } return settingValue; }