Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled password encryption on .NET Core/Windows #813

Merged
merged 1 commit into from
Aug 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,17 @@ do
if grep -q "netcoreapp1.0" "$testProject"; then
pushd $testDir

echo "$DOTNET test $testDir --configuration release --framework netcoreapp1.0"
$DOTNET test $testDir --configuration release --framework netcoreapp1.0
case "$(uname -s)" in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't build.sh on Mac have -notrait Platform=Linux? And similar for build.sh on Linux?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! It definitely should.

Linux)
echo "$DOTNET test $testDir --configuration release --framework netcoreapp1.0 -notrait Platform=Windows -notrait Platform=Darwin"
$DOTNET test $testDir --configuration release --framework netcoreapp1.0 -notrait Platform=Windows -notrait Platform=Darwin
;;
Darwin)
echo "$DOTNET test $testDir --configuration release --framework netcoreapp1.0 -notrait Platform=Windows -notrait Platform=Linux"
$DOTNET test $testDir --configuration release --framework netcoreapp1.0 -notrait Platform=Windows -notrait Platform=Linux
;;
*) ;;
esac

if [ $? -ne 0 ]; then
echo "$testDir FAILED on CoreCLR"
Expand Down
4 changes: 3 additions & 1 deletion build/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ Function Test-XProject {
$opts += '-v'
}
$opts += 'test', '--configuration', $Configuration, '--framework', 'netcoreapp1.0'
$opts += '-notrait', 'Platform=Linux', '-notrait', 'Platform=Darwin'
if ($VerbosePreference) {
$opts += '-verbose'
}
Expand Down Expand Up @@ -469,6 +470,7 @@ Function Test-XProject {
$htmlOutput = Join-Path $_ "bin\$Configuration\net46\win7-x64\xunit.results.html"
$desktopTestAssembly = Join-Path $_ "bin\$Configuration\net46\win7-x64\$directoryName.dll"
$opts = $desktopTestAssembly, '-html', $htmlOutput
$opts += '-notrait', 'Platform=Linux', '-notrait', 'Platform=Darwin'
if ($VerbosePreference) {
$opts += '-verbose'
}
Expand Down Expand Up @@ -620,7 +622,7 @@ Function Invoke-ILMerge {
if (-Not (Test-Path $outputFolder)) {
New-Item -ItemType Directory -Path $outputFolder | Out-Null
}

$includeList = Read-FileList (Join-Path $buildArtifactsFolder '.mergeinclude')
$notInList = $buildArtifacts | ?{ -not ($includeList -contains $_) }
if ($notInList) {
Expand Down
19 changes: 10 additions & 9 deletions src/NuGet.Core/NuGet.Configuration/Proxy/ProxyCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,24 @@ private bool TryAddProxyCredentialsToCache(WebProxy configuredProxy)

public WebProxy GetUserConfiguredProxy()
{
// Try reading from the settings. The values are stored as 3 config values http_proxy, http_proxy_user, http_proxy_password
// Try reading from the settings. The values are stored as 3 config values http_proxy, http_proxy.user, http_proxy.password
var host = _settings.GetValue(SettingsUtility.ConfigSection, ConfigurationConstants.HostKey);
if (!string.IsNullOrEmpty(host))
{
// The host is the minimal value we need to assume a user configured proxy.
var webProxy = new WebProxy(host);

#if !IS_CORECLR
var userName = _settings.GetValue(SettingsUtility.ConfigSection, ConfigurationConstants.UserKey);
var password = SettingsUtility.GetDecryptedValue(_settings, SettingsUtility.ConfigSection, ConfigurationConstants.PasswordKey);

if (!string.IsNullOrEmpty(userName)
&& !string.IsNullOrEmpty(password))
if (RuntimeEnvironmentHelper.IsWindows)
{
webProxy.Credentials = new NetworkCredential(userName, password);
var userName = _settings.GetValue(SettingsUtility.ConfigSection, ConfigurationConstants.UserKey);
var password = SettingsUtility.GetDecryptedValue(_settings, SettingsUtility.ConfigSection, ConfigurationConstants.PasswordKey);

if (!string.IsNullOrEmpty(userName)
&& !string.IsNullOrEmpty(password))
{
webProxy.Credentials = new NetworkCredential(userName, password);
}
}
#endif

var noProxy = _settings.GetValue(SettingsUtility.ConfigSection, ConfigurationConstants.NoProxy);
if (!string.IsNullOrEmpty(noProxy))
Expand Down
13 changes: 11 additions & 2 deletions src/NuGet.Core/NuGet.Configuration/Resources.Designer.cs

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.Core/NuGet.Configuration/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
<data name="Argument_Cannot_Be_Null_Or_Empty" xml:space="preserve">
<value>Value cannot be null or empty string.</value>
</data>
<data name="Error_EncryptionUnsupported" xml:space="preserve">
<value>Encryption is not supported on non-Windows platforms.</value>
</data>
<data name="Error_NoWritableConfig" xml:space="preserve">
<value>There are no writable config files.</value>
</data>
Expand Down
18 changes: 10 additions & 8 deletions src/NuGet.Core/NuGet.Configuration/Utility/EncryptionUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,27 @@ public static class EncryptionUtility

public static string EncryptString(string value)
{
#if IS_CORECLR
throw new NotSupportedException();
#else
if (!RuntimeEnvironmentHelper.IsWindows)
{
throw new NotSupportedException(Resources.Error_EncryptionUnsupported);
}

var decryptedByteArray = Encoding.UTF8.GetBytes(value);
var encryptedByteArray = ProtectedData.Protect(decryptedByteArray, _entropyBytes, DataProtectionScope.CurrentUser);
var encryptedString = Convert.ToBase64String(encryptedByteArray);
return encryptedString;
#endif
}

public static string DecryptString(string encryptedString)
{
#if IS_CORECLR
throw new NotSupportedException();
#else
if (!RuntimeEnvironmentHelper.IsWindows)
{
throw new NotSupportedException(Resources.Error_EncryptionUnsupported);
}

var encryptedByteArray = Convert.FromBase64String(encryptedString);
var decryptedByteArray = ProtectedData.Unprotect(encryptedByteArray, _entropyBytes, DataProtectionScope.CurrentUser);
return Encoding.UTF8.GetString(decryptedByteArray);
#endif
}

public static string GenerateUniqueToken(string caseInsensitiveKey)
Expand Down
1 change: 1 addition & 0 deletions src/NuGet.Core/NuGet.Configuration/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"netstandard1.3": {
"dependencies": {
"NETStandard.Library": "1.6.0",
"System.Security.Cryptography.ProtectedData": "4.0.0",
"System.Xml.XDocument": "4.0.11"
},
"buildOptions": {
Expand Down
15 changes: 15 additions & 0 deletions src/NuGet.Core/NuGet.Test.Utility/Traits/Platform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace NuGet.Test.Utility
{
/// <summary>
/// Platform names used to filter test cases for specific OS-es
/// </summary>
public static class Platform
{
public const string Windows = "Windows";
public const string Linux = "Linux";
public const string Darwin = "Darwin";
}
}
18 changes: 18 additions & 0 deletions src/NuGet.Core/NuGet.Test.Utility/Traits/PlatformAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Xunit.Sdk;

namespace NuGet.Test.Utility
{
/// <summary>
/// Test trait attribute applied to a test method to specify a platform filter.
/// </summary>
[TraitDiscoverer("NuGet.Test.Utility.PlatformDiscoverer", "NuGet.Test.Utility")]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class PlatformAttribute : Attribute, ITraitAttribute
{
public PlatformAttribute(string platform) { }
}
}
28 changes: 28 additions & 0 deletions src/NuGet.Core/NuGet.Test.Utility/Traits/PlatformDiscoverer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Linq;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace NuGet.Test.Utility
{
/// <summary>
/// This class discovers all of the tests and test classes that have
/// applied the Platform trait attribute
/// </summary>
public class PlatformDiscoverer : ITraitDiscoverer
{
/// <summary>
/// Gets the trait values from the Platform attribute.
/// </summary>
/// <param name="traitAttribute">The trait attribute containing the trait values.</param>
/// <returns>The trait values.</returns>
public IEnumerable<KeyValuePair<string, string>> GetTraits(IAttributeInfo traitAttribute)
{
var ctorArgs = traitAttribute.GetConstructorArguments().ToList();
yield return new KeyValuePair<string, string>("Platform", ctorArgs[0].ToString());
}
}
}
11 changes: 6 additions & 5 deletions src/NuGet.Core/NuGet.Test.Utility/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
}
},
"dependencies": {
"xunit": "2.1.0",
"NuGet.Packaging": {
"target": "project"
}
},
"xunit": "2.1.0"
},
"frameworks": {
"net45": {
"frameworkAssemblies": {
"System.IO.Compression": ""
"System.IO.Compression": "",
"System.Runtime": ""
},
"buildOptions": {
"define": [
Expand All @@ -41,8 +42,8 @@
],
"dependencies": {
"NETStandard.Library": "1.6.0",
"System.IO.Compression.ZipFile": "4.0.1",
"System.Diagnostics.Process": "4.1.0"
"System.Diagnostics.Process": "4.1.0",
"System.IO.Compression.ZipFile": "4.0.1"
},
"buildOptions": {
"define": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using NuGet.Test.Utility;
using Xunit;

namespace NuGet.Configuration
namespace NuGet.Configuration.Test
{
public class PackageSourceCredentialTests
{
Expand Down Expand Up @@ -34,30 +35,42 @@ public void FromUserInput_WithStorePasswordInClearText_DoesNotEncryptsPassword()
Assert.Equal("password", credentials.Password);
}

#if !IS_CORECLR
[Fact]
public void FromUserInput_WithStorePasswordEncrypted_EncryptsPassword()
[Fact, Platform(Platform.Windows)]
public void FromUserInput_WithStorePasswordEncrypted_OnWindows_EncryptsPassword()
{
var credentials = PackageSourceCredential.FromUserInput("source", "user", "password", storePasswordInClearText: false);

Assert.NotEqual("password", credentials.PasswordText);
Assert.Equal("password", credentials.Password);
}
#else
[Fact]
public void FromUserInput_WithStorePasswordEncrypted_Throws()

[Fact, Platform(Platform.Linux)]
public void FromUserInput_WithStorePasswordEncrypted_OnLinux_Throws()
{
Assert.Throws<NuGetConfigurationException>(() => PackageSourceCredential.FromUserInput("source", "user", "password", storePasswordInClearText: false));
}

[Fact]
public void Password_WithEncryptedPassword_Throws()
[Fact, Platform(Platform.Linux)]
public void Password_WithEncryptedPassword_OnLinux_Throws()
{
var credentials = new PackageSourceCredential("source", "user", "password", isPasswordClearText: false);

Assert.Throws<NuGetConfigurationException>(() => credentials.Password);
}

[Fact, Platform(Platform.Darwin)]
public void FromUserInput_WithStorePasswordEncrypted_OnMacOS_Throws()
{
Assert.Throws<NuGetConfigurationException>(() => PackageSourceCredential.FromUserInput("source", "user", "password", storePasswordInClearText: false));
}

[Fact, Platform(Platform.Darwin)]
public void Password_WithEncryptedPassword_OnMacOS_Throws()
{
var credentials = new PackageSourceCredential("source", "user", "password", isPasswordClearText: false);

Assert.Throws<NuGetConfigurationException>(() => credentials.Password);
}
#endif

[Fact]
public void IsValid_WithNonEmptyValues_ReturnsTrue()
Expand Down
Loading