Rob*_*son 11 .net sdk credentials amazon-web-services
我使用这里的示例从AWS SecretsManager 中检索 c# 代码中的机密。
我已经通过 AWS CLI 在本地设置了凭证,并且能够使用 AWS CLI 命令“aws secretsmanager list-secrets”检索秘密列表。
但是 c# 控制台应用程序失败并出现错误:
> Unhandled exception. System.AggregateException: One or more errors occurred. (Unable to get IAM security credentials from EC2 Instance Metadata Service.)
---> 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.DefaultInstanceProfileAWSCredentials.GetCredentialsAsync()
at Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at AWSConsoleApp2.GetSecretValueFirst.GetSecret() in D:\Work\Projects\Training\AWSConsoleApp2\AWSConsoleApp2\GetSecretValueFirst.cs:line 53
at AWSConsoleApp2.Program.Main(String[] args) in D:\Work\Projects\Training\AWSConsoleApp2\AWSConsoleApp2\Program.cs:line 11
Run Code Online (Sandbox Code Playgroud)
当我更改原始构造函数调用时
IAmazonSecretsManager 客户端 = 新 AmazonSecretsManagerClient();
添加 AWSCredentials 类型的继承参数
IAmazonSecretsManager client = new AmazonSecretsManagerClient(new StoredProfileAWSCredentials());
它工作正常。
类 StoredProfileAWSCredentials 已过时,但可以使用它。我使用在其他机器上没有错误的库,我无法更改它们。
我使用属于 Administrators 组并且对 SecretsMnager 具有完全访问权限的用户的凭据。区域已在 c# 代码中正确设置,配置文件为默认值。
有任何想法吗?感谢提前
Nin*_*ark 18
我多次遇到这个问题,但无法使用上述解决方案解决它。
对我有用的是使用AWS_PROFILE
环境变量显式设置我的AWS配置文件并将其设置为我想要使用的配置文件。
今天又遇到这个问题了,还是不行。最终解决这个问题的是设置AWS_ACCESS_KEY_ID
和AWS_SECRET_ACCESS_KEY
环境变量。
我担心有一天我会用尽其他方法来向 AWS 提供凭证。
Mar*_*ius 15
由于 AWS SDK 凭证配置引起了很多麻烦,因此我将介绍一些上下文。首先,如果您使用 dotnet core,请使用 AWSSDK.Extensions.NETCore.Setup 包 ( https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg- config-netcore.html),它将尊重您的 appsettings.json。
{
"AWS": {
"Region": "eu-west-1",
"Profile": "theprofileyouwantouse"
}
}
Run Code Online (Sandbox Code Playgroud)
项目:
<ItemGroup>
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.1" />
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.1.71" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
例子:
var config = host.Services.GetService<IConfiguration>();
var options = config.GetAWSOptions();
using var client = options.CreateServiceClient<IAmazonSecurityTokenService>();
var result = await client.GetCallerIdentityAsync(new Amazon.SecurityToken.Model.GetCallerIdentityRequest { });
Run Code Online (Sandbox Code Playgroud)
这将尝试在 ~/AppData/Local/AWSToolkit 中获取加密凭证,其次基于您的共享配置文件 (~/.aws/config)。截至 2021 年 11 月,它在版本 1 共享凭证文件 (~/.aws/credentials)* 中不使用 aws_access_key_id、aws_secret_access_key、aws_session_token
接下来,如果您假设的角色是 AWS SSO,则您的 csproj 文件中需要以下包:
<PackageReference Include="AWSSDK.SSO" Version="3.7.0.94" />
<PackageReference Include="AWSSDK.SSOOIDC" Version="3.7.0.94" />
Run Code Online (Sandbox Code Playgroud)
*如果您碰巧将凭证反向添加到共享凭证文件 (~/.aws/credentials) 作为 [profile myprofile] 而不是仅 [myprofile],则 SDK 将不会按您的预期运行,因此请将其删除。如果您的凭据文件没问题,那么您不必触及它,但请记住,SDK 将不会使用缓存的凭据(如果在该文件中找到任何凭据)。
现在,作者不使用 AWSSDK.Extensions.NETCore.Setup 包,这意味着我们得到的凭证解析路径略有不同。最重要的是:appsettings.json 不受尊重,这意味着您必须指定要以不同方式使用的配置文件,例如通过使用 AWS_PROFILE 环境变量。
其次,我们直接登陆 FallbackCredentialsFactory.cs,它在解析凭据时执行此操作:
CredentialsGenerators = new List<CredentialsGenerator>
{
#if BCL
() => new AppConfigAWSCredentials(), // Test explicit keys/profile name first.
#endif
() => AssumeRoleWithWebIdentityCredentials.FromEnvironmentVariables(),
// Attempt to load the default profile. It could be Basic, Session, AssumeRole, or SAML.
() => GetAWSCredentials(credentialProfileChain),
() => new EnvironmentVariablesAWSCredentials(), // Look for credentials set in environment vars.
() => ECSEC2CredentialsWrapper(proxy), // either get ECS credentials or instance profile credentials
};
Run Code Online (Sandbox Code Playgroud)
现在解析凭据“ECSEC2”的最后一步有一个后备,返回以下内容:
DefaultInstanceProfileAWSCredentials.Instance
这导致我们发现了作者所看到的错误。
概括:
小智 8
如果有人使用 docker-compose 并收到此错误,我会将其添加到我的 docker-compose.override.yml 文件中,并且它能够读取我的凭据
volumes:
- ~/.aws/:/root/.aws:ro
Run Code Online (Sandbox Code Playgroud)
小智 7
我遇到了同样的问题,这是我在开发环境中修复它的方法
这里需要注意的是,访问密钥管理器的用户配置文件应该有一个为 Secrets 管理器分配的有效安全组。
试试吧,让我知道,它是怎么回事。
小智 7
同样的问题,通过删除 $HOME/.aws/config 和凭证文件并使用 AWS CLI 重新创建来解决。
就我而言,我将笔记本电脑从 Windows 切换到新的 MBP。我通过复制 .aws 目录文件设置了新环境,并确认 AWS CLI 正常工作。令人困惑的是,dotnet SDK 因相同的错误而失败。
归档时间: |
|
查看次数: |
23011 次 |
最近记录: |