Repository navigation
Description
My team has set up AWS SSO and is starting to use aws sso login for most of their needs. Everything is working smoothly. However, it appears that AWS SDKs (e.g. Golang apps that call AWS APIs) do not support reading the temporary SSO credentials stored in ~/.aws/cli/cache/~/.aws/sso/cache. (See aws/aws-sdk-go#3186)
And as a result, my team is blocked from adopting AWS SSO because it only works with the AWS CLI but none one our existing tools.
Ideally, the various language-specific AWS SDKs would be able to pick up on the current AWS SSO credentials seamlessly. Honoring AWS_PROFILE or AWS_DEFAULT_PROFILE environment variables, and using the same credential lookup algorithm as the CLI. (I assume that's the on the roadmap eventually.)
However, getting that change fixed across all of the AWS SDKs, as well as them upstreamed into tools that rely on them, will take a long time. And it would be nice to unblock my team until then so they can just rely on aws sso login.
I can see the credential files on my local disk, e.g. ~/.aws/sso/cache/61368d38a2497e42a24a243072108001849d0b07.json. But it isn't clear how to map the current set of environment variables to which JSON file to load.
Could the CLI support some way of returning whatever the credentials it is using? e.g.
# Return the SSO credentials file for the provided profile, or just use AWS_PROFILE, etc.
aws --profile staging-environment \
sts get-caller-credentials
# Or even better: write the SSO credentials to ~/.aws/credentials. So other tools
# could just read from it.
aws sso write-credentials-to-disk --profile staging-environment
I don't know if there is a better approach here, as I'm do not know the specific differences between credential resolution in the AWS CLI vs. AWS SDKs. But hopefully there is some sort of workaround to make this scenario work?
Activity
maestretti commentedon Jun 10, 2020
This is an important feature, please prioritize it. Many of the AWS SDKs do not work with SSO forcing a workaround. Most SDKs do support external credential_process handlers via configuration profile. It would be great if
aws ssocould output credentials in the supported format as a one liner. This would eliminate the need for a number of third-party tools that work around this, and the many AWS customers that are rewriting those tools so as not to expose their credentials.aws sso get-profile-credentials --profile profileNamecould get the token from cache and return the STS creds for the given profile in the JSON format for credential_process.Format for credential helpers to export: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sourcing-external.html
Example of a 3rd party tool, also has a list of other tools on the README: https://github.com/victorskl/yawsso
aleksandrzhiliaev commentedon Aug 13, 2020
Are there any updates on this feature request?
ldormoy commentedon Sep 4, 2020
In the meantime, aws-vault v6+ is a nice solution to circumvent this issue in your local environment.
See 99designs/aws-vault#549
ryansonshine commentedon Oct 10, 2020
I've created an npm package for updating the credentials from the command line for any users out there running node https://github.com/ryansonshine/aws-sso-creds-helper
kdaily commentedon Oct 19, 2020
Hi @chrsmith,
Thanks for the request and sorry for the delay. This is also related to #4982. Marking as an SSO feature request.
benkehoe commentedon Nov 16, 2020
I've also written a utility in Python that supports AWS SSO credentials. https://github.com/benkehoe/aws-export-credentials
m17kea commentedon May 18, 2021
On the CDK repo they recommend the following wrapper: https://pypi.org/project/aws2-wrap/
m4dc4p commentedon Oct 5, 2021
Please, please fix this.
aws ssoneeds to write~/.aws/credentials.3 remaining items
TechIsCool commentedon May 5, 2022
Since my team and I needed this sooner than the merged PR, which I am glad to see in motion.
Here is my shell function
datfinesoul commentedon Jun 24, 2022
Thanks @TechIsCool, that function was extremely helpful. I had to make just a few changes to make it work for me, since I swap between SSO orgs, and some profiles didn't have regions, so I defaulted
us-east-1in my case.Edited: Made my first version OSX compatible
ebolingerjc commentedon Aug 2, 2022
Based on the examples above, my version initializes AWS env vars using .aws/sso & .aws/cli caches. It requires good profiles defined in .aws/config file. Compatible with both Zsh and Bash.
Source this file from your login profile (.zprofile or .bash_profile) or directly from the shell:
source ~/setup-awsSwitch between profiles by exporting AWS_PROFILE with a new value & source this script again.
export AWS_PROFILE=staging-envsource ~/setup-awsSave that as
~/setup-awsor wherever you like.jsifuentes commentedon Aug 2, 2022
Love all the script sharing here. I used @datfinesoul's as a base, but I wanted to have more confidence finding the SSO cache file instead of using the first found token. The SSO cache file name is a
sha1based on thesso_start_url, so we can just do the same thing in our script. https://github.com/boto/botocore/blob/b006ff741d12608a9187b873e276abd1fd8eb707/botocore/utils.py#L2364-L2365This does not automatically set the environment variables. It just outputs the export statements.
vnagendra commentedon Aug 22, 2022
@jsifuentes - I am not sure if this is right..
The code you link to is a great find, I don't want to take away from that. But shouldn't you be doing
utf-8encoding before looking up the file based on SHA1? I have absolutely no idea how to do this in bash... 😂Anyway, I tried the following...
echowhatever I had in the profile (ex:echo https://mydomain.awsapps.com/start)sha1sumEx:echo https://mydomain.awsapps.com/start | sha1sum~/.aws/sso/cachedirectoryOnce I find a solution for this, I'll post here. But you can verify the results fairly easily using this recipe
https://gchq.github.io/CyberChef/#recipe=Encode_text('UTF-8%20(65001)')SHA1(80)&input=aHR0cHM6Ly9teWRvbWFpbi5hd3NhcHBzLmNvbS9zdGFydA
jsifuentes commentedon Aug 22, 2022
Hi @vnagendra , I'm not sure I understand your question about the UTF-8 encoding. I don't think it's applicable in this case. Make sure you don't forget to include
-nin yourechocommand. The-nis important because it removes the\nthatechoautomatically includes at the end, so if you're excluding-n, it will lead to a different sha1 sum.Hope that helps.
vnagendra commentedon Aug 24, 2022
@jsifuentes, Thanks for your help! Using your script as a base, this is what I did.
My specific use case was there were a couple of Terraform modules. I wanted to upgrade them one by one to the latest version of Terraform. The latest version supports AWS SSO credentials, the older versions don't. I've always used direnv for managing a bunch of environment variables. So I used the following script inside the
.envrcof the module that was not upgraded. Meaning if you have the newer Terraform (and provider) version, you do not need all this circus.Please note, if you use this trick -- you must have the "function aws_sso()" declared inside that specific
.envrc(inside the module that is not upgraded). In my case thesource_upwas pointing to another.envrc(in the parent directory of course) which was just exporting the followingWith this setup, it was fairly simple..
Add export-creds command to the CLI
Add export-creds command to the CLI
drewwells commentedon Nov 8, 2022
The code examples above work by mutating the AWS environment variables. This can be problematic if you're using multiple environments. I altered a previous script to instead dump credentials into the credentials file. Now I can sso into multiple profiles
I use this to run it
aws_creds | xargs -d\\n -n1 sh -ctim-finnigan commentedon Feb 24, 2023
I think this can now be closed as #7398 was merged. Please refer to this documentation for
aws configure export-credentials: https://awscli.amazonaws.com/v2/documentation/api/latest/reference/configure/export-credentials.htmlgithub-actions commentedon Feb 24, 2023
Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.
apogrebnyak commentedon Sep 28, 2023
For AWS cli > 2.13.5 which uses sso-session sections in configuration file, you need to change the SHA1 source to
sso_sessionvalue in your profile