Refactor logging tests related to sensitive env. vars. (#2892)

refactor based on feedback
This commit is contained in:
Rasmus Kuusmann 2023-08-30 21:17:37 +03:00 committed by GitHub
parent 08cbbc24c4
commit 8ac6bd0706
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 28 deletions

View File

@ -18,15 +18,13 @@ namespace IntegrationTests.Helpers;
internal static class DirectoryHelpers
{
public static string GetTempDirectory()
public static DirectoryInfo CreateTempDirectory()
{
#if NET7_0_OR_GREATER
return Directory.CreateTempSubdirectory("native_logs").FullName;
return Directory.CreateTempSubdirectory("native_logs");
#else
var tempDir = Path.Combine(Path.GetTempPath(), "native_logs_" + Path.GetRandomFileName());
Directory.CreateDirectory(tempDir);
return tempDir;
return Directory.CreateDirectory(tempDir);
#endif
}
}

View File

@ -1,4 +1,4 @@
// <copyright file="DictionaryHelpers.cs" company="OpenTelemetry Authors">
// <copyright file="ListHelpers.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
@ -16,15 +16,15 @@
namespace IntegrationTests.Helpers;
internal static class DictionaryHelpers
internal static class ListHelpers
{
public static ICollection<(string Key, string Value)> ToEnvironmentVariablesList(this IEnumerable<string> list)
public static ICollection<KeyValuePair<string, string>> ToEnvironmentVariablesList(this IEnumerable<string> list)
{
return list.Select(x =>
{
var keyValuePair = x.Split(new[] { '=' }, 2);
return (keyValuePair[0], keyValuePair[1]);
return new KeyValuePair<string, string>(keyValuePair[0], keyValuePair[1]);
})
.ToList();
}

View File

@ -395,27 +395,29 @@ public class SmokeTests : TestHelper
[Fact]
public void NativeLogsHaveNoSensitiveData()
{
var tempLogsDirectory = DirectoryHelpers.GetTempDirectory();
var tempLogsDirectory = DirectoryHelpers.CreateTempDirectory();
var secretIdentificators = new[] { "API", "TOKEN", "SECRET", "KEY", "PASSWORD", "PASS", "PWD", "HEADER", "CREDENTIALS" };
EnableBytecodeInstrumentation();
SetEnvironmentVariable("OTEL_DOTNET_AUTO_LOG_DIRECTORY", tempLogsDirectory);
SetEnvironmentVariable("OTEL_DOTNET_AUTO_LOG_DIRECTORY", tempLogsDirectory.FullName);
SetEnvironmentVariable("OTEL_LOG_LEVEL", "debug");
foreach (var item in secretIdentificators)
{
SetEnvironmentVariable($"OTEL_{item}_VALUE", "this is secret!");
if (!EnvironmentTools.IsWindows())
{
SetEnvironmentVariable($"otel_{item.ToLowerInvariant()}_value2", "this is secret!");
}
}
try
{
RunTestApplication();
var logs = Directory.GetFiles(tempLogsDirectory);
logs.Should().NotBeNull();
var nativeLog = logs.FirstOrDefault(x => x.Contains("otel-dotnet-auto-native-"));
nativeLog.Should().NotBeNull();
var nativeLogContent = File.ReadAllText(nativeLog!);
var nativeLog = tempLogsDirectory.GetFiles("otel-dotnet-auto-native-*").Single();
var nativeLogContent = File.ReadAllText(nativeLog.FullName);
nativeLogContent.Should().NotBeNullOrWhiteSpace();
var environmentVariables = ParseEnvironmentVariablesLog(nativeLogContent);
@ -428,8 +430,13 @@ public class SmokeTests : TestHelper
secretVariables.Should().NotBeEmpty();
secretVariables.Should().AllSatisfy(secret => secret.Value.Should().Be("<hidden>"));
}
finally
{
tempLogsDirectory.Delete(true);
}
}
private static ICollection<(string Key, string Value)> ParseEnvironmentVariablesLog(string log)
private static ICollection<KeyValuePair<string, string>> ParseEnvironmentVariablesLog(string log)
{
var lines = log.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var variables = lines