@@ -24,27 +24,21 @@ CHECK_DOC_extra758='https://docs.aws.amazon.com/systems-manager/latest/userguide
24
24
CHECK_CAF_EPIC_extra758=' Infrastructure Security'
25
25
26
26
extra758 (){
27
+ # OLDAGE has the following format: YYYY-MM-DD
27
28
OLDAGE=" $( get_date_previous_than_months 12) "
28
- for regx in $REGIONS ; do
29
- EC2_RUNNING =$( $ AWSCLI ec2 describe-instances --query " Reservations[* ].Instances[*] .[InstanceId]" $PROFILE_OPT --region $ regx --output text 2>&1 )
30
- if [[ $( echo " $EC2_RUNNING " | grep -E ' AccessDenied|UnauthorizedOperation|AuthorizationError' ) ]]; then
31
- textInfo " $regx : Access Denied trying to describe instances" " $regx "
29
+ for regx in ${ REGIONS} ; do
30
+ INSTACES_OLD_THAN_AGE =$( " ${ AWSCLI} " ec2 describe-instances --query " Reservations[].Instances[?LaunchTime<=' ${OLDAGE} '][] .[InstanceId, LaunchTime, State.Name ]" ${ PROFILE_OPT} --region " ${ regx} " --output text 2>&1 )
31
+ if [[ $( echo " ${INSTACES_OLD_THAN_AGE} " | grep -E ' AccessDenied|UnauthorizedOperation|AuthorizationError' ) ]]; then
32
+ textInfo " ${ regx} : Access Denied trying to describe instances" " ${ regx} "
32
33
continue
33
34
fi
34
- if [[ $EC2_RUNNING ]]; then
35
- INSTACES_OLD_THAN_AGE=$( $AWSCLI ec2 describe-instances --query " Reservations[].Instances[?LaunchTime<='$OLDAGE '][].{id: InstanceId, launched: LaunchTime}" $PROFILE_OPT --region $regx --output text)
36
- if [[ $INSTACES_OLD_THAN_AGE ]]; then
37
- while IFS= read -r ec2_instace
38
- do
39
- EC2_ID=$( echo " $ec2_instace " | awk ' {print $1}' )
40
- LAUNCH_DATE=$( echo " $ec2_instace " | awk ' {print $2}' )
41
- textFail " $regx : EC2 Instance $EC2_ID running before than $OLDAGE " " $regx " " $EC2_ID "
42
- done <<< " $INSTACES_OLD_THAN_AGE"
43
- else
44
- textPass " $regx : All Instances newer than 12 months" " $regx "
45
- fi
35
+ if [[ " ${INSTACES_OLD_THAN_AGE} " ]]; then
36
+ while read -r EC2_ID LAUNCH_DATE STATE
37
+ do
38
+ textFail " ${regx} : EC2 Instance ${EC2_ID} launched before than ${OLDAGE} . Launch date: ${LAUNCH_DATE} - State: ${STATE} " " ${regx} " " ${EC2_ID} "
39
+ done <<< " ${INSTACES_OLD_THAN_AGE}"
46
40
else
47
- textInfo " $regx : No EC2 Instances Found " " $regx "
41
+ textPass " ${ regx} : No EC2 Instances found older than 12 months " " ${ regx} "
48
42
fi
49
43
done
50
44
}
0 commit comments