#!/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_extra725="7.25"
CHECK_TITLE_extra725="[extra725] Check if S3 buckets have Object-level logging enabled in CloudTrail (Not Scored) (Not part of CIS benchmark)"
CHECK_SCORED_extra725="NOT_SCORED"
CHECK_TYPE_extra725="EXTRA"
CHECK_ALTERNATE_check725="extra725"

# per Object-level logging is not configured at Bucket level but at CloudTrail trail level
extra725(){
  # "Check if S3 buckets have Object-level logging enabled in CloudTrail (Not Scored) (Not part of CIS benchmark)"
  textInfo "Looking for S3 Buckets Object-level logging information in all trails...  "

  # create a file with a list of all buckets
  TEMP_BUCKET_LIST_FILE=$(mktemp -t prowler.bucket-list-XXXXXX)
  $AWSCLI s3api list-buckets --query 'Buckets[*].{Name:Name}' $PROFILE_OPT --region $REGION --output text > $TEMP_BUCKET_LIST_FILE
  if [ ! -s $TEMP_BUCKET_LIST_FILE ]; then
    textInfo "$regx: No S3 buckets found" "$regx"
    exit
  fi

  # now create a list with all trails available and their region
  TEMP_TRAILS_LIST_FILE=$(mktemp -t prowler.trails-list-XXXXXX)
  for regx in $REGIONS; do
    $AWSCLI cloudtrail describe-trails $PROFILE_OPT --region $regx --query trailList[?HomeRegion==\`$regx\`].[Name,HomeRegion] --output text >> $TEMP_TRAILS_LIST_FILE
  done

  # look for buckets being logged per trail and create a list with them
  TEMP_BUCKETS_LOGGING_LIST_FILE=$(mktemp -t prowler.buckets-logging-list-XXXXXX)
  while IFS='' read -r LINE || [[ -n "${LINE}" ]]; do
    TRAIL_REGION=$(echo "${LINE}" | awk '{ print $2 }')
    TRAIL_NAME=$(echo "${LINE}" | awk '{ print $1 }')
    BUCKETS_OBJECT_LOGGING_ENABLED=$($AWSCLI cloudtrail get-event-selectors --trail-name "${TRAIL_NAME}" $PROFILE_OPT --region $TRAIL_REGION --query "EventSelectors[*].DataResources[?Type == \`AWS::S3::Object\`].Values" --output text |xargs -n1 |cut -d: -f 6|sed 's/\///g')
    echo $BUCKETS_OBJECT_LOGGING_ENABLED |tr " " "\n"|sort >> $TEMP_BUCKETS_LOGGING_LIST_FILE
    if [[ $BUCKETS_OBJECT_LOGGING_ENABLED ]]; then
      for bucket in $BUCKETS_OBJECT_LOGGING_ENABLED; do
        textPass "$regx: S3 bucket $bucket has Object-level logging enabled in trail $trail" "$regx"
      done
    fi
  done < $TEMP_TRAILS_LIST_FILE

  # diff to get the ones that are not in any trail then they are not logging
  BUCKETS_NOT_LOGGING=$(diff $TEMP_BUCKETS_LOGGING_LIST_FILE $TEMP_BUCKET_LIST_FILE | sed -n 's/^> //p')
  if [[ $BUCKETS_NOT_LOGGING ]]; then
    for bucket in $BUCKETS_NOT_LOGGING; do
      textFail "$regx: S3 bucket $bucket has Object-level logging disabled" "$regx"
   done
  fi
  # delete all temp files
  rm -fr $TEMP_BUCKET_LIST_FILE $TEMP_TRAILS_LIST_FILE $TEMP_BUCKETS_LOGGING_LIST_FILE

}
