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

Nik*_*kul 3 docker

我正在学习将 blazor 服务器应用程序加载到 docker 容器(aspnet 核心 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.
Run Code Online (Sandbox Code Playgroud)

这是一个警告,但我知道在容器上加载密钥不是一个好习惯,所以我想修复警告。任何指导表示赞赏。

Nir*_*edi 5

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

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

  • 在持久位置(卷)保留密钥并将该卷挂载到 docker 容器

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

有关 ASP.NET 数据保护的更多详细信息:

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

https://docs.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的配置数据保护策略

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

  • 这非常有帮助,谢谢。我已经完成了此操作,我可以看到在我的 C 驱动器上创建了一个目录,但该项目仍然将密钥存储在项目下的同名目录中。您是否需要更改目录信息才能获取本地 C 驱动器路径? (3认同)