I have a Ruby application using the AWS SDK Ruby v3, and recent I’ve added support for using SSO profiles instead of static "key ID + secret" configuration.
The new setup works well for a while, until the token "expires" and I start getting Aws::Errors::InvalidSSOToken
exceptions, at which point the user needs to manually run the CLI’s aws sso login
to get a browser login screen.
I would have liked to skip the manual AWS CLI running step – if the SDK can directly do the aws sso login
step (with the correct profile).
I can probably do exec
with the correct arguments – but I would like to do it "the SDK way".
2
Answers
Work In Progress Sample Code
This is not a complete implementation but a bit of scratch pad for tests that I got to work. There's still a lot more work to get it to a point where it is usable.
Kudus to @tsal-troser that, at his own answer, pointed at the Python example that was used to create this sample code, and @2ps that wrote that Python code.
This is yet incomplete, I hope to get back to it and finish the code next week. 🤞
Yes, you can. You can add an error handler when you get
InvalidSSOToken
then do an SSO authentication.I’ve never tried the SDK way. I’ve tried the
exec
cli commandaws sso login
(because it’s easier). It will create acache.json
file with thetoken
andexpiresAt
values.This is the project I used as a reference: https://github.com/NeilJed/aws-sso-credentials/blob/master/awssso
Here’s the part you can use as reference for checking if the token is expired. https://github.com/NeilJed/aws-sso-credentials/blob/master/awssso#L110-L137
Here’s another example of doing it via SDK but in Python:
https://stackoverflow.com/a/71850591/22277802