Skip to content

Commit 1fa62cf

Browse files
lazizejfagoagas
andauthored
fix(extra758): Reduce API calls. Print correct instance state. (prowler-cloud#1057)
* fix(extra758): Reduce API calls. Print correct instance state. * feat(oldage-format): Include comment Co-authored-by: Pepe Fagoaga <[email protected]>
1 parent d8d2ddd commit 1fa62cf

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

checks/check_extra758

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,21 @@ CHECK_DOC_extra758='https://docs.aws.amazon.com/systems-manager/latest/userguide
2424
CHECK_CAF_EPIC_extra758='Infrastructure Security'
2525

2626
extra758(){
27+
# OLDAGE has the following format: YYYY-MM-DD
2728
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}"
3233
continue
3334
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}"
4640
else
47-
textInfo "$regx: No EC2 Instances Found" "$regx"
41+
textPass "${regx}: No EC2 Instances found older than 12 months" "${regx}"
4842
fi
4943
done
5044
}

0 commit comments

Comments
 (0)