fix #7916 by using DOTNET_RUNNING_IN_CONTAINERS env var to detect container (#9903)

This commit is contained in:
Andrew Stanton-Nurse 2019-05-06 11:55:45 -07:00 committed by GitHub
parent e6db096af5
commit 557bbf7870
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 10 deletions

View File

@ -9,15 +9,15 @@ using System.Runtime.InteropServices;
namespace Microsoft.AspNetCore.DataProtection.Internal namespace Microsoft.AspNetCore.DataProtection.Internal
{ {
internal static class DockerUtils internal static class ContainerUtils
{ {
private static Lazy<bool> _isDocker = new Lazy<bool>(IsProcessRunningInDocker); private static Lazy<bool> _isContainer = new Lazy<bool>(IsProcessRunningInContainer);
public static bool IsDocker => _isDocker.Value; public static bool IsContainer => _isContainer.Value;
public static bool IsVolumeMountedFolder(DirectoryInfo directory) public static bool IsVolumeMountedFolder(DirectoryInfo directory)
{ {
if (!IsDocker) if (!IsContainer)
{ {
return false; return false;
} }
@ -77,14 +77,21 @@ namespace Microsoft.AspNetCore.DataProtection.Internal
return false; return false;
} }
private static bool IsProcessRunningInDocker() 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))
{
return true;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{ {
// we currently don't have a good way to detect if running in a Windows container // we currently don't have a good way to detect if running in a Windows container
return false; return false;
} }
// Try to detect docker using the cgroups process 1 is in.
const string procFile = "/proc/1/cgroup"; const string procFile = "/proc/1/cgroup";
if (!File.Exists(procFile)) if (!File.Exists(procFile))
{ {

View File

@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.DataProtection.Repositories
try try
{ {
if (DockerUtils.IsDocker && !DockerUtils.IsVolumeMountedFolder(Directory)) if (ContainerUtils.IsContainer && !ContainerUtils.IsVolumeMountedFolder(Directory))
{ {
// warn users that keys may be lost when running in docker without a volume mounted folder // warn users that keys may be lost when running in docker without a volume mounted folder
_logger.UsingEphemeralFileSystemLocationInContainer(Directory.FullName); _logger.UsingEphemeralFileSystemLocationInContainer(Directory.FullName);

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO; using System.IO;
@ -8,7 +8,7 @@ using Xunit;
namespace Microsoft.AspNetCore.DataProtection.Test namespace Microsoft.AspNetCore.DataProtection.Test
{ {
public class DockerUtilsTests public class ContainerUtilsTests
{ {
// example of content from /proc/self/mounts // example of content from /proc/self/mounts
private static readonly string[] fstab = new [] private static readonly string[] fstab = new []
@ -37,7 +37,7 @@ namespace Microsoft.AspNetCore.DataProtection.Test
[InlineData("../dir")] [InlineData("../dir")]
public void DeterminesFolderIsNotMounted(string directory) public void DeterminesFolderIsNotMounted(string directory)
{ {
Assert.False(DockerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab)); Assert.False(ContainerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab));
} }
[ConditionalTheory] [ConditionalTheory]
@ -50,7 +50,7 @@ namespace Microsoft.AspNetCore.DataProtection.Test
[InlineData("/app/subdir/two/")] [InlineData("/app/subdir/two/")]
public void DeterminesFolderIsMounted(string directory) public void DeterminesFolderIsMounted(string directory)
{ {
Assert.True(DockerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab)); Assert.True(ContainerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab));
} }
} }
} }