Last active
February 13, 2016 00:36
-
-
Save adamjkeller/df32e53e1458db02a606 to your computer and use it in GitHub Desktop.
AWS IAM - Find users and determine last time key(s) were used (if ever)
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
#!/usr/bin/env python | |
import boto.iam as a | |
import boto3 | |
class UserInfo(object): | |
def __init__(self, region = 'us-east-1'): | |
self.region = region | |
self.user_map = {} | |
self.print_map = {} | |
self.conn = a.connect_to_region(self.region) | |
self.client = boto3.client('iam') | |
def get_user_map(self): | |
users = [x for x in self.conn.get_all_users()['list_users_response']['list_users_result']['users']] | |
for user in users: | |
self.user_map[user['user_name']] = '' | |
def get_user_keys(self): | |
for userName, userID in self.user_map.iteritems(): | |
key_data = self.conn.get_all_access_keys(userName)['list_access_keys_response']['list_access_keys_result']['access_key_metadata'] | |
if len(key_data) > 1: | |
self.user_map[userName] = [] | |
for access_key in key_data: | |
self.user_map[userName].append(access_key['access_key_id']) | |
elif len(key_data) == 1: | |
self.user_map[userName] = key_data[0]['access_key_id'] | |
else: | |
print "User {0} does not have an access key".format(userName) | |
def check_user_key(self): | |
for userName, userID in self.user_map.iteritems(): | |
if isinstance(userID, list): #The idea here is that if the value is a list, we need to iterate | |
for uID in userID: | |
user = self.client.get_access_key_last_used(AccessKeyId=uID) | |
try: | |
print "{0}: {1}".format(userName, user['AccessKeyLastUsed']['LastUsedDate']) | |
except: | |
print "{0} has not used their access key: {1}".format(userName,uID) | |
pass | |
elif userID: | |
user = self.client.get_access_key_last_used(AccessKeyId=userID) | |
try: | |
if user['AccessKeyLastUsed']['LastUsedDate']: | |
print "{0}: {1}".format(userName, user['AccessKeyLastUsed']['LastUsedDate']) | |
except: | |
print "{0} has not used their access key".format(userName) | |
pass | |
def run(self): | |
self.get_user_map() | |
self.get_user_keys() | |
self.check_user_key() | |
if __name__ == '__main__': | |
userObj = UserInfo() | |
userObj.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Disclaimer: This is ugly and was thrown together in 20 minutes.