为AWS Web应用程序存储令牌签名证书的最佳方法是什么?

bzu*_*ith 8 amazon-web-services identityserver4

我在AWS的ElasticBeanstalk上使用IdentityServer4和.NET Core 2.0.我有签署代币的证书.存储此证书并从应用程序中检索证书的最佳方法是什么?我应该坚持使用应用程序文件吗?以某种方式将它扔在环境变量中?

编辑:为了清楚,这是一个令牌签名证书,而不是SSL证书.

F_S*_*O_K 6

我真的不喜欢“令牌签名证书”一词,因为它听起来太好了。您拥有的是一个私钥(作为证书的一部分),每个人都知道您应该保护私钥!

我不会将其存储在您的应用程序文件中。如果有人得到你的源代码,他们不应该也拿到钥匙到您的敏感数据(如果有人有你的签名证书,就可以产生任何标记他们喜欢假装是任何用户)。

我会考虑将证书存储在AWS参数存储区中。您可以将证书粘贴到可以静态加密的参数中。然后,您可以使用AWS策略锁定该参数,以便只有管理员和应用程序才能获得证书-您的顽皮开发人员不需要它!您的应用程序将在需要时提取参数字符串,并将其转换为证书对象。

这就是我在应用程序中存储机密的方式。如果需要,我可以提供更多示例/细节。

编辑 -这是Stu指导的最终结果

该项目需要2个从Nuget到项目的AWS软件包

  • AWSSDK.Extensions.NETCORE.Setup
  • AWSSDK。简单系统管理

在AWS SSM参数存储中创建2个参数,例如:

  • 名为的纯字符串/MyApp/Staging/SigningCertificate,值是Base64编码的.pfx文件
  • 加密的字符串/MyApp/Staging/SigningCertificateSecret,值是上述.pfx文件的密码

这是相关代码:

// In Startup class
private X509Certificate2 GetSigningCertificate()
{
    // Configuration is the IConfiguration built by the WebHost in my Program.cs and injected into the Startup constructor
    var awsOptions = Configuration.GetAWSOptions();
    var ssmClient = awsOptions.CreateServiceClient<IAmazonSimpleSystemsManagement>();

    // This is blocking because this is called during synchronous startup operations of the WebHost-- Startup.ConfigureServices()
    var res = ssmClient.GetParametersByPathAsync(new Amazon.SimpleSystemsManagement.Model.GetParametersByPathRequest()
    {
        Path = "/MyApp/Staging",
        WithDecryption = true
    }).GetAwaiter().GetResult();

    // Decode the certificate
    var base64EncodedCert = res.Parameters.Find(p => p.Name == "/MyApp/Staging/SigningCertificate")?.Value;
    var certificatePassword = res.Parameters.Find(p => p.Name == "/MyApp/Staging/SigningCertificateSecret")?.Value;
    byte[] decodedPfxBytes = Convert.FromBase64String(base64EncodedCert);
    return new X509Certificate2(decodedPfxBytes, certificatePassword);
}

public void ConfigureServices(IServiceCollection servies)
{
    // ...
    var identityServerBuilder = services.AddIdentityServer();
    var signingCertificate = GetSigningCertificate();
    identityServerBuilder.AddSigningCredential(signingCertificate);
    //...
}
Run Code Online (Sandbox Code Playgroud)

最后,您可能需要为可访问这些SSM参数的EC2实例设置IAM角色和/或策略。

编辑:本周,我一直在将Web应用程序SSL终止从负载平衡器移至我的Elastic beantalk实例。这需要将我的私钥存储在S3中。此处来自AWS的详细信息:在Amazon S3中安全存储私钥