diff --git a/src/DataProtection/DataProtection/src/Internal/DockerUtils.cs b/src/DataProtection/DataProtection/src/Internal/ContainerUtils.cs similarity index 82% rename from src/DataProtection/DataProtection/src/Internal/DockerUtils.cs rename to src/DataProtection/DataProtection/src/Internal/ContainerUtils.cs index 7a1ede17e0..01547be110 100644 --- a/src/DataProtection/DataProtection/src/Internal/DockerUtils.cs +++ b/src/DataProtection/DataProtection/src/Internal/ContainerUtils.cs @@ -9,15 +9,15 @@ using System.Runtime.InteropServices; namespace Microsoft.AspNetCore.DataProtection.Internal { - internal static class DockerUtils + internal static class ContainerUtils { - private static Lazy _isDocker = new Lazy(IsProcessRunningInDocker); + private static Lazy _isContainer = new Lazy(IsProcessRunningInContainer); - public static bool IsDocker => _isDocker.Value; + public static bool IsContainer => _isContainer.Value; public static bool IsVolumeMountedFolder(DirectoryInfo directory) { - if (!IsDocker) + if (!IsContainer) { return false; } @@ -77,14 +77,21 @@ namespace Microsoft.AspNetCore.DataProtection.Internal 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)) { // we currently don't have a good way to detect if running in a Windows container return false; } + // Try to detect docker using the cgroups process 1 is in. const string procFile = "/proc/1/cgroup"; if (!File.Exists(procFile)) { diff --git a/src/DataProtection/DataProtection/src/Repositories/FileSystemXmlRepository.cs b/src/DataProtection/DataProtection/src/Repositories/FileSystemXmlRepository.cs index 7ceede33d1..9eef2e2f18 100644 --- a/src/DataProtection/DataProtection/src/Repositories/FileSystemXmlRepository.cs +++ b/src/DataProtection/DataProtection/src/Repositories/FileSystemXmlRepository.cs @@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.DataProtection.Repositories 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 _logger.UsingEphemeralFileSystemLocationInContainer(Directory.FullName); diff --git a/src/DataProtection/DataProtection/test/DockerUtilsTests.cs b/src/DataProtection/DataProtection/test/ContainerUtilsTests.cs similarity index 87% rename from src/DataProtection/DataProtection/test/DockerUtilsTests.cs rename to src/DataProtection/DataProtection/test/ContainerUtilsTests.cs index 9ede10426b..413202e737 100644 --- a/src/DataProtection/DataProtection/test/DockerUtilsTests.cs +++ b/src/DataProtection/DataProtection/test/ContainerUtilsTests.cs @@ -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. using System.IO; @@ -8,7 +8,7 @@ using Xunit; namespace Microsoft.AspNetCore.DataProtection.Test { - public class DockerUtilsTests + public class ContainerUtilsTests { // example of content from /proc/self/mounts private static readonly string[] fstab = new [] @@ -37,7 +37,7 @@ namespace Microsoft.AspNetCore.DataProtection.Test [InlineData("../dir")] public void DeterminesFolderIsNotMounted(string directory) { - Assert.False(DockerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab)); + Assert.False(ContainerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab)); } [ConditionalTheory] @@ -50,7 +50,7 @@ namespace Microsoft.AspNetCore.DataProtection.Test [InlineData("/app/subdir/two/")] public void DeterminesFolderIsMounted(string directory) { - Assert.True(DockerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab)); + Assert.True(ContainerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab)); } } }