Created
January 25, 2017 11:57
-
-
Save th3james/29fab3e67cadf89a252f17a55ec8642c to your computer and use it in GitHub Desktop.
Find AWS user by 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
require 'json' | |
key_to_find = /.*thing-or-whatever.*/ | |
users = JSON.parse(`aws iam list-users`).fetch('Users') | |
usernames = users.map { |user| | |
user.fetch('UserName') | |
} | |
user_secret_keys = Hash[ | |
usernames.map { |username| | |
secret_key_output = `aws iam list-access-keys --user #{username}` | |
secret_keys = JSON.parse(secret_key_output).fetch('AccessKeyMetadata') | |
[username, secret_keys.map { |sk| sk.fetch('AccessKeyId') }] | |
} | |
] | |
matching_users = user_secret_keys.select { |user, keys| | |
keys.any? { |key| | |
key_to_find.match(key) | |
} | |
} | |
matching_users.each do |username, keys| | |
puts username | |
matching_keys = keys.select { |key| | |
key_to_find.match(key) | |
} | |
puts "\t#{matching_keys}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment