简体   繁体   English

数据保护 - 在多个应用程序之间共享机器密钥

[英]DataProtection - Share machine key between multiple applications

Let's suppose that we have two APIs, one for UserManagement and one for Auth .假设我们有两个 API,一个用于UserManagement ,一个用于Auth

UserManagement API is responsible for initial invitation email (where i need a ResetPasswordToken because this is my current app flow) and Auth API is responsible for password recovery (where i need a ResetPasswordToken ). UserManagement API负责初始邀请电子邮件(我需要ResetPasswordToken因为这是我当前的应用程序流程), Auth API负责密码恢复(我需要ResetPasswordToken )。

Of course, i need to specify the same machine key for both applications.当然,我需要为两个应用程序指定相同的机器密钥。

Let's also suppose that those two applications will be deployed behind a load balancer.我们还假设这两个应用程序将部署在负载均衡器之后。 2 apps x 3 instances. 2 个应用程序 x 3 个实例。

It is sufficient to have the same shared location for persisting keys (Redis or so) in both APIs?在两个 API 中为持久化密钥(Redis 左右)拥有相同的共享位置就足够了吗?

services.AddDataProtection().PersistKeysToRedis(/* */);

I'm thinking that if it works for one app, multiple instances scenario, it will work for multiple apps, multiple instances scenario too.我在想,如果它适用于一个应用程序、多个实例场景,它也适用于多个应用程序、多个实例场景。

PS: I wasn't able to find anything about any locking mechanism (it seems that there is one just looking at how it behaves) PS:我找不到任何关于任何锁定机制的信息(似乎有人只是在看它的行为方式)

Another thing that i'm concerned of: race condition?!我担心的另一件事是:比赛条件?!

Duc_Thuan_Nguy Jun 9, 2017 Duc_Thuan_Nguy 2017 年 6 月 9 日

Out of curiosity, how does key rolling handle concurrency?出于好奇,密钥滚动如何处理并发? For example, let's say we have a web-farm with 2 machines and a shared network directory.例如,假设我们有一个带有 2 台机器和一个共享网络目录的网络农场。 There may be a race condition in which both machines want to roll a new key at the same time.可能存在竞争条件,其中两台机器都希望同时滚动新密钥。 How is this situation handled?这种情况如何处理? Or the two machines can roll their own new keys and as long as they can have access to both new keys, they can unprotect data smoothly?或者两台机器可以滚动自己的新密钥,只要他们可以访问两个新密钥,就可以顺利解除数据保护?

Comment reference: https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-management评论参考: https : //docs.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-management

Later edit: It looks like if you have multiple apps it isn't sufficient to specify that you want to persist keys in the same location.稍后编辑:看起来如果您有多个应用程序,则指定您希望将密钥保留在同一位置是不够的。 There is a concept of application discriminator (all apps being isolated).有一个应用程序鉴别器的概念(所有应用程序都被隔离)。

You will need something like the following:您将需要类似以下内容:

services.AddDataProtection(configure => {
                configure.ApplicationDiscriminator = "App.X";
            }).PersistKeysToRedis(/* */);

Locking and race condition questions are still valid.锁定和竞争条件问题仍然有效。

No, it's not sufficient.不,这还不够。 ASP.NET Core's data protection isolates applications by default based on file paths, or IIS hosting information, so multiple apps can share a single keyring, but still not be able to read each other's data. ASP.NET Core 的数据保护默认根据文件路径或 IIS 托管信息隔离应用程序,因此多个应用程序可以共享一个密钥环,但仍然无法读取彼此的数据。

As the docs state正如文档所述

By default, the Data Protection system isolates apps from one another, even if they're sharing the same physical key repository.默认情况下,数据保护系统会将应用彼此隔离,即使它们共享相同的物理密钥存储库。 This prevents the apps from understanding each other's protected payloads.这会阻止应用程序了解彼此的受保护负载。 To share protected payloads between two apps, use SetApplicationName with the same value for each app要在两个应用程序之间共享受保护的有效负载,请为每个应用程序使用具有相同值的 SetApplicationName

public void ConfigureServices(IServiceCollection services)
{
    services.AddDataProtection()
        .SetApplicationName("shared app name");
}

A quick update on this one: it seems like it's possible to eliminate race conditions by using the DisableAutomaticKeyGeneration method on all of your apps except the "main" one.关于这个的快速更新:似乎可以通过在除“主要”应用程序之外的所有应用程序上使用DisableAutomaticKeyGeneration方法来消除竞争条件。

ie it will be即它将是

    services.AddDataProtection()
        .SetApplicationName("shared app name");

for the main one and对于主要的和

    services.AddDataProtection()
        .SetApplicationName("shared app name")
        .DisableAutomaticKeyGeneration();

for all other apps对于所有其他应用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM