aws-cli icon indicating copy to clipboard operation
aws-cli copied to clipboard

[v2] credentials supplied by aws sso login do not conform to AWS standards

Open dayer4b opened this issue 6 years ago • 33 comments

AWS documents that credentials generated by aws configure are stored in the standard path ~/.aws/credentials:

The AWS CLI stores the credentials that you specify with aws configure in a local file named credentials, in a folder named .aws in your home directory.

https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html

This path is not utilized by credentials stored by aws configure sso or aws sso login.

Other tools (like Terraform and Boto3) that rely on this standard do not function because they do not see the credentials stored in the ~/.aws/cli/cache/ JSON files.

Shared credential file (~/.aws/credentials)

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html

dayer4b avatar Feb 19 '20 21:02 dayer4b

For my own purposes, I am working around this issue by extracting the data from the JSON object and dumping it into the ~/.aws/credentials file as such:

#!/bin/bash

JSON_BASEPATH="${HOME}/.aws/cli/cache"
AWS_CREDENTIALS_PATH="${HOME}/.aws/credentials"

if [ -f ${AWS_CREDENTIALS_PATH} ]; then
        echo "backing up existing credentials"
	cp -rf ${AWS_CREDENTIALS_PATH} "${AWS_CREDENTIALS_PATH}-"$(date +"%s")
fi

# find the latest CLI JSON file

json_file=$(ls -tr "${JSON_BASEPATH}" | tail -n1)

# use jq to dump stuff in the right place

aws_access_key_id=$(cat ${JSON_BASEPATH}/${json_file} | jq -r '.Credentials.AccessKeyId')
aws_secret_access_key=$(cat ${JSON_BASEPATH}/${json_file} | jq -r '.Credentials.SecretAccessKey')
aws_session_token=$(cat ${JSON_BASEPATH}/${json_file} | jq -r '.Credentials.SessionToken')


echo "[default]" > ${AWS_CREDENTIALS_PATH}

echo "aws_access_key_id = ${aws_access_key_id}" >> ${AWS_CREDENTIALS_PATH}
echo "aws_secret_access_key = ${aws_secret_access_key}" >> ${AWS_CREDENTIALS_PATH}
echo "aws_session_token = ${aws_session_token}" >> ${AWS_CREDENTIALS_PATH}

I hope this helps others until a fix is ready!

dayer4b avatar Feb 19 '20 21:02 dayer4b

Hi @dayer4b , To make sure I understand the problem here, you've ran the configure sso command and logged in and are able to use the various services with the generated temporary credentials right?

aws sso login --profile my-sso-profile
aws s3 ls --profile my-sso-profile

this kind of call works for you, but when running through another sdk, it is not recognizing the credentials. Am I understanding correctly?

KaibaLopez avatar Feb 22 '20 00:02 KaibaLopez

@KaibaLopez that is the current best solutions I think we agree. Those STS creds are valid only for an hour so you have to generate those quite often.

This is not a good solutions. SSO does not always create new cache json files so you actually need to clear cache folder beforehand to be sure that the newest file is created by AWS cli.

heikkis avatar Mar 05 '20 12:03 heikkis

I think I'm missing something here, but this is not really a problem on the CLI, unless it is not working for you for cli actions either it'd be a problem with how you're getting credentials on the other sdks. You're right that you shouldn't need to move them manually, but each sdk has it's own way of retrieving credentials when it comes to sso logins, make sure that you're doing it correctly, it might need extra configuration.

Also, not sure if this was part of the problem but sso is meant to be temporary so not sure I can offer you a solution there. Again let me know if I missed something here or you don't agree with my response, I do feel like I'm not getting the full picture...

KaibaLopez avatar Mar 05 '20 22:03 KaibaLopez

I think you are correct in away. I think that it would be nice that cli would provide supported way for SSO STS creds like you can get those from the portal. Now this "hook" is way too complex and error brone.

Support could be very similar to STS lookup with MFA code.

heikkis avatar Mar 06 '20 06:03 heikkis

I think the problem OP is describing is really a feature request for the aws terraform provider. I found a feature request over on the provider repo related to supporting SSO credentials: https://github.com/terraform-providers/terraform-provider-aws/issues/10851

rossmckelvie avatar Mar 06 '20 17:03 rossmckelvie

Doing this on a utility-by-utility or sdk-by-sdk basis will make for an absolutely horrid developer and user experience.

The SDKs all support the AWS Shared Config file in some way. The SSO auth/cred process should also integrate through the AWS Shared Config so all SDKs and all utilities are able to leverage it the same way.

lorengordon avatar Mar 06 '20 18:03 lorengordon

@lorengordon Yes,and we are trying to unify the sdk's behaviors, but I think you misunderstood what I meant there, it's not as bad as that. It's more on the default credentials provider chain, I'm not familiar with how terraform does theirs, but they should have a way to read the credentials from the same place as the cli, it just may not be the default, that's what I meant by extra steps, not that you'd need to modify the code but that you'd need a different method call. All this being said, I'd recommend checking those sdk's documentation or opening an issue on their repos regarding this, it still sounds to me like there is no problem with the CLI's implementation.

