add support for both env vars in #7916 fix (#10009)

This commit is contained in:
Andrew Stanton-Nurse 2019-05-07 22:10:31 -07:00 committed by GitHub
parent 58c67727df
commit 8759518a97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 1 deletions

View File

@ -12,6 +12,8 @@ namespace Microsoft.AspNetCore.DataProtection.Internal
internal static class ContainerUtils
{
private static Lazy<bool> _isContainer = new Lazy<bool>(IsProcessRunningInContainer);
private const string RunningInContainerVariableName = "DOTNET_RUNNING_IN_CONTAINER";
private const string DeprecatedRunningInContainerVariableName = "DOTNET_RUNNING_IN_CONTAINERS";
public static bool IsContainer => _isContainer.Value;
@ -80,7 +82,8 @@ namespace Microsoft.AspNetCore.DataProtection.Internal
private static bool IsProcessRunningInContainer()
{
// Official .NET Core images (Windows and Linux) set this. So trust it if it's there.
if(string.Equals(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINERS"), "true", StringComparison.OrdinalIgnoreCase))
// We check both DOTNET_RUNNING_IN_CONTAINER (the current name) and DOTNET_RUNNING_IN_CONTAINERS (a deprecated name used in some images).
if (GetBooleanEnvVar(RunningInContainerVariableName) || GetBooleanEnvVar(DeprecatedRunningInContainerVariableName))
{
return true;
}
@ -102,5 +105,12 @@ namespace Microsoft.AspNetCore.DataProtection.Internal
// typically the last line in the file is "1:name=openrc:/docker"
return lines.Reverse().Any(l => l.EndsWith("name=openrc:/docker", StringComparison.Ordinal));
}
private static bool GetBooleanEnvVar(string envVarName)
{
var value = Environment.GetEnvironmentVariable(envVarName);
return string.Equals(value, "1", StringComparison.Ordinal) ||
string.Equals(value, "true", StringComparison.OrdinalIgnoreCase);
}
}
}