Skip to content

Instantly share code, notes, and snippets.

@nicwaller
Last active March 11, 2016 23:22
Show Gist options
  • Save nicwaller/4c3d526fef2b11c55d64 to your computer and use it in GitHub Desktop.
Save nicwaller/4c3d526fef2b11c55d64 to your computer and use it in GitHub Desktop.
Find the Amazon IAM user associated with a given API Access Key
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