Aws-sdk-net: StoredProfileAWSCredentials is obsolete

Created on 28 Mar 2017  路  11Comments  路  Source: aws/aws-sdk-net

With the latest version of the SDK, where we use StoredProfileAWSCredentials and StoredProfileFederatedCredentials we get an obsolete warning, e.g. Error CS0618 'StoredProfileAWSCredentials' is obsolete: 'This class is obsolete and will be removed in a future release. Please use Amazon.Runtime.CredentialManagement.NetSDKCredentialsFile or SharedCredentialsFile.

But the suggested replacements do not implement the base class AWSCredentials so they aren't compatible. Are there any docs on how to update?

code is as below:

private static AWSCredentials ReadAwsCredentials(StartupParameters parameters)
{
    if (CommandLineParamsHasAwsCreds(parameters))
    {
        return new BasicAWSCredentials(parameters.AwsAccessKey, parameters.AwsSecretKey);
    }

    if (!string.IsNullOrWhiteSpace(parameters.AwsProfile))
    {
                // warning here
        return new StoredProfileAWSCredentials(parameters.AwsProfile);
    }

    // use implicit credentials from config or profile
    FallbackCredentialsFactory.CredentialsGenerators = new List<FallbackCredentialsFactory.CredentialsGenerator>
    {
        () => new AppConfigAWSCredentials(),
                // warning here
        () => new StoredProfileAWSCredentials(),
                // warning here
        () => new StoredProfileFederatedCredentials(),
        () => new EnvironmentVariablesAWSCredentials()
    };
guidance

Most helpful comment

The obsolete message should be better. I'll work on that.

Here's the relevant doc for using credentials in the SDK:
http://docs-aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-creds.html

In your case here's what I'd suggest:

    private static AWSCredentials GetDefaultAWSCredentialsFromProfile()
    {
        var credentialProfileStoreChain = new CredentialProfileStoreChain();
        AWSCredentials defaultCredentials;
        if (credentialProfileStoreChain.TryGetAWSCredentials("default", out defaultCredentials))
            return defaultCredentials;
        else
            throw new AmazonClientException("Unable to find a default profile in CredentialProfileStoreChain.");
    }

    private static AWSCredentials ReadAwsCredentials(StartupParameters parameters)
    {
        if (CommandLineParamsHasAwsCreds(parameters))
        {
            return new BasicAWSCredentials(parameters.AwsAccessKey, parameters.AwsSecretKey);
        }

        if (!string.IsNullOrWhiteSpace(parameters.AwsProfile))
        {
            var credentialProfileStoreChain = new CredentialProfileStoreChain();
            AWSCredentials credentials;
            if (credentialProfileStoreChain.TryGetAWSCredentials(parameters.AwsProfile, out credentials))
                return credentials;
        }

        // use implicit credentials from config or profile
        FallbackCredentialsFactory.CredentialsGenerators = new List<FallbackCredentialsFactory.CredentialsGenerator>
        {
            () => new AppConfigAWSCredentials(),
            () => GetDefaultAWSCredentialsFromProfile(),
            () => new EnvironmentVariablesAWSCredentials()
        };

All 11 comments

The obsolete message should be better. I'll work on that.

Here's the relevant doc for using credentials in the SDK:
http://docs-aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-creds.html

In your case here's what I'd suggest:

    private static AWSCredentials GetDefaultAWSCredentialsFromProfile()
    {
        var credentialProfileStoreChain = new CredentialProfileStoreChain();
        AWSCredentials defaultCredentials;
        if (credentialProfileStoreChain.TryGetAWSCredentials("default", out defaultCredentials))
            return defaultCredentials;
        else
            throw new AmazonClientException("Unable to find a default profile in CredentialProfileStoreChain.");
    }

    private static AWSCredentials ReadAwsCredentials(StartupParameters parameters)
    {
        if (CommandLineParamsHasAwsCreds(parameters))
        {
            return new BasicAWSCredentials(parameters.AwsAccessKey, parameters.AwsSecretKey);
        }

        if (!string.IsNullOrWhiteSpace(parameters.AwsProfile))
        {
            var credentialProfileStoreChain = new CredentialProfileStoreChain();
            AWSCredentials credentials;
            if (credentialProfileStoreChain.TryGetAWSCredentials(parameters.AwsProfile, out credentials))
                return credentials;
        }

        // use implicit credentials from config or profile
        FallbackCredentialsFactory.CredentialsGenerators = new List<FallbackCredentialsFactory.CredentialsGenerator>
        {
            () => new AppConfigAWSCredentials(),
            () => GetDefaultAWSCredentialsFromProfile(),
            () => new EnvironmentVariablesAWSCredentials()
        };

Thanks so much! One question - is it correct that GetDefaultAWSCredentialsFromProfile throws if it has nothing? Is that to make the FallbackCredentialsFactory fall back to the next generator in the list?

That's correct. The exception triggers FallbackCredentialsFactory to move to the next generator.
Glad to help.

@vellozzi - for some reason this code didn't pick up stored profile credentials,
I am using profilename="default" and the credentials are stored in file credentials

if (!string.IsNullOrWhiteSpace(parameters.AwsProfile))
        {
            var credentialProfileStoreChain = new CredentialProfileStoreChain();
            AWSCredentials credentials;
            if (credentialProfileStoreChain.TryGetAWSCredentials(parameters.AwsProfile, out credentials))
                return credentials;
        }

(the above code's TryGetAWSCredentials() returned false)

however using the old construct works just fine:

var credentials = new StoredProfileAWSCredentials(profileName);

any suggestions what could be going wrong?

@AnatoliiAPFM, can you post the content of your credentials file?
Make sure to redact access/secret keys and any other private information.

@vellozzi - see profile redacted, interestingly there are some extra spaced in some of the profiles, including "default" which I was using

[myprofile1]
aws_access_key_id=********************
aws_secret_access_key=****************************************

[prof]
aws_access_key_id = ********************
aws_secret_access_key = ****************************************

[profxxx]
aws_access_key_id = ********************
aws_secret_access_key = ****************************************

[default]
aws_access_key_id = ********************
aws_secret_access_key = ****************************************

Was there a solution here? I am still seeing the same problem using the default profile name

No. I still can't tell from the information given. Can you open a new issue with your specific code and redacted credentials file?

@vellozzi looks like it was something in my credentials file. After editing the file to anonymize my keys I could no longer reproduce

@vellozzi figured it out it only occurs for me when I use the default profile. I opened #1041

Can we get a convenience function to load a profile?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

krzysztof-at-mp picture krzysztof-at-mp  路  4Comments

Lapeno94 picture Lapeno94  路  4Comments

ShahriatHossain picture ShahriatHossain  路  4Comments

mihafreenode picture mihafreenode  路  4Comments

berkeleybross picture berkeleybross  路  3Comments