【问题标题】:AWS SDK is using default profile instead of specified oneAWS SDK 使用默认配置文件而不是指定的配置文件
【发布时间】:2022-10-19 06:31:51
【问题描述】:

我在本地有多个 AWS 配置文件,我可以确认任一配置文件配置在我的应用程序之外按预期工作。

在我的应用程序中,我试图在appsettings.Development.json 中设置配置文件,我可以在其中确认所有其他环境变量都已正确加载。

...
  "AWS": {
    "Profile": "testprofile",
    "Region": "us-east-2",
    "SecretOne": "my/Secret/Key"
  },
...

testprofile 肯定存在。我可以将它与 AWS CLI 工具一起使用,并获取我试图从我的应用程序中找到的秘密。当我使用AmazonSecretsManagerClient 获取它们时,客户端配置了我的默认配置文件。

builder.Services.AddDefaultAWSOptions(builder.Configuration.GetAWSOptions());
builder.Services.AddAWSService<IAmazonSecretsManager>();
builder.Host.ConfigureAppConfiguration((_, configurationBuilder) =>
{
    configurationBuilder.AddAmazonSecretsManager(builder.Configuration["AWS:Region"], builder.Configuration["AWS:GoogleIdentity"]);
});
builder.Services.Configure<ApiCredentials>(builder.Configuration);

我什至可以确认builder.Configuration.GetAWSOptions() 正在获取配置文件的预期“testprofile”值。

我的program.cs 配置可能有问题,但我终生无法确定。


更新- 更多信息

100% 确定这是获取 appsettings.json 中设置的正确配置文件

builder.Services.AddDefaultAWSOptions(builder.Configuration.GetAWSOptions());

AmazonSecretsManagerClient 的构造函数无法识别它,而是获取“默认”配置文件。

        using (var client = new AmazonSecretsManagerClient(RegionEndpoint.GetBySystemName(_region)))

在这里,client -> Credentials 是“默认”配置文件。

【问题讨论】:

    标签: amazon-web-services .net-6.0


    【解决方案1】:

    看起来有点复杂,但不知何故,代码比我关注的任何教程都要少。它有效,但我仍然会接受更好的答案。

    我把这个放到我的Program.cs

    var chain = new CredentialProfileStoreChain();
    AWSCredentials basicProfile;
    if (!chain.TryGetAWSCredentials(builder.Configuration["AWS:Profile"], out basicProfile))
    {
        throw new Exception($"Could not get AWS profile credentials for {builder.Configuration["AWS:Profile"]}");
    }
    var client = new AmazonSecretsManagerClient(basicProfile, builder.Configuration.GetAWSOptions().Region);
    var cache = new SecretsManagerCache(client);
    var secretOneResult = cache.GetSecretString(builder.Configuration["AWS:SecretOne"]).Result;
    var secretOneSettings = JsonConvert.DeserializeObject<SecretOneConfig>(secretOneResult);
    

    【讨论】:

      猜你喜欢
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      • 2021-03-12
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多