Created
April 10, 2019 14:54
-
-
Save jamesthomasonjr/cb7f6b5f15d23329e42d76e2bb9e284d to your computer and use it in GitHub Desktop.
SAML to AWS STS Keys helper function for Fish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env fish | |
function _awsup_check_signin --argument-names 'credentials_file' | |
set --erase --universal AWS_ACCESS_KEY_ID | |
set --erase --universal AWS_SECRET_ACCESS_KEY | |
set --export --universal AWS_SHARED_CREDENTIALS_FILE "$credentials_file" | |
set --local account_name (aws iam list-account-aliases --output text --query 'AccountAliases[0]' 2>/dev/null) | |
if test -z "$account_name" | |
exit 0 | |
end | |
set --local account_id (aws sts get-caller-identity --output text --query 'Account' 2>/dev/null) | |
set --local aws_user (aws sts get-caller-identity --output text --query 'Arn' 2>/dev/null) | |
echo "Signed in to \"$account_name ($account_id)\" as \"$aws_user\"" | |
end | |
function awsup | |
set --local chrome_download_path "$HOME/Downloads" | |
set --local plugin_download_file "$chrome_download_path/credentials" | |
set --local target_cred_file "$HOME/.aws/aws_sts_credentials" | |
set --local cred_file_Export "" | |
if test -f "$plugin_download_file" | |
echo "New credential file found: \"$plugin_download_file\"" | |
echo "Moving to \"$target_cred_file\"" | |
mv "$plugin_download_file" "$target_cred_file" | |
set --local cred_file_export 'set --export AWS_SHARED_CREDENTIALS_FILE="'$target_cred_file'"' | |
# Double check that the current credentials are still valid | |
set --local signedin (_awsup_check_signin "$target_cred_file") | |
echo "$signedin" | |
echo | |
else | |
echo "No new credential file found: \"$plugin_download_file\"" | |
# Double check that the current credentials are still valid | |
set --local signedin (_awsup_check_signin "$target_cred_file") | |
if test -n "$signedin" | |
set --export --universal AWS_SHARED_CREDENTIALS_FILE "'$target_cred_file'" | |
echo "$signedin" | |
echo | |
else | |
echo "Resetting back to defaults" | |
echo | |
if test -f "$target_cred_file" | |
rm "$target_cred_file" | |
end | |
set --erase --universal AWS_SHARED_CREDENTIALS_FILE | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment