Created
August 25, 2016 18:27
-
-
Save tomislacker/a6d6f181a6df7d7a4606a905d017ad1e to your computer and use it in GitHub Desktop.
Find AWS IAM Account a Key Belongs To
This file contains hidden or 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 | |
| get_keys_for_user () | |
| { | |
| aws iam list-access-keys \ | |
| --user-name $@ \ | |
| | jq -r '.AccessKeyMetadata[].AccessKeyId' | |
| } | |
| get_all_users () | |
| { | |
| aws iam list-users \ | |
| | jq -r '.Users[].UserName' | |
| } | |
| get_username_for_key () | |
| { | |
| local this_key=$1 | |
| local found_user= | |
| while read username | |
| do | |
| if get_keys_for_user $username | grep -q $this_key | |
| then | |
| found_user=$username | |
| break | |
| fi | |
| done | |
| if [ -n "$found_user" ] | |
| then | |
| echo $found_user | |
| else | |
| return 1 | |
| fi | |
| } | |
| get_username_for_key $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment