Skip to content

Commit

Permalink
Don't fail silently when windows credentials go wrong. (#958)
Browse files Browse the repository at this point in the history
* Change error handling from hidden debug output to user prompts.
* Only delete corrupted file when approved by the user.
  • Loading branch information
Jim Przybylinski authored May 17, 2018
1 parent bdb6b47 commit 67cec9c
Show file tree
Hide file tree
Showing 24 changed files with 1,142 additions and 508 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{608977EE-0606-45F6-B68C-4861842B6FE3}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GoogleCloudExtension.GCloud.UnitTests</RootNamespace>
<AssemblyName>GoogleCloudExtension.GCloud.UnitTests</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="WindowsInstanceCredentialsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GoogleCloudExtension.GCloud\GoogleCloudExtension.GCloud.csproj">
<Project>{95EFAC7E-4F6E-46F0-BCC8-90EE1487E1E1}</Project>
<Name>GoogleCloudExtension.GCloud</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets')" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("GoogleCloudExtension.GCloud.UnitTests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Google Inc.")]
[assembly: AssemblyProduct("Cloud Tools for Visual Studio")]
[assembly: AssemblyCopyright("Copyright © Google Inc. 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: ComVisible(false)]

[assembly: Guid("608977ee-0606-45f6-b68c-4861842b6fe3")]

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System.Diagnostics.CodeAnalysis;

namespace GoogleCloudExtension.GCloud.UnitTests
{
[TestClass]
public class WindowsInstanceCredentialsTests
{
// ReSharper disable once UnusedAutoPropertyAccessor.Local
private static object[][] NotEqualObjects { get; } =
{
new object[] {null},
new object[] {"UserNameAndPassword"},
new object[] {new WindowsInstanceCredentials("DifferentUser", "Password")},
new object[] {new WindowsInstanceCredentials("User", "DifferentPassword")}
};

[TestMethod]
public void TestJsonSerialization()
{
var objectUnderTest = new WindowsInstanceCredentials("UserString", "PasswordString");

string json = JsonConvert.SerializeObject(objectUnderTest);

Assert.AreEqual(@"{""username"":""UserString"",""password"":""PasswordString""}", json);
}

[TestMethod]
public void TestJsonDeserialization()
{
var json = @"{""username"":""UserString"",""password"":""PasswordString""}";

var result = JsonConvert.DeserializeObject<WindowsInstanceCredentials>(json);

Assert.AreEqual("UserString", result.User);
Assert.AreEqual("PasswordString", result.Password);
}

[TestMethod]
[DynamicData(nameof(NotEqualObjects))]
public void TestEqualsFalse(object notEqualObject)
{
var sourceObject = new WindowsInstanceCredentials("User", "Password");
Assert.IsFalse(sourceObject.Equals(notEqualObject));
}

[TestMethod]
[DynamicData(nameof(NotEqualObjects))]
public void TestHashCodeNotEqual(object notEqualObject)
{
var sourceObject = new WindowsInstanceCredentials("User", "Password");
Assert.AreNotEqual(sourceObject.GetHashCode(), notEqualObject?.GetHashCode());
}

[TestMethod]
[SuppressMessage("ReSharper", "EqualExpressionComparison")]
public void TestEqualsTrue()
{
const string user = "User";
const string password = "Password";
var source = new WindowsInstanceCredentials(user, password);
var target = new WindowsInstanceCredentials(user, password);
Assert.IsTrue(source.Equals(source));
Assert.IsTrue(source.Equals(target));
}

[TestMethod]
public void TestGetHashCodeEqual()
{
const string user = "User";
const string password = "Password";
Assert.AreEqual(
new WindowsInstanceCredentials(user, password).GetHashCode(),
new WindowsInstanceCredentials(user, password).GetHashCode());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSTest.TestAdapter" version="1.2.1" targetFramework="net461" />
<package id="MSTest.TestFramework" version="1.2.1" targetFramework="net461" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using GoogleCloudExtension.Utils;
using Newtonsoft.Json;

namespace GoogleCloudExtension.GCloud
Expand All @@ -22,9 +23,47 @@ namespace GoogleCloudExtension.GCloud
public sealed class WindowsInstanceCredentials
{
[JsonProperty("username")]
public string User { get; set; }
public string User { get; }

[JsonProperty("password")]
public string Password { get; set; }
public string Password { get; }

[JsonConstructor]
public WindowsInstanceCredentials(string user, string password)
{
user.ThrowIfNull(nameof(user));
User = user;
Password = password;
}

/// <summary>Returns a string that represents the current object.</summary>
/// <returns>A string that represents the current object.</returns>
public override string ToString() => JsonConvert.SerializeObject(this);

public override bool Equals(object obj)
{
if (obj == this)
{
return true;
}
else if (!(obj is WindowsInstanceCredentials))
{
return false;
}
else
{
var credentials = (WindowsInstanceCredentials)obj;
return User == credentials.User &&
Password == credentials.Password;
}
}

public override int GetHashCode()
{
int hashCode = -1879510246;
hashCode = hashCode * -1521134295 + User.GetHashCode();
hashCode = hashCode * -1521134295 + Password.GetHashCode();
return hashCode;
}
}
}
6 changes: 6 additions & 0 deletions GoogleCloudExtension/GoogleCloudExtension.sln
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GoogleCloudExtension.Deploy
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestingHelpers", "TestingHelpers\TestingHelpers.csproj", "{4EB5A3A0-3D68-4880-8855-405A46CDCE0B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GoogleCloudExtension.GCloud.UnitTests", "GoogleCloudExtension.GCloud.UnitTests\GoogleCloudExtension.GCloud.UnitTests.csproj", "{608977EE-0606-45F6-B68C-4861842B6FE3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -171,6 +173,10 @@ Global
{4EB5A3A0-3D68-4880-8855-405A46CDCE0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4EB5A3A0-3D68-4880-8855-405A46CDCE0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4EB5A3A0-3D68-4880-8855-405A46CDCE0B}.Release|Any CPU.Build.0 = Release|Any CPU
{608977EE-0606-45F6-B68C-4861842B6FE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{608977EE-0606-45F6-B68C-4861842B6FE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{608977EE-0606-45F6-B68C-4861842B6FE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{608977EE-0606-45F6-B68C-4861842B6FE3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal interface IWindowsCredentialsStore
/// <param name="instance">The GCE VM</param>
/// <returns>The list of <seealso cref="WindowsInstanceCredentials"/> associated with The GCE VM. It might be
/// empty if no credentials are found.</returns>
IEnumerable<WindowsInstanceCredentials> GetCredentialsForInstance(Instance instance);
IList<WindowsInstanceCredentials> GetCredentialsForInstance(Instance instance);

/// <summary>
/// Returns the path where to store credential related information for a GCE VM.
Expand Down
Loading

0 comments on commit 67cec9c

Please sign in to comment.