From da3ea55121dec68ebdd231c6d2441357da487b0b Mon Sep 17 00:00:00 2001 From: elenavillamil Date: Tue, 21 Mar 2023 09:16:18 -0700 Subject: [PATCH] Updating logging property and update string manipulation (#208) * Updating logging property and update string manipulation * Address PR comment --------- Co-authored-by: elvilla --- .../Utilities/StringManipulationTests.cs | 24 +++++++++++++++++++ src/common/Utilities/StringManipulation.cs | 2 +- src/dsc/Telemetry/CommandLogging.cs | 3 +-- 3 files changed, 26 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..9c05962c --- /dev/null +++ b/src/common.tests/Utilities/StringManipulationTests.cs @@ -0,0 +1,24 @@ +// -------------------------------------------------------------------------------------------- +// 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("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) + { + 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(