Skip to content

Commit

Permalink
Updating logging property and update string manipulation (#208)
Browse files Browse the repository at this point in the history
* Updating logging property and update string manipulation

* Address PR comment

---------

Co-authored-by: elvilla <elvilla@microsoft.com>
  • Loading branch information
elenavillamil and elvilla authored Mar 21, 2023
1 parent 48f75ad commit da3ea55
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/common.tests/Utilities/StringManipulationTests.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
2 changes: 1 addition & 1 deletion src/common/Utilities/StringManipulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/dsc/Telemetry/CommandLogging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit da3ea55

Please sign in to comment.