Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zhili1208 committed Mar 4, 2016
1 parent 1245de1 commit c074067
Show file tree
Hide file tree
Showing 14 changed files with 446 additions and 34 deletions.
10 changes: 4 additions & 6 deletions src/NuGet.Clients/Options/PackageSourcesOptionsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Windows.Forms.VisualStyles;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using NuGet.Common;
using NuGet.PackageManagement.VisualStudio;
using NuGet.Protocol.Core.Types;

Expand Down Expand Up @@ -227,15 +228,12 @@ internal bool ApplyChangedSettings()
_packageSourceProvider.SavePackageSources(packageSources);
}
}
catch (InvalidOperationException)
catch (Configuration.NuGetConfigurationException e)
{
MessageHelper.ShowErrorMessage(Resources.ShowError_ConfigInvalidOperation, Resources.ErrorDialogBoxTitle);
MessageHelper.ShowErrorMessage(ExceptionUtilities.DisplayMessage(e), Resources.ErrorDialogBoxTitle);
return false;
}
catch (UnauthorizedAccessException)
{
MessageHelper.ShowErrorMessage(Resources.ShowError_ConfigUnauthorizedAccess, Resources.ErrorDialogBoxTitle);
}

// find the enabled package source
return true;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/NuGet.Clients/PackageManagement.VisualStudio/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,7 @@
<data name="FailedToUpdateBindingRedirects" xml:space="preserve">
<value>Failed to update binding redirects for {0} : {1}</value>
</data>
<data name="ConfigErrorDialogBoxTitle" xml:space="preserve">
<value>NuGet operation failed</value>
</data>
</root>
10 changes: 9 additions & 1 deletion src/NuGet.Clients/PackageManagement.VisualStudio/VSSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using NuGet.Common;

namespace NuGet.PackageManagement.VisualStudio
{
Expand Down Expand Up @@ -50,7 +51,14 @@ private void ResetSolutionSettings()
{
root = Path.Combine(SolutionManager.SolutionDirectory, EnvDTEProjectUtility.NuGetSolutionSettingsFolder);
}
SolutionSettings = Configuration.Settings.LoadDefaultSettings(root, configFileName: null, machineWideSettings: MachineWideSettings);
try
{
SolutionSettings = Configuration.Settings.LoadDefaultSettings(root, configFileName: null, machineWideSettings: MachineWideSettings);
}
catch (Configuration.NuGetConfigurationException ex)
{
MessageHelper.ShowErrorMessage(ExceptionUtilities.DisplayMessage(ex), Strings.ConfigErrorDialogBoxTitle);
}
}

