From a146b578819e85a6638f8d4c14ead3fbf6a1de08 Mon Sep 17 00:00:00 2001 From: elvilla Date: Fri, 17 Mar 2023 13:42:23 -0700 Subject: [PATCH 1/2] Updating logging property and update string manipulation --- .../Utilities/StringManipulationTests.cs | 25 +++++++++++++++++++ src/common/Utilities/StringManipulation.cs | 2 +- src/dsc/Telemetry/CommandLogging.cs | 3 +-- 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 src/common.tests/Utilities/StringManipulationTests.cs diff --git a/src/common.tests/Utilities/StringManipulationTests.cs b/src/common.tests/Utilities/StringManipulationTests.cs new file mode 100644 index 00000000..97eed38f --- /dev/null +++ b/src/common.tests/Utilities/StringManipulationTests.cs @@ -0,0 +1,25 @@ +// -------------------------------------------------------------------------------------------- +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. +// -------------------------------------------------------------------------------------------- + +using Microsoft.BridgeToKubernetes.Common.Utilities; +using Xunit; + +namespace Microsoft.BridgeToKubernetes.Common.Tests.Utilities +{ + public class StringManipulationTests + { + [Theory] + [InlineData("BEGIN PRIVATE KEY\nuirethweuifhewiufhweiuofhweo\njweiorjweoifnewouhfousdgherufhsdiuhEND PRIVATE KEY\n", "KEY_WAS_REMOVED\n")] + [InlineData("iurhuweifhoBEGIN PRIVATE KEY\nuirethweuifhewiufhweiuofhweo\njweiorjweoifnewouhfousdgherufhsdiuhEND PRIVATE KEY\n", "iurhuweifhoKEY_WAS_REMOVED\n")] + + [InlineData("hello world", "hello world")] + + [InlineData("BEGIN PRIVATE KEY\nuirethweuifhewiufhweiuofhweo\njweiorjweoifnewouhfousdgherufhsdiuhEND PRIVATE KEY", "KEY_WAS_REMOVED")] + public void RemovePrivateKeyIfNeededTest(string input, string expected) + { + Assert.True(string.Equals(StringManipulation.RemovePrivateKeyIfNeeded(input), expected)); + } + } +} \ No newline at end of file diff --git a/src/common/Utilities/StringManipulation.cs b/src/common/Utilities/StringManipulation.cs index 8f635c87..818bb2ff 100644 --- a/src/common/Utilities/StringManipulation.cs +++ b/src/common/Utilities/StringManipulation.cs @@ -15,7 +15,7 @@ public static string RemovePrivateKeyIfNeeded(string inputString) { if (inputString.ContainsIgnoreCase("BEGIN PRIVATE KEY")) { - return Regex.Replace(inputString, "BEGIN PRIVATE KEY.*END PRIVATE KEY", "KEY_WAS_REMOVED"); + return Regex.Replace(inputString, @"(BEGIN PRIVATE KEY(\s|.)*END PRIVATE KEY)", "KEY_WAS_REMOVED"); } return inputString; } diff --git a/src/dsc/Telemetry/CommandLogging.cs b/src/dsc/Telemetry/CommandLogging.cs index 8a0e2fc3..c53730c9 100644 --- a/src/dsc/Telemetry/CommandLogging.cs +++ b/src/dsc/Telemetry/CommandLogging.cs @@ -54,8 +54,7 @@ public void Finished(bool success, string failureReason) if (!string.IsNullOrWhiteSpace(failureReason)) { // Ensure no secrets are logged - - properties.Add("FailureReason", StringManipulation.RemovePrivateKeyIfNeeded(failureReason)); + properties.Add("FailureReason2", StringManipulation.RemovePrivateKeyIfNeeded(failureReason)); } this._log?.Event( From 4333ac8ce37b4df16d7fc7da077194e565bea80f Mon Sep 17 00:00:00 2001 From: elvilla Date: Mon, 20 Mar 2023 10:34:43 -0700 Subject: [PATCH 2/2] Address PR comment --- src/common.tests/Utilities/StringManipulationTests.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/common.tests/Utilities/StringManipulationTests.cs b/src/common.tests/Utilities/StringManipulationTests.cs index 97eed38f..9c05962c 100644 --- a/src/common.tests/Utilities/StringManipulationTests.cs +++ b/src/common.tests/Utilities/StringManipulationTests.cs @@ -13,9 +13,8 @@ public class StringManipulationTests [Theory] [InlineData("BEGIN PRIVATE KEY\nuirethweuifhewiufhweiuofhweo\njweiorjweoifnewouhfousdgherufhsdiuhEND PRIVATE KEY\n", "KEY_WAS_REMOVED\n")] [InlineData("iurhuweifhoBEGIN PRIVATE KEY\nuirethweuifhewiufhweiuofhweo\njweiorjweoifnewouhfousdgherufhsdiuhEND PRIVATE KEY\n", "iurhuweifhoKEY_WAS_REMOVED\n")] - + [InlineData("BEGIN PRIVATE KEYb2kkkkkkkkkkkkkkEND PRIVATE KEY", "KEY_WAS_REMOVED")] [InlineData("hello world", "hello world")] - [InlineData("BEGIN PRIVATE KEY\nuirethweuifhewiufhweiuofhweo\njweiorjweoifnewouhfousdgherufhsdiuhEND PRIVATE KEY", "KEY_WAS_REMOVED")] public void RemovePrivateKeyIfNeededTest(string input, string expected) {