I'm trying to determine the age of AWS keys in days. I'm using Bash.
I get the creation date of the AWS key using this command:
user_key1_date=$(aws iam list-access-keys --user-name "$aws_user_name" --profile "$aws_key" --output text --query 'AccessKeyMetadata[*].[AccessKeyId,CreateDate]' | awk 'NR==1 { print $2 }')
And I get a result like this:
2018-01-04T20:59:01Z
I am then trying to get the age of the keys in days and assign it to a variable with this line:
key1Age=$(date -d "$user_key1_date" +%j)
I then try to report the age of the key with this line:
The AWS access key: $user_access_key1 for user name: $aws_user_name was created on $date1. This key is $key1Age days old and needs to be replaced.
But the age of the key that gets reported is innacurate:
Key AKIAIPSNLEFKBLH2CLOQ is 004 days old. Time to change your key!
What am I doing wrong?