Last active
March 11, 2016 23:22
-
-
Save nicwaller/4c3d526fef2b11c55d64 to your computer and use it in GitHub Desktop.
Find the Amazon IAM user associated with a given API Access Key
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
function whohaskey { | |
APIKEY="$1" | |
CACHE_DIR=~/.aws/keycache | |
mkdir -p $CACHE_DIR | |
if [ -f $CACHE_DIR/$APIKEY ]; then | |
cat $CACHE_DIR/$APIKEY | |
return | |
fi | |
for USER in $(aws iam list-users | jq -r .Users[].UserName); do | |
JSON=$(aws iam list-access-keys --user-name "$USER") | |
KEYS=$(echo "$JSON" | jq -r .AccessKeyMetadata[].AccessKeyId) | |
for KEY in $KEYS; do | |
echo -n '.' | |
echo "$USER" > $CACHE_DIR/$KEY | |
if [ "$KEY" == "$APIKEY" ]; then | |
echo $USER | |
fi | |
done | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment