Last active
July 10, 2017 04:51
-
-
Save chrisbrownie/c201189a9bd248dae11df3ed0976cf97 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# Number of days old an access key is allowed to be | |
ACCESS_KEY_MAX_AGE=90 | |
# Get every user's username | |
users=`aws iam list-users | jq -r '.Users[] | .UserName'` | |
# Get the oldest Created Date that we can allow (now minus ACCESS_KEY_MAX_AGE days) | |
created_date_limit=`date -d "-$ACCESS_KEY_MAX_AGE days" +%s` | |
for user in $users ; do | |
# Iterate through each user and retrieve their access keys | |
rawkeys=`aws iam list-access-keys --user-name $user` | |
keys=`echo $rawkeys | jq -r '.AccessKeyMetadata[] | .AccessKeyId'` | |
for key in $keys; do | |
thisKey=`echo $rawkeys | jq '.AccessKeyMetadata[] | select (.AccessKeyId=="'$key'")'` | |
# Convert the CreateDate property to epoch date | |
create_date=`echo $thisKey | jq -r '.CreateDate'` | |
create_date_epoch=`date -d $create_date +%s` | |
# If thet key was created BEFORE the limit (i.e. is less than), it's too old | |
if (( $create_date_epoch < $created_date_limit)); then | |
echo "$user:$key:old" | |
else | |
echo "$user:$key:good" | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment