Skip to content

Commit

Permalink
Resolves Warning SYSLIB021 - Derived cryptographic types are obsolete (
Browse files Browse the repository at this point in the history
  • Loading branch information
Eneuman authored May 8, 2023
1 parent 9845a41 commit e102da8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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()
{
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();
}

[Fact]
public void GetMacAddressHashOnLinux()
{
const string output = " ether dc:41:a9:aa:1a:14 txqueuelen 1000 (Ethernet)\r\n RX packets 0 bytes 0 (0.0 B)\r\n RX errors 0 dropped 0 overruns 0 frame 0\r\n TX packets 0 bytes 0 (0.0 B)\r\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\r\n\r\ndummy0: flags=130<BROADCAST,NOARP> mtu 1500\r\n ether 6a:c7:29:b2:dc:9e txqueuelen 1000 (Ethernet)\r\n RX packets 0 bytes 0 (0.0 B)\r\n RX errors 0 dropped 0 overruns 0 frame 0\r\n TX packets 0 bytes 0 (0.0 B)\r\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\r\n\r\neth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1280\r\n inet 0.0.0.0 netmask 0.0.0.0 broadcast 0.0.0.0\r\n inet6 fe80::215:5dff:feb6:b81 prefixlen 64 scopeid 0x20<link>\r\n ether 00:15:5d:b6:0b:81 txqueuelen 1000 (Ethernet)\r\n RX packets 640092 bytes 605988996 (605.9 MB)\r\n RX errors 0 dropped 0 overruns 0 frame 0\r\n TX packets 77211 bytes 5949128 (5.9 MB)\r\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\r\n\r\nlo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536\r\n inet 127.0.0.1 netmask 255.0.0.0\r\n inet6 ::1 prefixlen 128 scopeid 0x10<host>\r\n loop txqueuelen 1000 (Local Loopback)\r\n RX packets 58608 bytes 361924804 (361.9 MB)\r\n RX errors 0 dropped 0 overruns 0 frame 0\r\n TX packets 58608 bytes 361924804 (361.9 MB)\r\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\r\n\r\nsit0: flags=128<NOARP> mtu 1480\r\n sit txqueuelen 1000 (IPv6-in-IPv4)\r\n RX packets 0 bytes 0 (0.0 B)\r\n RX errors 0 dropped 0 overruns 0 frame 0\r\n TX packets 0 bytes 0 (0.0 B)\r\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\r\n\r\ntunl0: flags=128<NOARP> mtu 1480\r\n tunnel txqueuelen 1000 (IPIP Tunnel)\r\n RX packets 0 bytes 0 (0.0 B)\r\n RX errors 0 dropped 0 overruns 0 frame 0\r\n TX packets 0 bytes 0 (0.0 B)\r\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0";

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

var fakePlatform = A.Fake<IPlatform>();
A.CallTo(() => fakePlatform.IsLinux).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 = "e6e736e74149404e33e7f35171e3b178798c389a195b2fd81be1e9c0e6e13409";
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()
{
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),
stdOutCallback: null,
stdErrCallback: null);
return output;
}
catch (Exception)
{
process.Close();
return "";
}
}
Expand Down

0 comments on commit e102da8

Please sign in to comment.