Skip to content

Commit b91b417

Browse files
authored
Update SensitiveDataLogger for dual environment support (#865)
Modified `EnsureDevelopmentEnvironment `to check `DOTNET_ENVIRONMENT` if `ASPNETCORE_ENVIRONMENT` is not set, and updated exception message. Updated **SensitiveDataLoggerTests.cs** to distinguish between environment variables. Added new test methods for different environments. Renamed existing test methods for clarity. Updated `Dispose` method to clear both environment variables. ## Motivation and Context (Why the change? What's the scenario?) See https://github.com/microsoft/kernel-memory/issues/851
1 parent b3e9397 commit b91b417

File tree

2 files changed

+76
-25
lines changed

2 files changed

+76
-25
lines changed

service/Abstractions/Diagnostics/SensitiveDataLogger.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All rights reserved.
1+
// Copyright (c) Microsoft. All rights reserved.
22

33
using System;
44
using Microsoft.Extensions.Logging;
@@ -68,10 +68,14 @@ public static void LogSensitive(
6868

6969
private static void EnsureDevelopmentEnvironment()
7070
{
71-
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
71+
const string AspNetCoreEnvVar = "ASPNETCORE_ENVIRONMENT";
72+
const string DotNetEnvVar = "DOTNET_ENVIRONMENT";
73+
74+
// The ASPNETCORE_ENVIRONMENT environment variable has the precedence. If it does not exist, checks for DOTNET_ENVIRONMENT.
75+
var env = Environment.GetEnvironmentVariable(AspNetCoreEnvVar) ?? Environment.GetEnvironmentVariable(DotNetEnvVar);
7276
if (!string.Equals(env, "Development", StringComparison.OrdinalIgnoreCase))
7377
{
74-
throw new InvalidOperationException("Sensitive data logging can be enabled only in a development environment. Check ASPNETCORE_ENVIRONMENT env var.");
78+
throw new InvalidOperationException($"Sensitive data logging can be enabled only in a development environment. Check {AspNetCoreEnvVar} or {DotNetEnvVar} env vars.");
7579
}
7680
}
7781
}

service/tests/Abstractions.UnitTests/Diagnostics/SensitiveDataLoggerTests.cs

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ namespace Microsoft.KM.Abstractions.UnitTests.Diagnostics;
66

77
public sealed class SensitiveDataLoggerTests : IDisposable
88
{
9-
private const string EnvironmentVariableName = "ASPNETCORE_ENVIRONMENT";
9+
private const string AspNetCoreEnvVar = "ASPNETCORE_ENVIRONMENT";
10+
private const string DotNetEnvVar = "DOTNET_ENVIRONMENT";
1011

1112
[Fact]
1213
public void ItIsDisabledByDefault()
@@ -17,13 +18,15 @@ public void ItIsDisabledByDefault()
1718

1819
[Theory]
1920
[Trait("Category", "UnitTest")]
20-
[InlineData("development")]
21-
[InlineData("Development")]
22-
public void ItCanBeEnabledInDevelopmentEnvironment(string environment)
21+
[InlineData(AspNetCoreEnvVar, "development")]
22+
[InlineData(AspNetCoreEnvVar, "Development")]
23+
[InlineData(DotNetEnvVar, "development")]
24+
[InlineData(DotNetEnvVar, "Development")]
25+
public void ItCanBeEnabledInDevEnvironments(string envVar, string envType)
2326
{
2427
// Arrange
2528
Assert.False(SensitiveDataLogger.Enabled);
26-
Environment.SetEnvironmentVariable(EnvironmentVariableName, environment);
29+
Environment.SetEnvironmentVariable(envVar, envType);
2730

2831
// Act
2932
SensitiveDataLogger.Enabled = true;
@@ -34,35 +37,47 @@ public void ItCanBeEnabledInDevelopmentEnvironment(string environment)
3437

3538
[Theory]
3639
[Trait("Category", "UnitTest")]
37-
[InlineData("staging")]
38-
[InlineData("Staging")]
39-
[InlineData("production")]
40-
[InlineData("Production")]
41-
[InlineData("any")]
42-
public void ItCannotBeEnabledInNonDevelopmentEnvironments(string environment)
40+
[InlineData(AspNetCoreEnvVar, "staging")]
41+
[InlineData(AspNetCoreEnvVar, "Staging")]
42+
[InlineData(AspNetCoreEnvVar, "production")]
43+
[InlineData(AspNetCoreEnvVar, "Production")]
44+
[InlineData(AspNetCoreEnvVar, "any")]
45+
[InlineData(DotNetEnvVar, "staging")]
46+
[InlineData(DotNetEnvVar, "Staging")]
47+
[InlineData(DotNetEnvVar, "production")]
48+
[InlineData(DotNetEnvVar, "Production")]
49+
[InlineData(DotNetEnvVar, "any")]
50+
public void ItCannotBeEnabledInNonDevEnvironments(string envVar, string envType)
4351
{
4452
// Arrange
4553
Assert.False(SensitiveDataLogger.Enabled);
46-
Environment.SetEnvironmentVariable(EnvironmentVariableName, environment);
54+
Environment.SetEnvironmentVariable(envVar, envType);
4755

4856
// Act/Assert
4957
Assert.Throws<InvalidOperationException>(() => SensitiveDataLogger.Enabled = true);
5058
}
5159

5260
[Theory]
5361
[Trait("Category", "UnitTest")]
54-
[InlineData("development")]
55-
[InlineData("Development")]
56-
[InlineData("staging")]
57-
[InlineData("Staging")]
58-
[InlineData("production")]
59-
[InlineData("Production")]
60-
[InlineData("any")]
61-
public void ItCanBeDisabledInAnyEnvironment(string environment)
62+
[InlineData(AspNetCoreEnvVar, "development")]
63+
[InlineData(AspNetCoreEnvVar, "Development")]
64+
[InlineData(AspNetCoreEnvVar, "staging")]
65+
[InlineData(AspNetCoreEnvVar, "Staging")]
66+
[InlineData(AspNetCoreEnvVar, "production")]
67+
[InlineData(AspNetCoreEnvVar, "Production")]
68+
[InlineData(AspNetCoreEnvVar, "any")]
69+
[InlineData(DotNetEnvVar, "development")]
70+
[InlineData(DotNetEnvVar, "Development")]
71+
[InlineData(DotNetEnvVar, "staging")]
72+
[InlineData(DotNetEnvVar, "Staging")]
73+
[InlineData(DotNetEnvVar, "production")]
74+
[InlineData(DotNetEnvVar, "Production")]
75+
[InlineData(DotNetEnvVar, "any")]
76+
public void ItCanBeDisabledInAnyEnvironment(string envVar, string envType)
6277
{
6378
// Arrange
6479
Assert.False(SensitiveDataLogger.Enabled);
65-
Environment.SetEnvironmentVariable(EnvironmentVariableName, environment);
80+
Environment.SetEnvironmentVariable(envVar, envType);
6681

6782
// Act
6883
SensitiveDataLogger.Enabled = false;
@@ -71,9 +86,41 @@ public void ItCanBeDisabledInAnyEnvironment(string environment)
7186
Assert.False(SensitiveDataLogger.Enabled);
7287
}
7388

89+
[Theory]
90+
[Trait("Category", "UnitTest")]
91+
[InlineData("development", "staging", false)]
92+
[InlineData("Development", "Staging", false)]
93+
[InlineData("development", "production", false)]
94+
[InlineData("Development", "Production", false)]
95+
[InlineData("staging", "development", true)]
96+
[InlineData("Staging", "Development", true)]
97+
[InlineData("production", "development", true)]
98+
[InlineData("Production", "Development", true)]
99+
public void AspNetCoreEnvTypeHasThePrecedence(string aspNetCoreEnvType, string dotNetEnvType, bool fail)
100+
{
101+
// Arrange
102+
Assert.False(SensitiveDataLogger.Enabled);
103+
Environment.SetEnvironmentVariable(AspNetCoreEnvVar, aspNetCoreEnvType);
104+
Environment.SetEnvironmentVariable(DotNetEnvVar, dotNetEnvType);
105+
106+
if (fail)
107+
{
108+
Assert.Throws<InvalidOperationException>(() => SensitiveDataLogger.Enabled = true);
109+
}
110+
else
111+
{
112+
// Act
113+
SensitiveDataLogger.Enabled = true;
114+
115+
// Assert
116+
Assert.True(SensitiveDataLogger.Enabled);
117+
}
118+
}
119+
74120
public void Dispose()
75121
{
76-
Environment.SetEnvironmentVariable(EnvironmentVariableName, null);
122+
Environment.SetEnvironmentVariable(AspNetCoreEnvVar, null);
123+
Environment.SetEnvironmentVariable(DotNetEnvVar, null);
77124
SensitiveDataLogger.Enabled = false;
78125
}
79126
}

0 commit comments

Comments
 (0)