-
-
Save laidbackware/28adc44ae41138970b774fb5df38e124 to your computer and use it in GitHub Desktop.
Test IAM access to AWS s3 bucket from EC2
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
#!/bin/bash | |
instance_profile=`curl http://169.254.169.254/latest/meta-data/iam/security-credentials/` | |
aws_token_response=`curl --silent http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile}` | |
aws_access_key_id=`echo "${aws_token_response}" | grep AccessKeyId | cut -d':' -f2 | sed 's/[^0-9A-Z]*//g'` | |
aws_secret_access_key=`echo "${aws_token_response}" | grep SecretAccessKey | cut -d':' -f2 | sed 's/[^0-9A-Za-z/+=]*//g'` | |
token=`echo "${aws_token_response}" | sed -n '/Token/{p;}' | cut -f4 -d'"'` | |
echo "${aws_token_response}" | sed -n '/Expiration/{p;}' | |
file=$2 | |
bucket=$1 | |
region=$3 | |
date="`date +'%a, %d %b %Y %H:%M:%S %z'`" | |
resource="/${bucket}/${file}" | |
signature_string="GET\n\n\n${date}\nx-amz-security-token:${token}\n/${resource}" | |
signature=`/bin/echo -en "${signature_string}" | openssl sha1 -hmac ${aws_secret_access_key} -binary | base64` | |
authorization="AWS ${aws_access_key_id}:${signature}" | |
curl -s -H "Date: ${date}" -H "X-AMZ-Security-Token: ${token}" -H "Authorization: ${authorization}" "https://s3-${region}.amazonaws.com/${resource}" -o "${file}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment