【问题标题】:How to store Data Protection-keys outside of the docker container?如何将数据保护密钥存储在 docker 容器之外?
【发布时间】:2020-08-10 14:50:36
【问题描述】:

我正在学习将 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.

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

【问题讨论】:

标签: docker


【解决方案1】:

您收到的警告是因为 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 以配置 DataProtection 政策

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多