KaibaLopez avatar Mar 06 '20 20:03 KaibaLopez

@KaibaLopez Ok, gotcha. I use terraform a lot and have also helped review/expand the credential handling to better work with the features of the AWS Shared Config. It will indeed leverage the SDK credential chain properly. The only known real issue currently is that IF you are using the AWS_PROFILE env and that profile uses certain features from the AWS Shared Config file, then you must export AWS_SDK_LOAD_CONFIG=1. That's kind of a thing in the aws-sdk-go for enabling resolution for the AWS Shared Config.

Far as I can tell, I don't think there is truly a terraform issue here.

lorengordon avatar Mar 06 '20 20:03 lorengordon

I would have to say I am experiencing the same "issue" as the OP.

I am using boto3 and it is expecting the AWS CLI V1 shared credentials file in INI format. https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html

As for a feature request I think it would be helpful to be able to choose if you also want the temporary credentials stored in a "legacy" format.

I am currently seeing if I can make a wrapper script similar to the OP that exports the access key, secret key, and session token.

taingalls avatar Mar 25 '20 00:03 taingalls

I have created and shared a script to simplify updating ~/.aws/credentials for AWS SSO users. It will update the AWS credentials file by adding/updating the specified profile credentials using the AWS CLI v2 cached SSO login.

sgtoj avatar Apr 12 '20 11:04 sgtoj

I pulled the SSO credential support from botocore v2 and the CLI into a credential_process helper, so by adding one line to your SSO-configured profile in .aws/config, it'll work with any SDK that supports credential_process (or any tool that uses one of those SDKs, like Terraform): aws-sso-credential-process.

@flyinprogrammer has written a similar thing in go that's a little less full-featured, but has the advantage of being a single executable: aws-sso-fetcher

benkehoe avatar May 27 '20 22:05 benkehoe

Well, come here for some quick ready made solution. Ended up writing one, yawsso -- which I extend @sgtoj work, without depending on anything except CLI v2 itself, to sync up CLI v2 SSO login session to v1 credentials file.

pip install yawsso
aws configure sso
aws sso login --profile=dev
yawsso -p dev

victorskl avatar May 29 '20 08:05 victorskl

Sorry I didn't reply to these messages sooner, but I think most of the commenters here are understanding that pain that I have described. My concern with the proposed solution is that it effectively breaks the document contract that the AWS CLI has established and fostered for years.

This is particularly problematic with boto3 as this is literally managed by Amazon. I pointed to boto3 and terraform because these are very commonly used and one of them is managed by Amazon.

In the meantime, I have entirely given up any attempt at using aws configure from the command line, instead I have been manually copying credentials from the SSO web page and placing them somewhere that command line tools understand.

@victorskl I look forward to trying out your tool!

dayer4b avatar Jun 05 '20 21:06 dayer4b

Sorry I didn't reply to these messages sooner, but I think most of the commenters here are understanding that pain that I have described. My concern with the proposed solution is that it effectively breaks the document contract that the AWS CLI has established and fostered for years.

This is particularly problematic with boto3 as this is literally managed by Amazon. I pointed to boto3 and terraform because these are very commonly used and one of them is managed by Amazon.

In the meantime, I have entirely given up any attempt at using aws configure from the command line, instead I have been manually copying credentials from the SSO web page and placing them somewhere that command line tools understand.

@victorskl I look forward to trying out your tool!

I've moved (back) to aws-vault https://github.com/99designs/aws-vault/blob/master/USAGE.md#aws-single-sign-on-aws-sso it covers all my needs, and is far more secure than aws cli leaving creds in clear text on disk

FernandoMiguel avatar Jun 06 '20 14:06 FernandoMiguel

Note for aws-vault you need to get a v6 pre-release binary to use the SSO functionality. This is what has allowed us to roll out SSO to our org's backend developers

rossmckelvie avatar Jun 09 '20 14:06 rossmckelvie

For anyone looking for a node version of this type of script I've published this one to npm https://github.com/ryansonshine/aws-sso-creds-helper

ryansonshine avatar Jul 28 '20 00:07 ryansonshine

For folks still needing backward compatibility to ~/.aws/credentials file, I have created a script to automate the web flow of 'aws sso login', so you do not need to switch to the browser for SSO authentication, update the ~/.aws/credentials and inject env params AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN into current terminal.

https://github.com/arvindkgs/aws_sso_login

arvindkgs avatar Feb 19 '21 09:02 arvindkgs

Please note that @arvindkgs's tool requires you to put your password in an environment variable or in your shell history, both of which are run against recommended practice. The process of using your browser to sign in is a security feature of AWS SSO login, not a bug. It also only appears to work with Okta, and I see no support for MFA.

benkehoe avatar Feb 25 '21 15:02 benkehoe

@benkehoe I built this for developers who don't want to shift focus from their cmd line. While this is a feature of SSO it is distracting for me. Yes, it is only for OKTA now. I added to environment because some tools require creds in the environment. I will add a switch to make this optional. I have explored other tools like https://github.com/Noovolari/leapp and https://github.com/linaro-its/aws2-wrap and found them lacking.

arvindkgs avatar Feb 25 '21 16:02 arvindkgs