From 210f44f66f2b8ff5d6145866f5284b3ca6f5a19c Mon Sep 17 00:00:00 2001 From: Sergio Garcia <38561120+sergargar@users.noreply.github.com> Date: Wed, 11 May 2022 10:16:29 +0200 Subject: [PATCH] fix(custom-file-in-bucket): Custom file names are also support for S3 output. (#1129) --- include/outputs_bucket | 54 +++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/include/outputs_bucket b/include/outputs_bucket index 621b9fca..b9036478 100644 --- a/include/outputs_bucket +++ b/include/outputs_bucket @@ -33,25 +33,41 @@ if [[ $OUTPUT_BUCKET ]]; then fi fi -copyToS3(){ +copyToS3() { # Prowler will copy each format to its own folder in S3, that is for better handling # and processing by Quicksight or others. - if [[ $OUTPUT_BUCKET ]]; then - if [[ "${MODES[@]}" =~ "csv" ]]; then - $AWSCLI $PROFILE_OPT s3 cp $OUTPUT_DIR/prowler-output-${ACCOUNT_NUM}-${OUTPUT_DATE}.$EXTENSION_CSV \ - s3://$OUTPUT_BUCKET/csv/ --acl bucket-owner-full-control - fi - if [[ "${MODES[@]}" =~ "html" ]]; then - $AWSCLI $PROFILE_OPT s3 cp $OUTPUT_DIR/prowler-output-${ACCOUNT_NUM}-${OUTPUT_DATE}.$EXTENSION_HTML \ - s3://$OUTPUT_BUCKET/html/ --acl bucket-owner-full-control - fi - if [[ "${MODES[@]}" =~ "json" ]]; then - $AWSCLI $PROFILE_OPT s3 cp $OUTPUT_DIR/prowler-output-${ACCOUNT_NUM}-${OUTPUT_DATE}.$EXTENSION_JSON \ - s3://$OUTPUT_BUCKET/json/ --acl bucket-owner-full-control - fi - if [[ "${MODES[@]}" =~ "json-asff" ]]; then - $AWSCLI $PROFILE_OPT s3 cp $OUTPUT_DIR/prowler-output-${ACCOUNT_NUM}-${OUTPUT_DATE}.$EXTENSION_ASFF \ - s3://$OUTPUT_BUCKET/json-asff/ --acl bucket-owner-full-control - fi + # Also, check if -F was introduced + if [ -n "${OUTPUT_FILE_NAME+x}" ]; then + OUTPUT_PATH="${OUTPUT_FILE_NAME}" + else + OUTPUT_PATH="$OUTPUT_DIR/prowler-output-${ACCOUNT_NUM}-${OUTPUT_DATE}" fi -} \ No newline at end of file + + for output_format in "${MODES[@]}"; + do + case ${output_format} in + csv) + s3cp "${OUTPUT_PATH}" "${EXTENSION_CSV}" + ;; + html) + s3cp "${OUTPUT_PATH}" "${EXTENSION_HTML}" + ;; + json) + s3cp "${OUTPUT_PATH}" "${EXTENSION_JSON}" + ;; + json-asff) + s3cp "${OUTPUT_PATH}" "${EXTENSION_ASFF}" + ;; + *) + echo "$OPTRED ERROR!$OPTNORMAL - Invalid output format copying to S3. Use -h for help." + exit 1 + ;; + esac + done +} + +s3cp(){ + OUTPUT_PATH="${1}" + EXTENSION="${2}" + "${AWSCLI}" ${PROFILE_OPT} s3 cp "${OUTPUT_PATH}.${EXTENSION}" s3://"${OUTPUT_BUCKET}"/${EXTENSION}/ --acl bucket-owner-full-control +}