private void OnSolutionOpenedOrClosed(object sender, EventArgs e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NuGet.Configuration
{
public class NuGetConfigurationException : Exception
{
public NuGetConfigurationException(string message)
: base(message)
{
}

public NuGetConfigurationException(string message, Exception innerException)
: base(message, innerException)
{
}
}
}
28 changes: 26 additions & 2 deletions src/NuGet.Core/NuGet.Configuration/PackageSource/PackageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Globalization;

namespace NuGet.Configuration
{
Expand Down Expand Up @@ -32,7 +33,30 @@ public class PackageSource : IEquatable<PackageSource>

public string UserName { get; set; }

public string Password { get; set; }
public string Password
{
get
{
if (PasswordText != null && !IsPasswordClearText)
{
try
{
return EncryptionUtility.DecryptString(PasswordText);
}
catch (NotSupportedException e)
{
throw new NuGetConfigurationException(
string.Format(CultureInfo.CurrentCulture, Resources.UnsupportedDecryptPassword, Source), e);
}
}
else
{
return PasswordText;
}
}
}

public string PasswordText { get; set; }

public bool IsPasswordClearText { get; set; }

Expand Down Expand Up @@ -167,7 +191,7 @@ public PackageSource Clone()
{
Description = Description,
UserName = UserName,
Password = Password,
PasswordText = PasswordText,
IsPasswordClearText = IsPasswordClearText,
IsMachineWide = IsMachineWide,
ProtocolVersion = ProtocolVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private PackageSource ReadPackageSource(SettingValue setting, bool isEnabled)
if (credentials != null)
{
packageSource.UserName = credentials.Username;
packageSource.Password = credentials.Password;
packageSource.PasswordText = credentials.PasswordText;
packageSource.IsPasswordClearText = credentials.IsPasswordClearText;
}

Expand Down Expand Up @@ -211,7 +211,7 @@ private PackageSourceCredential ReadCredential(string sourceName)
var encryptedPassword = values.FirstOrDefault(k => k.Key.Equals(ConfigurationConstants.PasswordToken, StringComparison.OrdinalIgnoreCase)).Value;
if (!String.IsNullOrEmpty(encryptedPassword))
{
return new PackageSourceCredential(userName, EncryptionUtility.DecryptString(encryptedPassword), isPasswordClearText: false);
return new PackageSourceCredential(userName, encryptedPassword, isPasswordClearText: false);
}

var clearTextPassword = values.FirstOrDefault(k => k.Key.Equals(ConfigurationConstants.ClearTextPasswordToken, StringComparison.Ordinal)).Value;
Expand Down Expand Up @@ -393,7 +393,7 @@ public void SavePackageSources(IEnumerable<PackageSource> sources)
// Overwrite the <packageSourceCredentials> section
Settings.DeleteSection(ConfigurationConstants.CredentialsSectionName);

var sourceWithCredentials = sources.Where(s => !String.IsNullOrEmpty(s.UserName) && !String.IsNullOrEmpty(s.Password));
var sourceWithCredentials = sources.Where(s => !String.IsNullOrEmpty(s.UserName) && !String.IsNullOrEmpty(s.PasswordText));
foreach (var source in sourceWithCredentials)
{
Settings.SetNestedValues(ConfigurationConstants.CredentialsSectionName, source.Name, new[]
Expand All @@ -419,10 +419,18 @@ private void OnPackageSourcesChanged()

private static KeyValuePair<string, string> ReadPasswordValues(PackageSource source)
{
var passwordToken = source.IsPasswordClearText ? ConfigurationConstants.ClearTextPasswordToken : ConfigurationConstants.PasswordToken;
var passwordValue = source.IsPasswordClearText ? source.Password : EncryptionUtility.EncryptString(source.Password);
try
{
var passwordToken = source.IsPasswordClearText ? ConfigurationConstants.ClearTextPasswordToken : ConfigurationConstants.PasswordToken;
var passwordValue = source.IsPasswordClearText ? source.PasswordText : EncryptionUtility.EncryptString(source.PasswordText);

return new KeyValuePair<string, string>(passwordToken, passwordValue);
return new KeyValuePair<string, string>(passwordToken, passwordValue);
}
catch (NotSupportedException e)
{
throw new NuGetConfigurationException(
string.Format(CultureInfo.CurrentCulture, Resources.UnsupportedEncryptPassword, source.Source), e);
}
}

public void DisablePackageSource(PackageSource source)
Expand Down Expand Up @@ -487,14 +495,14 @@ private class PackageSourceCredential
{
public string Username { get; private set; }

public string Password { get; private set; }
public string PasswordText { get; private set; }

public bool IsPasswordClearText { get; private set; }

public PackageSourceCredential(string username, string password, bool isPasswordClearText)
public PackageSourceCredential(string username, string passwordText, bool isPasswordClearText)
{
Username = username;
Password = password;
PasswordText = passwordText;
IsPasswordClearText = isPasswordClearText;
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/NuGet.Core/NuGet.Configuration/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@
<data name="Settings_FileName_Cannot_Be_A_Path" xml:space="preserve">
<value>Parameter 'fileName' to Settings must be just a fileName and not a path</value>
</data>
<data name="ShowError_ConfigInvalidOperation" xml:space="preserve">
<value>NuGet.Config is malformed. Path: '{0}'.</value>
</data>
<data name="ShowError_ConfigInvalidXml" xml:space="preserve">
<value>NuGet.Config is invalid XML. Path: '{0}'.</value>
</data>
<data name="ShowError_ConfigRootInvalid" xml:space="preserve">
<value>NuGet.Config does not contain the expected root element: 'configuration'. Path: '{0}'.</value>
</data>
<data name="ShowError_ConfigUnauthorizedAccess" xml:space="preserve">
<value>Failed to read NuGet.Config due to unaothirized access. Path: '{0}'.</value>
</data>
<data name="Unknown_Config_Exception" xml:space="preserve">
<value>Unexpected failure reading NuGet.Config. Path: '{0}'.</value>
</data>
<data name="UnsupportedDecryptPassword" xml:space="preserve">
<value>Password decryption failed for source : '{0}' is unsupported on this platform. A clear text password may be used instead.</value>
</data>
<data name="UnsupportedEncryptPassword" xml:space="preserve">
<value>Password encryption failed for source : '{0}' is unsupported on this platform. A clear text password may be used instead.</value>
</data>
<data name="UnsupportedHashAlgorithm" xml:space="preserve">
<value>Hash algorithm '{0}' is unsupported. Supported algorithms include: SHA512 and SHA256.</value>
</data>
Expand Down
57 changes: 57 additions & 0 deletions src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public Settings(string root, string fileName, bool isMachineWideSettings)
ExecuteSynchronized(() => config = XmlUtility.GetOrCreateDocument(CreateDefaultConfig(), ConfigFilePath));
ConfigXDocument = config;
IsMachineWideSettings = isMachineWideSettings;
CheckConfigRoot();
}

public event EventHandler SettingsChanged = delegate { };
Expand Down Expand Up @@ -1034,6 +1035,29 @@ private void ExecuteSynchronized(Action ioOperation)
owner = _globalMutex.WaitOne(TimeSpan.FromMinutes(1));
ioOperation();
}
catch (InvalidOperationException e)
{
throw new NuGetConfigurationException(
string.Format(CultureInfo.CurrentCulture,Resources.ShowError_ConfigInvalidOperation, ConfigFilePath, e.Message), e);
}

catch (UnauthorizedAccessException e)
{
throw new NuGetConfigurationException(
string.Format(CultureInfo.CurrentCulture,Resources.ShowError_ConfigUnauthorizedAccess, ConfigFilePath, e.Message), e);
}

catch (XmlException e)
{
throw new NuGetConfigurationException(
string.Format(CultureInfo.CurrentCulture,Resources.ShowError_ConfigInvalidXml, ConfigFilePath, e.Message), e);
}

catch (Exception e)
{
throw new NuGetConfigurationException(
string.Format(CultureInfo.CurrentCulture,Resources.Unknown_Config_Exception, ConfigFilePath, e.Message), e);
}
finally
{
if (owner)
Expand Down Expand Up @@ -1070,6 +1094,29 @@ private void ExecuteSynchronizedCore(Action ioOperation)
// back than hang
ioOperation();
}
catch (InvalidOperationException e)
{
throw new NuGetConfigurationException(
string.Format(CultureInfo.CurrentCulture, Resources.ShowError_ConfigInvalidOperation, fileName, e.Message), e);
}

catch (UnauthorizedAccessException e)
{
throw new NuGetConfigurationException(
string.Format(CultureInfo.CurrentCulture, Resources.ShowError_ConfigUnauthorizedAccess, fileName, e.Message), e);
}

catch (XmlException e)
{
throw new NuGetConfigurationException(
string.Format(CultureInfo.CurrentCulture, Resources.ShowError_ConfigInvalidXml, fileName, e.Message), e);
}

catch (Exception e)
{
throw new NuGetConfigurationException(
string.Format(CultureInfo.CurrentCulture, Resources.Unknown_Config_Exception, fileName, e.Message), e);
}
finally
{
if (owner)
Expand Down Expand Up @@ -1124,5 +1171,15 @@ private static bool FoundClearTag(XDocument config)
}
return false;
}

// this method will check NuGet.Config file, if the root is not configuration, it will throw.
private void CheckConfigRoot()
{
if (ConfigXDocument.Root.Name != "configuration")
{
throw new NuGetConfigurationException(
string.Format(Resources.ShowError_ConfigRootInvalid, ConfigFilePath));
}
}
}
}
Loading

0 comments on commit c074067

Please sign in to comment.