问题描述
我有EC2实例(通过Elastic Beanstalk)运行我的ASP.Net Core 3.1 Web应用程序而没有问题。 AWS凭证包含在与实例配置的密钥对中。
我现在想将数据保护密钥存储在为它们创建的S3存储桶中,以便可以在所有EC2实例之间共享密钥。但是,当我在Startup.ConfigureServices中添加此服务时,会在本地收到运行时错误:
services.AddDefaultAWSOptions(Configuration.GetAWSOptions("AWS"));
services.AddAWSService<IAmazonS3>();
services.AddDataProtection()
.SetApplicationName("Crums")
.PersistKeysToAWSSystemsManager("/CrumsWeb/DataProtection");
如果我注释掉上面的 .PersistKeysToAWSSystemsManager(“ / CrumsWeb / DataProtection”); 行,则我的应用程序在本地运行良好。当我取消注释该行时,会发生错误。因此,它与此有关,但我似乎无法弄清楚。
我本来要由hotchkj使用PersistKeysToAwsS3,但是当AWS推出PersistKeysToAWSSystemsManager时不推荐使用。
运行时错误 AmazonClientException:在我的Program.cs中的CreateHostBuilder上未配置RegionEndpoint或ServiceURL :
我已经花了很多时间尝试使用这些网站的建议让Visual Studio 2019在本地运行我的应用程序:
https://aws.amazon.com/blogs/developer/configuring-aws-sdk-with-net-core/
https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-netcore.html
ASP NET Core AWS No RegionEndpoint or ServiceURL configured when deployed to Heroku
No RegionEndpoint or ServiceURL configured
https://github.com/secretorange/aws-aspnetcore-environment-startup
https://www.youtube.com/watch?v=C4AyfV3Z3xs&ab_channel=AmazonWebServices
我的appsettings.Development.json(我也在appsettings.json中也尝试过)包含:
"AWS": {
"Profile": "default","Region": "us-east-1","ProfilesLocation": "C:\\Users\\username\\.aws\\credentials"
}
凭据文件包含:
[default]
aws_access_key_id = MY_ACCESS_KEY
aws_secret_access_key = MY_SECRET_KEY
region = us-east-1
toolkit_artifact_guid=GUID
解决方法
我最终放弃了PersistKeysToAWSSystemsManager来存储我的数据保护密钥,因为我不想设置另一个AWS服务,只是将密钥存储在他们的Systems Manager中。我已经在为S3帐户付款,所以我选择使用已弃用的NuGet软件包AspNetCore.DataProtection.Aws.S3。
我在为密钥创建的存储桶中使用服务器端加密。这是Startup.cs中的代码:
services.AddDataProtection()
.SetApplicationName("AppName")
.PersistKeysToAwsS3(new AmazonS3Client(RegionEndpoint.USEast1),new S3XmlRepositoryConfig("S3BucketName")
{
KeyPrefix = "DataProtectionKeys/",// Folder in the S3 bucket for keys
});
请注意PersistKeysToAwsS3中的RegionEndpoint参数,该参数解决了No RegionEndpoint或ServiceURL配置错误。
我将AmazonS3FullAccess策略添加到了在我所有实例中运行的IAM角色中。 这为实例提供了访问S3存储桶的权限。我的本地开发计算机似乎也可以访问S3存储桶,尽管我不知道它从何处获取凭据。我尝试了几次appsettings.json和凭据文件更改为本地设置的区域和凭据,但没有任何效果。也许它使用的是我在设置AWS Toolkit in Visual Studio时输入的凭据。