-
Notifications
You must be signed in to change notification settings - Fork 0
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
New pre commit hook #3
base: main
Are you sure you want to change the base?
Changes from 4 commits
54c8a4b
f04be76
b2d4132
ed5efd3
17f7397
304b34e
50088b0
8ad9d5e
9598897
e254add
b09a056
0d34c4b
dc86fcb
f6f77fc
a75009e
8834a8b
0b9ea55
7e4b03b
5606ca2
71e40bd
62405ab
b05dc3d
9c16a79
59927b2
181033f
a010147
6207827
c5b0f7a
a94c175
eb5f427
3cb661a
90d746c
fb615a1
26190c3
eb3a548
daae91c
4ff1f44
04f160b
1a09a2e
681ce63
d00efff
1fba480
ca27cd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#!/bin/bash | ||
#set -x | ||
PREFIX="pre-commit:" | ||
RESULTS_FILE=tfsec-scan-output.txt | ||
TFSEC_RESULTS_DIR=/tmp/tfsec-results | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
TFSEC_WORKING_DIR=/tmp/tfsec-files | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
|
||
initialise() { | ||
mkdir -p $TFSEC_WORKING_DIR | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation should be two spaces. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
mkdir -p $TFSEC_RESULTS_DIR | ||
rm $TFSEC_WORKING_DIR/* 2> /dev/null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove stray space. Why are you redirecting STDERR to /dev/null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. Redirecting as it gives an error if this is run for the first time There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Righto, maybe you could:
|
||
rm $TFSEC_RESULTS_DIR/* 2> /dev/null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
ALLOW_TFSEC_ISSUES="no" | ||
} | ||
|
||
populateGlobalVars() { | ||
stagedFileList=$(git diff --diff-filter=d --cached --name-only) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All variables should be upper case. It's generally a good idea to quote VAR="$(some command)" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
tfFileList=$(echo "$stagedFileList" | grep -E '\.(tf)$') | ||
if [[ ! -z "$tfFileList" ]] && [ ${#tfFileList[@]} -gt 0 ]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason why you mixed [[ ]] and [ ] ..? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no reason. changed now to [[ |
||
then | ||
TERRAFORM_FILES_STAGED="yes" | ||
else | ||
TERRAFORM_FILES_STAGED="no" | ||
fi | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove stray space. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
} | ||
|
||
copyTerraformFilesToBeScanned() { | ||
|
||
echo "$PREFIX Performing a tfsec scan on the terraform files you have staged (only terraform .tf files will be parsed)." | ||
echo "$PREFIX The list of files to be scanned by tfsec will be as follows.." | ||
|
||
#browse through ALL files , not only the TF files | ||
for value in ${stagedFileList} | ||
do | ||
# copy all TF files to /tmp/tfsec-files | ||
if [[ $value == *.tf ]] | ||
then | ||
echo "$PREFIX FILE: $value" | ||
cp "$value" "$TFSEC_WORKING_DIR" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. B0rked indentation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
fi | ||
|
||
# check if the tfsec-scan-output file has been staged | ||
if [[ $value == $RESULTS_FILE ]] | ||
then | ||
# by staging this file, the user has indicated that they want to allow through the commit despite the tfsec issues. | ||
ALLOW_TFSEC_ISSUES="yes" | ||
# also copy this to the same directory so that a diff can take place later between this staged file and the latest scan output | ||
cp "$value" "$TFSEC_WORKING_DIR" | ||
|
||
fi | ||
done | ||
} | ||
|
||
removeTimingStatsFromScanOutput() { | ||
#remove any timing stats from the file, as these are problematic when doing a diff | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space after each # There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
#Firstly, save the file before any sed replacement just in case we wish to troubleshoot | ||
cp $TFSEC_RESULTS_DIR/$RESULTS_FILE $TFSEC_RESULTS_DIR/tfsec-scan-output-orig.txt | ||
sed -i '/^ disk i/d' $TFSEC_RESULTS_DIR/$RESULTS_FILE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quoting and cuddling are a must here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
sed -i '/^ parsing/d' $TFSEC_RESULTS_DIR/$RESULTS_FILE | ||
sed -i '/^ adaptation/d' $TFSEC_RESULTS_DIR/$RESULTS_FILE | ||
sed -i '/^ checks/d' $TFSEC_RESULTS_DIR/$RESULTS_FILE | ||
sed -i '/^ total/d' $TFSEC_RESULTS_DIR/$RESULTS_FILE | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two blank lines here, but elsewhere you use a single blank line between code blocks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
|
||
informUserThatWeAllowCommitThroughDespiteTfsecIssues() { | ||
echo "$PREFIX Your Commit is being let through despite tfsec issues!!!" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A pile of echo statements is calling out for a sub-procedure (there is a my-info example in the style guide) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
echo "$PREFIX You have indicated that we should let the commit go through, but please review the tfsec issues detected with the team!" | ||
echo "$PREFIX The tfsec-scan-output.txt file has details of these tfsec issues shown and has been committed too" | ||
} | ||
|
||
informUserThatWeDenyCommitBecauseTfSecNotUptoDate() { | ||
echo "$PREFIX Your Commit is disallowed and has been aborted!!!" | ||
echo "$PREFIX Even though you have GIT add(ed) your $RESULTS_FILE file, it is not up to date." | ||
echo "$PREFIX Please check the latest issues in $TFSEC_RESULTS_DIR/$RESULTS_FILE and see if you wish to still ignore" | ||
echo "$PREFIX If you still wish to ignore issues, please GIT add the latest file $TFSEC_RESULTS_DIR/$RESULTS_FILE" | ||
} | ||
|
||
|
||
informUserThatWeDenyCommitBecauseOutstandingTfSecIssues() { | ||
echo "$PREFIX Your Commit is disallowed and has been aborted!!!" | ||
echo "$PREFIX You should aim to fix the above reported tfsec issues." | ||
echo "$PREFIX The output of the tfsec scan was also written to the file $TFSEC_RESULTS_DIR/$RESULTS_FILE" | ||
echo "$PREFIX NOTE : If you cannot fix and instead want these to be reviewed, please GIT add the following file" | ||
echo "$PREFIX NOTE : (The file to GIT add is $TFSEC_RESULTS_DIR/$RESULTS_FILE)" | ||
echo "$PREFIX NOTE : If you GIT add the above file, the commit will be allowed through" | ||
} | ||
|
||
|
||
initialise; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the ; are needed. Also, if this is the start of the main block, you should have a comment to suggest things have started. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
populateGlobalVars; | ||
|
||
if [ "$TERRAFORM_FILES_STAGED" = "yes" ]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then should be beneath the if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you using [ ] or [[ ]]..? I think [[ ]] is safer, but I can't remember why. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [[ now |
||
|
||
copyTerraformFilesToBeScanned; | ||
|
||
#Do the latest tfsec scan on the files that have been staged | ||
if ! tfsec $TFSEC_WORKING_DIR --no-colour --out $TFSEC_RESULTS_DIR/$RESULTS_FILE; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quoting and cuddling, blah, blah. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
|
||
removeTimingStatsFromScanOutput; | ||
|
||
if [ "$ALLOW_TFSEC_ISSUES" = "yes" ] | ||
then | ||
#Check that the file added is exactly the same as the latest tfsec issues. They must be identical in order for the commit to be allowed through | ||
diffResult=$(diff $TFSEC_RESULTS_DIR/$RESULTS_FILE $TFSEC_WORKING_DIR/$RESULTS_FILE) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
if [ $? -eq 0 ] | ||
then | ||
informUserThatWeAllowCommitThroughDespiteTfsecIssues; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a fan of Camel case function names. Also, could they be shorter? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
exit 0 | ||
else | ||
informUserThatWeDenyCommitBecauseTfSecNotUptoDate; | ||
exit 1 | ||
fi | ||
else | ||
informUserThatWeDenyCommitBecauseOutstandingTfSecIssues; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
exit 1 | ||
fi | ||
fi | ||
fi |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
# tfsec-testing | ||
|
||
## Instructions on using git hooks in this repository. | ||
--- | ||
*Please run the following command to set the git hooks directory as .githooks/* | ||
--- | ||
git config --local core.hooksPath .githooks/ | ||
|
||
*The pre-commit hook* | ||
--- | ||
This pre-commit hooks runs *tfsec* on any Terraform (.tf) files included as part of the commit. | ||
If it finds *tfsec* issues reported, it will then disallow the commit. To override this, read the full instructions on what to do. This is output by the pre-commit hook |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quote your variables please.
Also, it would help if there were some useful comments - reading comments aids understanding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok