【问题标题】:IAM security exception when using DynamoDB in a virtual machine在虚拟机中使用 DynamoDB 时出现 IAM 安全异常
【发布时间】:2020-07-23 06:32:29
【问题描述】:

我正在使用 Visual Studio 创建一个 ASP.NET 项目,该项目将 DynamoDB 与 Amazon .NET SDK 结合使用。我已经安装了 Amazon Visual Studio 工具包并使用 .NET 加密商店中的密钥/密钥创建了一个 default 配置文件。

当我在 Visual Studio 内的开发机器上本地运行 ASP.NET 程序时,一切正常。 当我将同一个项目部署到虚拟机(在本地 LAN 上运行)并且 IIS 尝试执行代码时,它会抛出异常:

   Amazon.Runtime.AmazonServiceException: Unable to get IAM security credentials from EC2 Instance Metadata Service.
   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.FetchCredentials()
   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentials()
   at Amazon.Runtime.Internal.Util.SdkCache.CacheKey.Create(AmazonServiceClient client, Object cacheType)
   at Amazon.Runtime.Internal.Util.SdkCache.GetCache[TKey,TValue](AmazonServiceClient client, Object cacheIdentifier, IEqualityComparer`1 keyComparer)
   at Amazon.DynamoDBv2.DocumentModel.Table.LoadTableInfo()
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.GetUnconfiguredTable(String tableName)
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.GetTargetTable(ItemStorageConfig storageConfig, DynamoDBFlatConfig flatConfig, DynamoDBConsumer consumer)
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.ConvertQueryHelper[T](DynamoDBFlatConfig currentConfig, ItemStorageConfig storageConfig, QueryFilter filter, List`1 indexNames)
   at Authentication.Controllers.AuthController.VerifyLicense(Object details)

这显然是凭据问题,但我无法弄清楚为什么它在 VM 和本地 VS IDE 中的行为不同。根据DynamoDB docs,如果您使用默认配置文件,它应该在您构建/部署它时自动将凭据嵌入到程序中。它似乎没有这样做。

我在项目中与 DynamoDB 初始化相关的唯一代码是:

            AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig();
            clientConfig.RegionEndpoint = RegionEndpoint.USEast1;
            AmazonDynamoDBClient client = new AmazonDynamoDBClient(clientConfig); // using the "default" profile in Visual Studio which contains the AWS credentials

是否有一个特殊指令要添加到项目或代码中以嵌入 Secret/Key 凭据(这将是不安全的),或者我是否遗漏了其他内容? 在 VS 的 AWS Explorer 中创建配置文件时,我是否应该使用 Shared Credentials File 而不是 .NET Encrypted Store? (文档对差异非常吝啬)

【问题讨论】:

  • 虚拟机是指在 EC2 实例上和本地您自己的机器上?
  • 非 EC2 在 LAN 上运行

标签: asp.net amazon-web-services amazon-dynamodb virtual-machine


【解决方案1】:

所以我能够拼凑出解决方案,这取决于您的环境。

显然 AWS SDK 没有将密钥和密钥嵌入到项目中。它仅将项目引用到 .NET 加密存储和默认配置文件(位于安装 VS 的同一台机器上)。

如果您想在不同的机器(例如虚拟机)上使用 DynamoDB 项目,那么您需要创建一个包含您的密钥和密钥的凭证文件,并更新您的项目/部署以引用凭证文件的位置(超级不安全)。

我在这里找到了示例: Amazon.Runtime.AmazonServiceException: Unable to find credentials https://aws.amazon.com/blogs/developer/referencing-credentials-using-profiles/

由于我使用的是 SDK.NET,因此我能够更新 Web.Release.config 文件以包含以下指令,该指令更新 IIS 中的 web.config 文件以添加 AWSProfilesLocation 密钥,然后由项目使用。这仅包含在 Release web.config 文件中,因为在本地机器上调试时 VS 使用 AWS .NET 加密存储。

<appSettings xdt:Transform="Replace">
    <add key="AWSProfilesLocation" value="C:\HOSTING_FILES\App_Data\credentials" />
  </appSettings>

我使用的一个小技巧是将凭据文件保存到 VS 项目的 App_Data 目录中,以便在部署项目时它会自动将最新的凭据文件复制到虚拟机。或者,您也可以创建一个指令以在发布时复制您想要的任何文件夹:How do you include additional files using VS2010 web deployment packages?

如果您要部署到 AWS EC2 实例或 Lambda,则无需包含凭证文件,因为 AWS 环境中的项目已经可以访问该信息: https://docs.amazonaws.cn/powershell/latest/userguide/specifying-your-aws-credentials.html https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/authentication-and-access-control.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 2011-04-01
    • 2018-01-14
    • 2019-12-10
    • 2022-08-24
    • 1970-01-01
    相关资源
    最近更新 更多