-
Notifications
You must be signed in to change notification settings - Fork 867
Can't use SDK with AWS SSO credentials are mounted in docker container #2477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Similar issue in PowerShell aws/aws-tools-for-powershell#299. |
Maybe related: It looks like a similar problem was resolved in 537a981 (see #1850) but then the fix was lost in fbd05f4#diff-53123b41f5f6d93f2030a4f5c22f41724b1896dd082b5445f614837c91c29f5f. It looks to me like a code generator was executed and the changes made in #1850 were lost. |
Here's a workaround that I found. First, I modify my code to support both AWS credentials from environment variables or from credentials file: using Amazon;
using Amazon.Runtime;
using Amazon.Runtime.CredentialManagement;
using Amazon.S3;
using System;
using System.Threading.Tasks;
namespace WithOldSdk
{
// docker run --rm -it -v "$(Get-Location):/src" -v "$env:USERPROFILE/.aws:/root/.aws:ro" -w /src mcr.microsoft.com/dotnet/core/sdk:3.1 dotnet run
// Found 327 buckets.
internal class Program
{
static async Task Main(string[] args)
{
AWSCredentials credentials;
if (Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID") != null)
{
Console.WriteLine("Getting credentials from environment variables.");
credentials = new EnvironmentVariablesAWSCredentials();
}
else
{
Console.WriteLine("Getting credentials from credentials files.");
var profileName = args.Length == 0 ? "default" : args[0];
var sharedFile = new SharedCredentialsFile();
if (!sharedFile.TryGetProfile(profileName, out var profile))
{
throw new ArgumentException($"AWS profile '{profileName}' was not found.");
}
if (!AWSCredentialsFactory.TryGetAWSCredentials(profile, sharedFile, out credentials))
{
throw new NotSupportedException($"Failed to get AWS credentials from profile named '{profileName}'.");
}
}
var s3Client = new AmazonS3Client(credentials, RegionEndpoint.USEast1);
var response = await s3Client.ListBucketsAsync();
Console.WriteLine($"Found {response.Buckets.Count} buckets.");
}
}
} Then, when I want to run it in docker, I use the environment variables instead of mounting my credentials files $accountId = aws configure get sso_account_id
$roleName = aws configure get sso_role_name
$accessToken = (Get-Content (dir $env:HOMEPATH\.aws\sso\cache -Exclude botocore*.json)[0] | ConvertFrom-Json).accessToken
$region = aws configure get region --profile dev
$roleCredentials = aws sso get-role-credentials --account-id $accountId --role-name $roleName --access-token $accessToken --region $region | ConvertFrom-Json
docker run --rm -it -v "$(Get-Location):/src" -e AWS_SESSION_TOKEN=$($roleCredentials.roleCredentials.sessionToken) -e AWS_ACCESS_KEY_ID=$($roleCredentials.roleCredentials.accessKeyId) -e AWS_SECRET_ACCESS_KEY=$($roleCredentials.roleCredentials.secretAccessKey) -e AWS_DEFAULT_REGION=$region -w /src mcr.microsoft.com/dotnet/core/sdk:3.1 dotnet run Then, the program runs successfully:
|
Hello there, |
|
Thanks a lot @CamileDahdah 👍 Do you know if nuget packages that include the fix are already available for consumption? |
No problem! Yes Nuget packages should be up-to-date by now. |
Describe the bug
I have the following code:
with the following csproj file:
Given that we are using AWS SSO, before executing this program I have to run
aws sso login
. Then, I can run the program successfully like this:I can also run this program in docker by mounting my AWS credentials file in the docker container:
So far, so good. I then modify my csproj file to use the latest AWS SDK. Here is how my csproj looks now:
With this change, I can still run the program successfully:
But then, if I run the program in docker in exactly the same way as before, I get an SSO error:
Expected Behavior
Running the program in docker with the latest SDK should not create any SSO error since I am already logged in. It should behave the same as the old SDK did.
Current Behavior
As shown in the error above, the latest version of the AWS SDK raises an error while the old SDK does not.
Reproduction Steps
See description above.
Possible Solution
Did not find any
Additional Information/Context
No response
AWS .NET SDK and/or Package version used
AWSSDK.S3 -> 3.7.101.10
AWSSDK.SSO -> 3.7.100.10
AWSSDK.SSOOIDC -> 3.7.100.10
Targeted .NET Platform
.NET Core 3.1
Operating System and version
Windows 11
The text was updated successfully, but these errors were encountered: