Add support for connecting to AWS S3 with IAM Roles that use IMDSv2 (6 hours server session keys)
Requesting adding IAM-Role with IMDSv2 support for WinSCP to retrieve the AWS AccessKeyID and SecretAccessKey automatically when accessing S3 buckets. Such as adding a check box to try connecting with S3 with IAM-Role with IMDS-v2 support, or automatically detecting iam-role keys, or trying, etc.
Related issue: Issue 2089 – Allow S3 connection with IAM role instead of credentials
However, when instead using IMDSv2 with temporary 6 hour session keys (that is the server you're RDP into has an assigned IAM role to it with S3 permissions to your bucket), it would be beneficial if WinSCP would instead automatically manage the retrieving these keys.
As an example in PowerShell, here's the code to retrieve the temporary session credentials for connecting to S3 bucket.
Related issue: Issue 2089 – Allow S3 connection with IAM role instead of credentials
However, when instead using IMDSv2 with temporary 6 hour session keys (that is the server you're RDP into has an assigned IAM role to it with S3 permissions to your bucket), it would be beneficial if WinSCP would instead automatically manage the retrieving these keys.
As an example in PowerShell, here's the code to retrieve the temporary session credentials for connecting to S3 bucket.
# the below command will show the last 4 characters of the access_key and secret_key for the iam-role. It also includes profile (if set), and AWS region. aws configure list # To get the full iam-role access_key and secret_key the below powershell code works: # Step 1: Get IMDSv2 token $token = (Invoke-WebRequest -Uri "http://169.254.169.254/latest/api/token" -Method PUT -Headers @{ "X-aws-ec2-metadata-token-ttl-seconds" = "21600" }).Content # Step 2: Get IAM role name $roleName = (Invoke-WebRequest -Uri "http://169.254.169.254/latest/meta-data/iam/security-credentials/" -Headers @{ "X-aws-ec2-metadata-token" = $token }).Content # Step 3: Get temporary credentials $credentials = (Invoke-WebRequest -Uri "http://169.254.169.254/latest/meta-data/iam/security-credentials/$roleName" -Headers @{ "X-aws-ec2-metadata-token" = $token }).Content | ConvertFrom-Json # Output $credentials # In which $credentials will contain like below (creds redacted of course) $credentials | gm TypeName: System.Management.Automation.PSCustomObject Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() AccessKeyId NoteProperty string AccessKeyId=***** ToString Method string ToString() Code NoteProperty string Code=Success Expiration NoteProperty string Expiration=2025-01-23T00:30:21Z LastUpdated NoteProperty string LastUpdated=2025-01-22T18:19:59Z SecretAccessKey NoteProperty string SecretAccessKey=***** Token NoteProperty string Token=***** Type NoteProperty string Type=AWS-HMAC # I haven't tested below, but it looks like it may work in powershell by setting $credentials to the below environmental variables. Also could set a profile, etc. $env:AWS_ACCESS_KEY_ID = $credentials.AccessKeyId $env:AWS_SECRET_ACCESS_KEY = $credentials.SecretAccessKey $env:AWS_SESSION_TOKEN = $credentials.Token # Verify aws sts get-caller-identity