Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdk-nag-scan): copy output files to separate folders #69

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions utils/cdk-docker-execute.sh
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,41 @@ if [ "${#cfn_files[@]}" -gt 0 ]; then
debug_echo "Importing CloudFormation template file ${file} to apply CDK Nag rules against it"
npx cdk synth --context fileName="${file}" --quiet 2>> ${REPORT_PATH}
CRC=$?
echo "<<<<<< end cdk-nag result for ${cfn_filename} <<<<<<" >> ${REPORT_PATH}
mkdir -p ${_ASH_OUTPUT_DIR}/${DIRECTORY}/${cfn_filename}_cdk_nag_results

RC=$(bumprc $RC $CRC)

#
# Copy and then remove these files to avoid permission setting errors when running in a single container
# Check to see if there is output to copy, if so, create a folder and copy the files
#
fileName="*.template.json"
# echo "checking for ${fileName}" >> ${REPORT_PATH}
# find -type f -name ${fileName} >> ${REPORT_PATH} 2>&1
# ls ${fileName} >> ${REPORT_PATH} 2>&1
fileExists=$(find ${CDK_WORK_DIR}/cdk.out -type f -name ${fileName} | wc -l)
# echo "fileExists = ${fileExists}" >> ${REPORT_PATH}
reportsName="AwsSolutions-*-NagReport.csv"
# echo "checking for ${reportsName}" >> ${REPORT_PATH}
# find -type f -name ${reportsName} >> ${REPORT_PATH} 2>&1
# ls ${reportsName} >> ${REPORT_PATH} 2>&1
reportsExist=$(find ${CDK_WORK_DIR}/cdk.out -type f -name ${reportsName} | wc -l)
# echo "reportsExist = ${reportsExist}" >> ${REPORT_PATH}
if [ "${fileExists}" -gt 0 -o "${reportsExist}" -gt 0 ]; then
mkdir -p ${_ASH_OUTPUT_DIR}/${DIRECTORY}/${cfn_filename}_cdk_nag_results

echo "Writing CDK-NAG reports for ${cfn_filename}" >> ${REPORT_PATH}
#
# Copy and then remove these files to avoid permission setting errors when running in a single container
#
cp ${CDK_WORK_DIR}/cdk.out/*.template.json ${_ASH_OUTPUT_DIR}/${DIRECTORY}/${cfn_filename}_cdk_nag_results/ >/dev/null 2>&1
rm ${CDK_WORK_DIR}/cdk.out/*.template.json >/dev/null 2>&1
cp ${CDK_WORK_DIR}/cdk.out/AwsSolutions-*-NagReport.csv ${_ASH_OUTPUT_DIR}/${DIRECTORY}/${cfn_filename}_cdk_nag_results/ >/dev/null 2>&1
rm ${CDK_WORK_DIR}/cdk.out/AwsSolutions-*-NagReport.csv >/dev/null 2>&1
else
echo "No CDK-NAG reports generated for ${cfn_filename}" >> ${REPORT_PATH}
fi

RC=$(bumprc $RC $CRC)
echo "<<<<<< end cdk-nag result for ${cfn_filename} <<<<<<" >> ${REPORT_PATH}
done
cp ${CDK_WORK_DIR}/cdk.out/*.template.json ${_ASH_OUTPUT_DIR}/${DIRECTORY}/${cfn_filename}_cdk_nag_results/
rm ${CDK_WORK_DIR}/cdk.out/*.template.json
cp ${CDK_WORK_DIR}/cdk.out/AwsSolutions-*-NagReport.csv ${_ASH_OUTPUT_DIR}/${DIRECTORY}/${cfn_filename}_cdk_nag_results/
rm ${CDK_WORK_DIR}/cdk.out/AwsSolutions-*-NagReport.csv
else
echo "found ${#cfn_files[@]} files to scan. Skipping scans." >> ${REPORT_PATH}
fi
Expand Down
Loading