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

Updating logging property and update string manipulation #208

Merged
merged 3 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/common.tests/Utilities/StringManipulationTests.cs
Original file line number Diff line number Diff line change
@@ -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")]
elenavillamil marked this conversation as resolved.
Show resolved Hide resolved
[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));
}
}
}
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