Last active
November 25, 2022 06:52
-
-
Save xxxVxxx/9648264fd6f41bbb1f65 to your computer and use it in GitHub Desktop.
boto3 aws find all IAM accesskeys details for the account
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
import boto3 | |
boto3.setup_default_session(profile_name='IAM') | |
resource = boto3.resource('iam') | |
client = boto3.client("iam") | |
KEY = 'LastUsedDate' | |
for user in resource.users.all(): | |
Metadata = client.list_access_keys(UserName=user.user_name) | |
if Metadata['AccessKeyMetadata'] : | |
for key in user.access_keys.all(): | |
AccessId = key.access_key_id | |
Status = key.status | |
LastUsed = client.get_access_key_last_used(AccessKeyId=AccessId) | |
if (Status == "Active"): | |
if KEY in LastUsed['AccessKeyLastUsed']: | |
print "User: " , user.user_name , "Key: " , AccessId , "AK Last Used: " , LastUsed['AccessKeyLastUsed'][KEY] | |
else: | |
print "User: ", user.user_name , "Key: ", AccessId , "Key is Active but NEVER USED" | |
else: | |
print "User: ", user.user_name , "Key: ", AccessId , "Keys is InActive" | |
else: | |
print "User: ", user.user_name , "No KEYS for this USER" #".. proof: " , Metadata |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AttributeError: 'IAM' object has no attribute 'resource'