Skip to content

Instantly share code, notes, and snippets.

@ashayh
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save ashayh/155294e58f9ae928a8a1 to your computer and use it in GitHub Desktop.

Select an option

Save ashayh/155294e58f9ae928a8a1 to your computer and use it in GitHub Desktop.
use ruby fog to search which users owns an IAM key
#!/usr/bin/env ruby
# If you have many IAM users, it's not easy to find which user
# owns a particular IAM key from the console.
# This script can be run with the first argument as the access key id.
require 'rubygems'
require 'ap' # gem install awesome_print
require 'fog' # gem install fog
# the aws_credentials var is not needed if you keep keys in ~/.fog file as yaml:
# http://fog.io/about/getting_started.html
# aws_credentials = {
# :aws_access_key_id => '',
# :aws_secret_access_key => ''
# }
#iam = Fog::AWS::IAM.new(aws_credentials)
iam = Fog::AWS::IAM.new # pick up vars from ~/.fog
users = iam.list_users.body["Users"]
puts "Searching for #{ARGV[0]} ..."
users.each do |user|
u = user["UserName"]
access_keys = iam.list_access_keys('UserName' => u).body["AccessKeys"]
access_keys.each do |key|
if key["AccessKeyId"] == ARGV[0]
ap iam.get_user(u).body
exit 0
end
end
end
# ToDo: list groups and maybe policies.
puts "Not found"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment