如何将数据保护密钥存储在docker容器之外



我正在学习将blazor服务器应用程序加载到docker容器(aspnet core 3.0.201(上。我已经成功地将图像加载到容器上。我可以创建一个应用程序来构建它,但在运行blazor服务器应用程序时,我收到了这样的警告:

warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. 
Protected data will be unavailable when container is destroyed.

这是一个警告,但我知道在容器上加载密钥不是一个好的做法,所以我想修复这个警告。欢迎提供任何指导。

您收到的警告是因为ASP.NET Core DataProtection将密钥存储在HOME目录(/root/.aspnet/DataProtection keys(中,因此当容器重新启动密钥丢失时,这可能会使服务崩溃。

这可以通过将密钥保存在以下位置来解决:

  • 在持久位置(卷(持久化密钥并装载码头集装箱的体积

  • 在Azure或Redis 等外部密钥存储中持久保存密钥

有关ASP.NET DataProtection:的更多详细信息

https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-3.1

https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/introduction?view=aspnetcore-3.1

使用以下命令将外部卷(C:\/temp-keys(装载到docker容器卷(/root/.aspnet/DataProtection keys(

docker run -d -v /c/temp-keys:/root/.aspnet/DataProtection-Keys container-name

此外,您需要更新Starup.cs - ConfigureServices以配置DataProtection策略

services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(@"C:temp-keys"))
.UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration()
{
EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
});

也许这是个愚蠢的问题。但是,既然容器是短暂的,并且您的应用程序不需要任何持久数据,我们为什么要关心容器/POD被拆除或重新启动时密钥丢失的问题。它只会生成一个新的密钥,对吧?

最新更新