#!/usr/bin/env bash

# Prowler - the handy cloud security tool (copyright 2018) by Toni de la Fuente
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
CHECK_ID_extra7162="7.162"
CHECK_TITLE_extra7162="[extra7162] Check if CloudWatch Log Groups have a retention policy of at least 365 days"
CHECK_SCORED_extra7162="NOT_SCORED"
CHECK_CIS_LEVEL_extra7162="EXTRA"
CHECK_SEVERITY_extra7162="Medium"
CHECK_ASFF_RESOURCE_TYPE_extra7162="AwsLogsLogGroup"
CHECK_ALTERNATE_check7162="extra7162"
CHECK_SERVICENAME_extra7162="cloudwatch"
CHECK_RISK_extra7162='If log groups have a low retention policy of less than 365 days; crucial logs and data can be lost'
CHECK_REMEDIATION_extra7162='Add Log Retention policy of at least 365 days to log groups. This will persist logs and traces for a long time.'
CHECK_DOC_extra7162='https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/AWS_Logs.html'
CHECK_CAF_EPIC_extra7162='Data Retention'

extra7162() {
    # "Check if CloudWatch Log Groups have a retention policy of 365 days"
    local LOG_GROUP_RETENTION_PERIOD_DAYS="365"
    for regx in ${REGIONS}; do
        LIST_OF_365_OR_MORE_RETENTION_LOG_GROUPS=$("${AWSCLI}" logs describe-log-groups ${PROFILE_OPT} --region "${regx}" --query "logGroups[?retentionInDays>=\`${LOG_GROUP_RETENTION_PERIOD_DAYS}\`].[logGroupName]" --output text 2>&1)
        if grep -E -q 'AccessDenied|UnauthorizedOperation|AuthorizationError' <<< "${LIST_OF_365_OR_MORE_RETENTION_LOG_GROUPS}"; then
            textInfo "${regx}: Access Denied trying to describe log groups" "${regx}"
            continue
        fi
        if [[ ${LIST_OF_365_OR_MORE_RETENTION_LOG_GROUPS} ]]; then
            for log in ${LIST_OF_365_OR_MORE_RETENTION_LOG_GROUPS}; do
                textPass "${regx}: ${log} Log Group has at least 365 days retention period!" "${regx}" "${log}"
            done
        fi

        LIST_OF_NEVER_EXPIRE_RETENTION_LOG_GROUPS=$("${AWSCLI}" logs describe-log-groups ${PROFILE_OPT} --region "${regx}" --query "logGroups[?retentionInDays==null].[logGroupName]" --output text 2>&1)
        if grep -E -q 'AccessDenied|UnauthorizedOperation|AuthorizationError' <<< "${LIST_OF_NEVER_EXPIRE_RETENTION_LOG_GROUPS}"; then
            textInfo "${regx}: Access Denied trying to describe log groups" "${regx}"
            continue
        fi
        if [[ ${LIST_OF_NEVER_EXPIRE_RETENTION_LOG_GROUPS} ]]; then
            for log in ${LIST_OF_NEVER_EXPIRE_RETENTION_LOG_GROUPS}; do
                textPass "${regx}: ${log} Log Group retention period never expires!" "${regx}" "${log}"
            done
        fi

        LIST_OF_NON_365_RETENTION_LOG_GROUPS=$("${AWSCLI}" logs describe-log-groups ${PROFILE_OPT} --region "${regx}" --query "logGroups[?retentionInDays<\`${LOG_GROUP_RETENTION_PERIOD_DAYS}\`].[logGroupName]" --output text 2>&1)
        if grep -E -q 'AccessDenied|UnauthorizedOperation|AuthorizationError' <<< "${LIST_OF_NON_365_RETENTION_LOG_GROUPS}"; then
            textInfo "${regx}: Access Denied trying to describe log groups" "${regx}"
            continue
        fi
        if [[ ${LIST_OF_NON_365_RETENTION_LOG_GROUPS} ]]; then
            for log in ${LIST_OF_NON_365_RETENTION_LOG_GROUPS}; do
                textFail "${regx}: ${log} Log Group does not have at least 365 days retention period!" "${regx}" "${log}"
            done
        fi
        
        REGION_NO_LOG_GROUP=$("${AWSCLI}" logs describe-log-groups ${PROFILE_OPT} --region "${regx}" --output text)
        if grep -E -q 'AccessDenied|UnauthorizedOperation|AuthorizationError' <<< "${REGION_NO_LOG_GROUP}"; then
            textInfo "${regx}: Access Denied trying to describe log groups" "${regx}"
            continue
        fi
        if [[ ${REGION_NO_LOG_GROUP} ]]; then
            :
        else
            textInfo "${regx} does not have a Log Group!" "${regx}"

        fi
    done
}
