aws-sdk-net: AWSCredentialsFactory.TryGetAWSCredentials() returns invalid SSOAWSCredentials

Description

My company has an SSO setup in AWS (auth via Azure) to allow users to obtain temporary role credentials. Having just updated one of our .NET AWS applications to AWSSDK v3.7.x (via NuGET) from v3.3.x, I was no longer able to run the app locally to access AWS using my credentials (which can be successfully obtained via AWS CLI v2).

The app was previously (using AWSSDK v3.3.x) implicitly detecting my credentials in both ~/.aws/credentials and local Environment vars when creating an AmazonS3Client(), but this is now failing. According to the sequence of credentials detection, it should be fine, and it does appear as if the SDK is finding my shared credentials file, since it’s returning the correct type, but the app is throwing an exception when trying to use the returned AWSCredentials object.

Following the basic guide to access credentials and profiles, creating a new SharedCredentialsFile() successfully locates and loads my credentials file, and the return out AWSCredentials is returning an SSOAWSCredentials object, but the object is invalid because two mandatory properties in the (SSOAWSCredentialsOptions)Options property are null: ClientName and SsoVerificationCallback.

When attempting to use the generated credentials in an AmazonS3Client(), exceptions are returned saying either:

    Value cannot be null. Parameter name: Options property cannot be empty: ClientName

Or:

    Value cannot be null. Parameter name: Options property cannot be empty: SsoVerificationCallback

But, if I manually set those two properties, it all works:

    ((SSOAWSCredentials)awsCredentials).Options.ClientName = "SSO";
    ((SSOAWSCredentials)awsCredentials).Options.SsoVerificationCallback = new Action<SsoVerificationArguments>(delegate (SsoVerificationArguments ssoArgs) {
        System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() {
            FileName = ssoArgs.VerificationUriComplete,
            UseShellExecute = true
        });
    });

ClientName appears to accept (and work with) any random string. It doesnt appear to be relevant to any credentials Profile names. The new inline delegate() for SsoVerificationCallback is simply sending VerificationUriComplete to my systems default browser to do the SSO login and device authentication. (I have a check to make sure System.Environment.UserInteractive is true as well.)

Reproduction Steps

Attempt to access any AWS service using an SSOAWSCredentials object returned by AWSCredentialsFactory.TryGetAWSCredentials()

Environment

  • SDK Version: AWSSDK.Core v3.7.0.2, AWSSDK.S3 v3.7.0.2, AWSSDK.SSO v3.7.0.1 AWSSDK.SSOOIDC v3.7.0.1
  • OS Info: Windows 10 (20.04) v10.0.19041.867
  • Build Environment: Visual Studio 2017
  • Targeted .NET Platform: .NET Core v2.1

Resolution

Can the AWSSDK be updated to return some sort of default values in ClientName and SsoVerificationCallback in an SSOAWSCredentials.Options object?

Or at least update the linked documentation above with a notice to manually handle this scenario and specify that it must be dealt with?


This is a 🐛 bug-report

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 17 (6 by maintainers)

Most upvoted comments

I have a suggestion that I think would resolve this in a satisfactory way (hopefully). I validated when the ClientName and SsoVerificationCallback are provided, and the authentication is successful it caches the authorization in the .aws\sso\cache\ folder in the same way that aws sso login cli command caches the authorization token. This cache is then used to avoid any additional calls to the SSO login process if a valid token is present.

I think the biggest issue here is that it’s currently checking for that ClientName and SsoVerificationCallback are provided even if it has a valid auth token cached and doesn’t even need to execute an SSO login.

If it would delay the validation of those two properties until they were actually required then users could use the AWS CLI or similar method to execute their AWS SSO login. Then the code can happily use that login until that cached authorization is expired. If they would prefer that their project code does the login then they would need to add custom code like mentioned aboved to allow that individual project to execute the login process.

TL;DR: If it’s going to use the cached login then don’t check for these properties until a new login is needed. Then the project can support other methods of SSO login like using the AWS CLI’s SSO login.

I thought I had tested it without the SSOOIDC project, but I guess the dlls were still in the bin folder. I took a look at the code again and it would be pretty easy to remove the reference to SSOOIDC, but I would not be able to remove the AWSSDK.SSO reference since it’s used to generate the credentials even when the token is cached.

However, if you don’t want you include the SSO and SSOOIDC Dlls in your production release you can add <PrivateAssets>all</PrivateAssets>

    <PackageReference Include="AWSSDK.SSO" Version="3.7.0.23">
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="AWSSDK.SSOOIDC" Version="3.7.0.23">
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>

This will include them when building locally, but exclude them when using publish to package the project for release.

Based on @KenHundley’s merged PR, I was able to get my .net core test project to work with AWS CLI v2 sso login. I did however have to keep the AWSSDK.SSOOIDC package reference.

I have created AWSSSO.S3Buckets as a sample project for demonstration purposes.

@boyersnet for this specific use case, I was only using it to try to identify where my problem was coming from and limit the number of classes in play. We do actually use it in some legacy situations with .NET Framework though.

I’m already familiar with how the configuration system in .NET Framework & the AWS SDK works. Thanks!

Ah, thanks @KenHundley, that did it. All appears to be working now! Hope this helps the next reader.

Are you running the latest CLI version? I think the may have changed the format of the cached date at some point.