#!/usr/bin/env bash

# Prowler - the handy cloud security tool (copyright 2019) 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_extra7182="7.182"
CHECK_TITLE_extra7182="[extra7182] Directory Service SNS Notifications"
CHECK_SCORED_extra7182="NOT_SCORED"
CHECK_CIS_LEVEL_extra7182="EXTRA"
CHECK_SEVERITY_extra7182="Medium"
CHECK_ASFF_RESOURCE_TYPE_extra7182="AwsDirectoryService"
CHECK_ALTERNATE_check7182="extra7182"
CHECK_SERVICENAME_extra7182="ds"
CHECK_RISK_cextra7182="As a best practice, monitor status of Directory Service. This helps to avoid late actions to fix Directory Service issues"
CHECK_REMEDIATION_extra7182="It is recommended set up SNS messaging to send email or text messages when the status of your directory changes"
CHECK_DOC_extra7182="https://docs.aws.amazon.com/directoryservice/latest/admin-guide/ms_ad_enable_notifications.html"
CHECK_CAF_EPIC_extra7182="Infrastructure Security"

extra7182(){
  for regx in $REGIONS; do
    DIRECTORY_SERVICE_IDS=$("${AWSCLI}" ds describe-directories ${PROFILE_OPT} --region "${regx}" --query 'DirectoryDescriptions[*].DirectoryId[]' --output text 2>&1)
    if grep -q -E 'AccessDenied|UnauthorizedOperation|AuthorizationError' <<< "${DIRECTORY_SERVICE_IDS}"; then
        textInfo "${regx}: Access Denied trying to describe directories" "${regx}"
        continue
    fi
    
    if [[ ${DIRECTORY_SERVICE_IDS} ]]; then
        for DIRECTORY_ID in ${DIRECTORY_SERVICE_IDS}; do
            DIRECTORY_SERVICE_MONITORING=$("${AWSCLI}" ds describe-event-topics ${PROFILE_OPT} --region "${regx}" --directory-id "${DIRECTORY_ID}" --query 'EventTopics' --output text 2>&1)
            if grep -q -E 'AccessDenied|UnauthorizedOperation|AuthorizationError' <<< "${DIRECTORY_SERVICE_MONITORING}"; then
                textInfo "${regx}: Access Denied trying to describe Directory Service event topics" "${regx}"
                continue
            fi
            if [[ "${DIRECTORY_SERVICE_MONITORING}" ]]; then
                textPass "${regx}: Directory Service ${DIRECTORY_ID} have SNS messaging enabled" "${regx}" "${DIRECTORY_ID}"
            else 
                textFail "${regx}: Directory Service ${DIRECTORY_ID} does not have SNS messaging enabled" "${regx}" "${DIRECTORY_ID}"     
            fi
        done
    else
        textInfo "${regx}: No Directory Service found" "${regx}"
    fi
  done
}
