aws-sdk-net: STS Request ("/latest/api/token") gives 403 response when running .Net Core 3.0 or higher on EC2 instance with instance role

Hi There,

We are trying to access secrets manager using AWS SDK for get secret value on a known valid ARN.

When running a very basic .Net Core 3.0 console application on a EC2 instance that has an iam role associated with it’s instance, I get a 403 from the request to /latest/api/token when attempting to get credentials with which to perform request to secrets manager.

When I downgrade to .Net Core 2.2 and change nothing else about the application, it resolves fine and allows access the secrets.

When debugging locally, .Net Core 3.0 works correctly and allows access to secrets. The issue only presents itself when placed on an EC2 Instance with an associated instance role. No credentials are stored in/on the instance itself.

Expected Behavior

When running the provided console application on the EC2 instance, the following output is expected. This output is also the output that is correctly shown when running the version of the console app built targetting .Net Core 2.2. (resources values have been redacted for security):

dotnet ConsoleApp3.0Test.dll
Hello World!
Attempting to connect to arn: {redacted}
Constructing secrets manager configuration options...
Constructing secrets manager provider ...
Loading secrets...
Getting {redacted} secret...
{redacted} is {expected value}

Current Behavior

When running the provided console application on the EC2 instance, targetting .Net Core 3.0 or higher, the following exception is provided:

dotnet ConsoleApp3.0Test.dll
Hello World!
Attempting to connect to arn: {redacted}
Constructing secrets manager configuration options...
Constructing secrets manager provider ...
Loading secrets...
Unhandled exception. Amazon.Runtime.AmazonServiceException: Unable to get IAM se
curity 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](IExecutionCont
ext executionContext)
   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext exec
utionContext)
   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext exec
utionContext)
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext e
xecutionContext)
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext e
xecutionContext)
   at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionCont
ext executionContext)
   at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext ex
ecutionContext)
   at ConsoleApp3._0Test.SecretsManagerConfigurationProvider.LoadAsync() in {user directory}\ConsoleApp3.0Test\Program.cs:line 111
   at ConsoleApp3._0Test.SecretsManagerConfigurationProvider.Load() in {user directory}\ConsoleApp3.0Test\Program.cs:line 107
   at ConsoleApp3._0Test.Program.Main(String[] args) in {user directory}\ConsoleApp3.0Test\Program.cs:line 36

When debugging through the AWS SDK, I found the issue was originating from the first call to /latest/api/token was returning with a HTTP status code of ‘403’. This results in the EnsureStatusCode method failing and then bubbles up to the exception you see above.

Possible Solution

N/A

Steps to Reproduce (for bugs)

  1. Create EC2 instance with instance role which has permissions to peform “Get Secret Value” on the arn for a valid secret.
  2. Run the provided .Net Core 3.0 console application on the EC2 instance. ConsoleApp3.0Test.zip

Context

This prevents us providing a new software release version which contains the update from .Net Core 2.2 to 3.0 and is a major show stopper for us.

Your Environment

  • AWSSDK.Core version used: 3.3.104.21
  • AWSSDK.SecretsManager version used: 3.3.101.78
  • Service assembly and version used:
  • Operating System and version: Microsoft Windows Server 2012 R2 Standard 6.3.9600 N/A Build 9600
  • Visual Studio version: Microsoft Visual Studio Professional 2019 Version 16.4.3
  • Targeted .NET platform: .Net Core 3.0

.NET Core Info

  • .NET Core version used for development: 3.1.101
  • .NET Core version installed in the environment where application runs: 3.1.1
  • Output of dotnet --info:
dotnet --info
  It was not possible to find any installed .NET Core SDKs
  Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from:
      https://aka.ms/dotnet-download
Host (useful for support):
  Version: 3.1.1
  Commit:  a1388f194c
.NET Core SDKs installed:
  No SDKs were found.
.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNe
tCore.All]
  Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNe
tCore.App]
  Microsoft.AspNetCore.App 3.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNe
tCore.App]
  Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNe
tCore.App]
  Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.
App]
  Microsoft.NETCore.App 3.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.
App]
  Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.
App]

  • Contents of project.json/project.csproj:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <RootNamespace>ConsoleApp3._0Test</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AWSSDK.SecretsManager" Version="3.3.101.78" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.1" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
  </ItemGroup>

</Project>

Additional Notes

When running PowerShell for AWS and executing the command for Get Secret Value on the EC2 instance we get the values back correctly, so there is no issue with the permission with the role for the EC2 instance.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 15 (5 by maintainers)

Most upvoted comments

Please leave this closed.