Last active
December 21, 2017 19:19
-
-
Save gjyoung1974/b00fbbc7fa357e815bc29ec2511455b8 to your computer and use it in GitHub Desktop.
aws_iam_access_key_age.py
This file contains hidden or 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 | |
import datetime | |
import time | |
# AWS Client>> | |
profile = 'some-profile' | |
boto3.setup_default_session(profile_name=profile) | |
client = boto3.client('iam') | |
# Array of users | |
response = client.list_users() # List all the users | |
for user in response["Users"]: | |
username = user['UserName'] | |
# Array of access keys for 1 user | |
res = client.list_access_keys(UserName=username) # for each user get the each access key | |
for key in res['AccessKeyMetadata']: | |
# Format the dates | |
accesskeydate = key['CreateDate'] | |
accesskeydate = accesskeydate.strftime("%Y-%m-%d %H:%M:%S") | |
currentdate = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) | |
accesskeyd = time.mktime(datetime.datetime.strptime(accesskeydate, "%Y-%m-%d %H:%M:%S").timetuple()) | |
currentd = time.mktime(datetime.datetime.strptime(currentdate, "%Y-%m-%d %H:%M:%S").timetuple()) | |
active_days = (currentd - accesskeyd) / 60 / 60 / 24 # We get the data in seconds and convert it to days | |
if active_days >= 90: | |
if key['Status'] == 'Active': | |
print key['UserName'] + ', ' + key['AccessKeyId'] + ", " + str((int(round(active_days)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment