Skip to content

Instantly share code, notes, and snippets.

@tonys-code-base
Last active October 16, 2022 06:15
Show Gist options
  • Save tonys-code-base/f8e7bdad64cdc27a364e7b1c3280a764 to your computer and use it in GitHub Desktop.
Save tonys-code-base/f8e7bdad64cdc27a364e7b1c3280a764 to your computer and use it in GitHub Desktop.
Refresh/Set credentials generated from aws-sts-assume-role-saml for AWS CLI profile
profilename=$1
principal_arn=$2
role_arn=$3
saml_assertion_path=$4
sts_credentials=$(aws sts assume-role-with-saml \
--principal-arn $principal_arn \
--role-arn $role_arn \
--saml-assertion \
file:\/\/$saml_assertion_path)
rc=$?
if [ $rc -ne 0 ]; then
echo "error code $rc"
exit $rc
fi
aws configure set aws_access_key_id \
$(echo $sts_credentials \
| jq -r .Credentials.AccessKeyId) \
--profile $profilename
rc=$?
if [ $rc -ne 0 ]; then
echo "error code $rc"
exit $rc
fi
aws configure set aws_secret_access_key \
$(echo $sts_credentials \
| jq -r .Credentials.SecretAccessKey) \
--profile $profilename
rc=$?
if [ $rc -ne 0 ]; then
echo "error code $rc"
exit $rc
fi
aws configure set aws_session_token \
$(echo $sts_credentials \
| jq -r .Credentials.SessionToken) \
--profile $profilename
rc=$?
if [ $rc -ne 0 ]; then
echo "error code $rc"
exit $rc
fi
aws configure set aws_session_expiration \
$(echo $sts_credentials \
| jq -r .Credentials.Expiration) \
--profile $profilename
rc=$?
if [ $rc -ne 0 ]; then
echo "error code $rc"
exit $rc
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment