asp.net-core - 多 pod 的数据保护 key 轮换策略是什么?

标签 asp.net-core data-protection

我使用 services.AddDataProtection().PersistKeysToFileSystem(path).ProtectKeysWithAzureKeyVault(authData). 来加密数据保护 key 。在部署后的 24 小时内,没有生成新的数据保护 key 。这意味着在当前数据保护 key 到期之前,不会进行任何加密。

现在,为了强制生成数据保护 key ,我可以删除最新的数据保护 key 并重新启动 pod,但这将导致这里描述的竞争条件:https://github.com/dotnet/aspnetcore/issues/28475所以我需要重新启动它们。使用现已删除的数据保护 key 加密 cookie 的用户是否会被注销?

这也让我很困扰,因为当数据保护 key 每 180 天轮换一次时究竟会发生什么?用户的 cookie 使用它加密,所以如果他们登录,他们的 cookie 将不再有效吗? 此外,如果假设 6 个 pod 中的一个生成新的数据保护 key ,其余的何时同步?您是否有可能使用一个 pod 获取表单,然后使用另一个 pod 提交它,而他们使用不同的数据保护 key ?

如何处理这一切?

最佳答案

此问题仍未解决,有一个元问题链接到有关该主题的其他未解决问题。

https://github.com/dotnet/aspnetcore/issues/36157

我有同样的问题,但我有 AWS Lambda 函数而不是 pod。

我通过禁用自动 key 生成解决了这个问题:

    services.AddDataProtection()
        .DisableAutomaticKeyGeneration()

并自己管理 key 。我至少有两把 key :

  • 默认键。激活后 190 天过期。这是 180 天内的默认 key 。
  • 下一个键。它在当前 key 到期前 10 天激活。它在激活后 190 天后过期。这将是 180 天内的默认 key 。

这是我在部署 lambda 函数之前执行的代码,然后每月执行一次:

public class KeyringUpdater
{
    private readonly ILogger<KeyringUpdater> logger;
    private readonly IKeyManager keyManager;

    public KeyringUpdater(IKeyManager keyManager, ILogger<KeyringUpdater> logger)
    {
        this.logger = logger;
        this.keyManager = keyManager;
    }

    private IKey? GetDefaultKey(IReadOnlyCollection<IKey> keys)
    {
        var now = DateTimeOffset.UtcNow;
        return keys.FirstOrDefault(x => x.ActivationDate <= now && x.ExpirationDate > now && x.IsRevoked == false);
    }

    private IKey? GetNextKey(IReadOnlyCollection<IKey> keys, IKey key)
    {
        return keys.FirstOrDefault(x => x.ActivationDate > key.ActivationDate && x.ActivationDate < key.ExpirationDate && x.ExpirationDate > key.ExpirationDate && x.IsRevoked == false);
    }

    public void Update()
    {
        var keys = this.keyManager.GetAllKeys();
        logger.LogInformation("Found {Count} keys", keys.Count);
        var defaultKey = GetDefaultKey(keys);
        if (defaultKey == null)
        {
            logger.LogInformation("No default key found");
            var now = DateTimeOffset.UtcNow;
            defaultKey = this.keyManager.CreateNewKey(now, now.AddDays(190));
            logger.LogInformation("Default key created. ActivationDate: {ActivationDate}, ExpirationDate: {ExpirationDate}", defaultKey.ActivationDate, defaultKey.ExpirationDate);
            keys = this.keyManager.GetAllKeys();
        }
        else
        {
            logger.LogInformation("Found default key. ActivationDate: {ActivationDate}, ExpirationDate: {ExpirationDate}", defaultKey.ActivationDate, defaultKey.ExpirationDate);
        }
        var nextKey = GetNextKey(keys, defaultKey);
        if (nextKey == null)
        {
            logger.LogInformation("No next key found");
            nextKey = this.keyManager.CreateNewKey(defaultKey.ExpirationDate.AddDays(-10), defaultKey.ExpirationDate.AddDays(180));
            logger.LogInformation("Next key created. ActivationDate: {ActivationDate}, ExpirationDate: {ExpirationDate}", nextKey.ActivationDate, nextKey.ExpirationDate);
        }
        else
        {
            logger.LogInformation("Found next key. ActivationDate: {ActivationDate}, ExpirationDate: {ExpirationDate}", nextKey.ActivationDate, nextKey.ExpirationDate);
        }
    }
}

关于asp.net-core - 多 pod 的数据保护 key 轮换策略是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65235299/

相关文章:

asp.net-core - ASP.NET Core 模板 3.1.5 - serviceDependencies.json

c# - 无法跟踪实体类型 'BookLoan' 的实例

asp.net - Kestrel HTTPS POST 在生产时导致 400 错误请求状态代码 (ASP.NET Core 2.x)

c# - 在 ASP.NET Core 的自定义中间件中获取对请求的 Controller 和操作的引用

objective-c - Core Data 持久存储保护

javascript - 为什么 Google +1 会记录我的鼠标移动?

docker - Dotnetcore 容器启动时出现错误,询问我是否要运行 SDK 命令

c# - 我怎样才能保护字符串,例如C#

ios - 数据保护功能是否也会在 Core Data 的幕后保护 sqlite 文件?

ios - 当需要后台模式访问时,如何在 iOS 上安全地存储数据?