Skip to content

Commit ef0229c

Browse files
exclude api keys from sample app logs (#7592)
## Summary of changes Exclude environment variables named `*API_KEY*` in `SampleHelpers.GetDatadogEnvironmentVariables()`. ## Reason for change Prevent logging API keys in test outputs. ## Implementation details - In `GetDatadogEnvironmentVariables()`, filter out any environment variable whose key contains "API_KEY" (case-insensitive) Bonus changes: - Changed the return type from `IEnumerable<T>` to `List<T>` since we already materialize the list (potential tiny perf improvement when iterating) - Removed unnecessary `ToUpperInvariant()` call on the key and replaced `StartsWith()` with a case-insensitive comparison ## Test coverage None, it's a helper method used in tests. ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: Where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. MergeQueue is NOT enabled in this repository. If you have write access to the repo, the PR has 1-2 approvals (see above), and all of the required checks have passed, you can use the Squash and Merge button to merge the PR. If you don't have write access, or you need help, reach out in the #apm-dotnet channel in Slack. -->
1 parent c2c3b52 commit ef0229c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

tracer/test/test-applications/Samples.Shared/SampleHelpers.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,15 +368,15 @@ public static void TrySetExceptionOnActiveScope(Exception exception)
368368
SetExceptionMethod.Invoke(span, new object[] { new Exception() });
369369
}
370370

371-
public static IEnumerable<KeyValuePair<string, string>> GetDatadogEnvironmentVariables()
371+
public static List<KeyValuePair<string, string>> GetDatadogEnvironmentVariables()
372372
{
373373
var prefixes = new[] { "COR_", "CORECLR_", "DD_", "DATADOG_" };
374374

375375
var envVars = from envVar in Environment.GetEnvironmentVariables().Cast<DictionaryEntry>()
376376
from prefix in prefixes
377-
let key = (envVar.Key as string)?.ToUpperInvariant()
377+
let key = (envVar.Key as string)?.ToUpperInvariant() // use ToUpperInvariant() because in .NET Framework string.Contains() doesn't have StringComparison overload
378378
let value = envVar.Value as string
379-
where key.StartsWith(prefix)
379+
where key.StartsWith(prefix, StringComparison.Ordinal) && !key.Contains("API_KEY")
380380
orderby key
381381
select new KeyValuePair<string, string>(key, value);
382382

0 commit comments

Comments
 (0)