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

Resolves Warning SYSLIB021 - Derived cryptographic types are obsolete #210

Merged
merged 11 commits into from
May 8, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using FakeItEasy;
using Microsoft.BridgeToKubernetes.Common.IO;
using Microsoft.BridgeToKubernetes.Common.Logging.MacAddressHash;
using Microsoft.BridgeToKubernetes.Common.PersistentProperyBag;
using Microsoft.BridgeToKubernetes.TestHelpers;
using System;
using Xunit;

namespace Microsoft.BridgeToKubernetes.Common.Tests.Logging.MacAddressHash
{
public class MacInformationProviderTests : TestsBase
{
[Fact]
public void GetMacAddressHashOnWindows()
hsubramanianaks marked this conversation as resolved.
Show resolved Hide resolved
{
const string output = "Physical Address Transport Name\r\n=================== ==========================================================\r\nDC-41-A9-AA-1A-14 Media disconnected\r\nDC-42-A9-AA-1E-18 Media disconnected\r\nCA-48-3A-C0-A6-63 \\Device\\Tcpip_{DCA2D11A-367A-4582-A3C5-077619A50152}";

var fakeClientConfig = A.Fake<IClientConfig>();
_autoFake.Provide(fakeClientConfig);

var fakePlatform = A.Fake<IPlatform>();
A.CallTo(() => fakePlatform.IsWindows).Returns(true);
A.CallTo(() => fakePlatform.ExecuteAndReturnOutput(A<string>.Ignored, A<string>.Ignored, A<TimeSpan>.Ignored, A<Action<string>>.Ignored, A<Action<string>>.Ignored, null, null)).Returns((0, output));
_autoFake.Provide(fakePlatform);

var macInformationProvider = _autoFake.Resolve<MacInformationProvider>();

const string expectedResult = "f52b35d47f8b2bf2eb37182c7dd6197d1879b90cb43f80f3eeda7a4b77eb1fd9";
string result = macInformationProvider.GetMacAddressHash();

Assert.Equal(expectedResult, result);

A.CallTo(() => fakeClientConfig.SetProperty("mac.address", expectedResult)).MustHaveHappenedOnceExactly();
A.CallTo(() => fakeClientConfig.Persist()).MustHaveHappenedOnceExactly();
}
}
}
2 changes: 1 addition & 1 deletion src/common/Logging/MacAddressHash/FipsCompliantSha.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ namespace Microsoft.BridgeToKubernetes.Common.Logging.MacAddressHash
internal class FipsCompliantSha
{
// FIPS compliant SHA256 hash algorithm.
public static readonly HashAlgorithm Sha256 = HashAlgorithm.Create(typeof(SHA256CryptoServiceProvider).AssemblyQualifiedName);
public static readonly SHA256 Sha256 = SHA256.Create();
}
}
38 changes: 11 additions & 27 deletions src/common/Logging/MacAddressHash/MACInformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// --------------------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -46,9 +45,9 @@ internal static class Mono

/// <summary></summary>
/// <param name="clientConfig">The client config, it contains a cached value for the hashed mac</param>
/// <param name="vsCodeStorageReader"/>
/// <param name="platform"/>
/// <param name="vsRegistryPropertyReader"/>
/// <param name="vsCodeStorageReader"></param>
/// <param name="platform"></param>
/// <param name="vsRegistryPropertyReader"></param>
public MacInformationProvider(
IClientConfig clientConfig,
VSCodeStorageReader vsCodeStorageReader,
Expand All @@ -69,7 +68,7 @@ public MacInformationProvider(
/// Check if there is a persisted value otherwise calculates and persist a new one
/// </summary>
/// <returns>The hash of the mac address</returns>
private string GetMacAddressHash()
public string GetMacAddressHash()
hsubramanianaks marked this conversation as resolved.
Show resolved Hide resolved
{
string persistedValue = null;
string result = null;
Expand Down Expand Up @@ -157,34 +156,19 @@ private void PersistMacAddressHash(string hashedMacAddress)
private static bool ValidateMacAddressHash(string macAddressHash)
=> !string.IsNullOrEmpty(macAddressHash) && Regex.IsMatch(macAddressHash, PersistRegex);

private static string RunCommandAndGetOutput(string commandName, string commandArgs = null)
private string RunCommandAndGetOutput(string commandName, string commandArgs = null)
{
var processOutput = new StringBuilder();
var process = new Process();
try
{
process.EnableRaisingEvents = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;

process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = commandName;
process.StartInfo.Arguments = commandArgs ?? string.Empty;
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
{
processOutput.AppendLine(e.Data);
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
process.Close();
return processOutput.ToString();
(var exitCode, var output) = this._platform.ExecuteAndReturnOutput(commandName,
commandArgs,
timeout: TimeSpan.FromSeconds(30),
hsubramanianaks marked this conversation as resolved.
Show resolved Hide resolved
stdOutCallback: null,
stdErrCallback: null);
return output;
}
catch (Exception)
{
process.Close();
return "";
}
}
Expand Down