Skip to content

Instantly share code, notes, and snippets.

@nickrw
Created September 2, 2014 14:39
Show Gist options
  • Save nickrw/8059cb4475e89d3090cd to your computer and use it in GitHub Desktop.
Save nickrw/8059cb4475e89d3090cd to your computer and use it in GitHub Desktop.
Finds the IAM user that owns an AWS access key
#!/bin/sh
# Usage: ./find-access-key-by-id.sh <ACCESS KEY ID>
# Dependencies:
# * awscli (pip install awscli)
# * jq (https://github.com/stedolan/jq)
MYSTERY_KEY=$1
users=$(aws iam list-users | jq -r .Users[].UserName)
for iamuser in $users; do
match=$(aws iam list-access-keys --user-name $iamuser | \
jq -r ".AccessKeyMetadata[]
| select(.AccessKeyId == \"$MYSTERY_KEY\")
| .UserName + \": \" + .AccessKeyId + \" (\" + .Status + \")\""
)
if [ ! -z "$match" ]; then
echo "$match"
exit 0
fi
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment