aws / aws-sdk-net

The official AWS SDK for .NET. For more information on the AWS SDK for .NET, see our web site:
http://aws.amazon.com/sdkfornet/
Apache License 2.0
2.06k stars 854 forks source link

Loading credentials from custom profile location #1399

Closed abjrcode closed 5 years ago

abjrcode commented 5 years ago

According to documentation it should be possible to override the profile location and profile name via AppConfig or directly by setting the properties on AWSConfigs.

Tried both approaches without avail

Expected Behavior

Running this:

 var webHostBuilder = new WebHostBuilder()
                .UseStartup<Startup>()
                .UseEnvironment("Development")
                .ConfigureServices(serviceCollection =>
                {
                    AWSConfigs.AWSProfileName = "dummy";
                    AWSConfigs.AWSProfilesLocation = Path.Join(typeof(TestWebHost).Assembly.Location, "dummy_credentials");
                    serviceCollection.AddSingleton(settings);
                });

            return new TestServer(webHostBuilder).CreateClient();

then later this:

var clientConfig = new AmazonDynamoDBConfig
            {
                ServiceURL = _dynamodbEndpoint // POINTS TO LOCAL CONTAINER ENDPOINT
            };
.
.
.
using (var dynamoClient = new AmazonDynamoDBClient(clientConfig))

Then it should load the credentials from my custom credentials file.

Current Behavior

It doesn't load the credentials from the specified location. Checking the source code I don't even see how that will happen:

  1. https://github.com/aws/aws-sdk-net/blob/master/sdk/src/Services/DynamoDBv2/Generated/_bcl35/AmazonDynamoDBClient.cs#L83
  2. https://github.com/aws/aws-sdk-net/blob/master/sdk/src/Core/Amazon.Runtime/Credentials/FallbackCredentialsFactory.cs
  3. https://github.com/aws/aws-sdk-net/blob/master/sdk/src/Core/Amazon.Runtime/Credentials/FallbackCredentialsFactory.cs#L35
  4. https://github.com/aws/aws-sdk-net/blob/master/sdk/src/Core/Amazon.Runtime/Credentials/FallbackCredentialsFactory.cs#L54
  5. https://github.com/aws/aws-sdk-net/blob/master/sdk/src/Core/Amazon.Runtime/Credentials/FallbackCredentialsFactory.cs#L69

From what I see the behaviour described in the documentation page only applies to BCL and not .NET Core?

Context

I wanted to supply dummy credentials because I am using DynamoDb container in my integration tests but it was still trying to look for the default credential profile in ~/.aws/credentials which doesn't necessarily exists. Basically trying to make the tests portable and independent of global environment by supplying fake credentials.

Your Environment

.NET Core Info

Runtime Environment: OS Name: Windows OS Version: 10.0.18362 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\2.1.402\

Host (useful for support): Version: 2.1.4 Commit: 85255dde3e

.NET Core SDKs installed: 2.0.0 [C:\Program Files\dotnet\sdk] 2.1.4 [C:\Program Files\dotnet\sdk] 2.1.104 [C:\Program Files\dotnet\sdk] 2.1.202 [C:\Program Files\dotnet\sdk] 2.1.402 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]


* Contents of project.json/project.csproj: Empty

[logging]: http://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-other.html
klaytaybai commented 5 years ago

It looks like you are not using the .NET Core Setup Extensions. You can read some documentation on this here. Let me know if that helps.

abjrcode commented 5 years ago

So to get the behaviour described in the documentation I must be using the Extensions
I thought they were just added convenience and the core logic is still within the other assemblies I will give them a try. Thank you for your help

abjrcode commented 5 years ago

So this is a bit like going down the rabbit hole.
I managed to pass the dummy credentials in but then it came to my mind that I might have the same issue with region, so I tried passing that as well and now it fails to authenticate although the service endpoint is pointing towards the local dynamo container.

I guess my question is now like this:

Basically I want to reach the following goal: My tests are completely independent from the global environment.

Sorry if I am asking too much and thank you for your help

abjrcode commented 5 years ago

What does the region default to if it cannot find it in any of the fallback mechanisms and the service URL points to localhost? Also it seems you cannot anymore provide custom service client configuration?

klaytaybai commented 5 years ago

If you specify a region, it will override the service url. If you are working with localhost, you shouldn't specify the region. https://github.com/aws/aws-sdk-net/blob/662be216d79d4c34f058d4e1bb1b7891e4d682fc/sdk/src/Core/Amazon.Runtime/ClientConfig.cs#L117-L136

abjrcode commented 5 years ago

Thanks for your kind help sir.