Skip to content

Instantly share code, notes, and snippets.

@th3james
Created January 25, 2017 11:57
Show Gist options
  • Save th3james/29fab3e67cadf89a252f17a55ec8642c to your computer and use it in GitHub Desktop.
Save th3james/29fab3e67cadf89a252f17a55ec8642c to your computer and use it in GitHub Desktop.
Find AWS user by access key
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