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

BAH - 1566 | Added. terrascan to pre-commit hook #1

Merged
merged 3 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ repos:
rev: v0.1.17
hooks:
- id: terraform-fmt
- repo: local
hooks:
- id: terrascan
name: Terrascan
language: script
entry : ./terrascan/terrascan.sh
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ This is a one-time setup that needs to be run only when the repo is cloned.
pip install pre-commit
(or)
brew install pre-commit
2. Initialise pre-commit hooks
2. Install pre-commit dependencies

- [terrascan](https://github.com/accurics/terrascan)

3. Initialise pre-commit hooks

pre-commit install --install-hooks

Expand Down
2 changes: 1 addition & 1 deletion terraform/modules/rds/security_group.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ resource "aws_security_group" "rds" {
from_port = var.mysql_rds_port
to_port = var.mysql_rds_port
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
cidr_blocks = [data.aws_vpc.bahmni-vpc.cidr_block]
}
tags = {
Name = "bahmni-rds-sg-${var.environment}"
Expand Down
3 changes: 3 additions & 0 deletions terrascan/terrascan-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rules:
skip-rules:
- AC_AWS_0480 # Skips validation of detailed monitoring for EC2 Instances
24 changes: 24 additions & 0 deletions terrascan/terrascan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# Terrascan scan and save output to scan_results
scan_results="$(terrascan scan --config-path terrascan/terrascan-config.yaml --iac-type terraform --use-colors t)"

# Get scan summary from scan_results
scan_summary=$(echo "$scan_results" | sed -n -e '/Scan Summary -/,$p' | sed -r "s/[[:cntrl:]]\[([0-9]{1,3};)*[0-9]{1,3}m//g")

#Extract values from scan_summary
low_level_violations=$(grep "Low" <<< "$scan_summary" | grep -o -E "[0-9]+")
medium_level_violations=$(grep "Medium" <<< "$scan_summary" | grep -o -E "[0-9]+")
high_level_violations=$(grep "High" <<< "$scan_summary" | grep -o -E "[0-9]+")

#Show Scan Output
echo "$scan_results"

#Check for violations
if [[ $high_level_violations -gt 0 ]]
then
echo "High Level Voilations Found"
exit 1
fi
echo "No Violations found"
exit 0