Skip to content

Commit b720254

Browse files
committed
add rule for enforcing terrascan pre-commit hook
1 parent c09f5f3 commit b720254

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests:
2+
- name: "Should have Talisman pre-commit hook configured"
3+
def: {}
4+
params: {}
5+
expect: "pass"
6+
git:
7+
repo_base: correct
8+
- name: "Should fail Talisman pre-commit hook is not configured"
9+
def: {}
10+
params: {}
11+
expect: "fail"
12+
git:
13+
repo_base: misconfigured
14+
- name: "Should fail is pre-commit is not configured at all"
15+
def: {}
16+
params: {}
17+
expect: "fail"
18+
git:
19+
repo_base: empty
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v3.2.0
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- id: check-yaml
8+
- id: check-added-large-files
9+
10+
- repo: https://github.com/tenable/terrascan
11+
rev: 'v1.28.0'
12+
hooks:
13+
- id: terraform-pre-commit
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v3.2.0
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- id: check-yaml
8+
- id: check-added-large-files
9+
args: ['--maxkb=600']
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
version: v1
3+
release_phase: alpha
4+
type: rule-type
5+
name: require_terrascan_pre_commit_hook
6+
display_name: Enable Terrascan Pre-commit hooks for detecting compliance and security violations
7+
short_failure_message: Terrascan Pre-commit hook is not configured for the repository
8+
severity:
9+
value: medium
10+
context: {}
11+
description: |
12+
Verifies that Terrascan Pre-commit hook is configured for the repository
13+
guidance: |
14+
Ensure that Terrascan is configured as a (pre-commit)[https://pre-commit.com/]
15+
hook for the repository.
16+
17+
Terrascan is a static code analyzer for Infrastructure as Code. Terrascan allows you to:
18+
- Seamlessly scan infrastructure as code for misconfigurations.
19+
- Monitor provisioned cloud infrastructure for configuration changes that introduce posture drift, and enables reverting to a secure posture.
20+
- Detect security vulnerabilities and compliance violations.
21+
- Mitigate risks before provisioning cloud native infrastructure.
22+
- Offers flexibility to run locally or integrate with your CI\CD.
23+
24+
[Read more](https://runterrascan.io/)
25+
26+
def:
27+
in_entity: repository
28+
rule_schema:
29+
type: object
30+
properties: {}
31+
ingest:
32+
type: git
33+
git: {}
34+
eval:
35+
type: rego
36+
rego:
37+
type: deny-by-default
38+
def: |
39+
package minder
40+
import future.keywords.if
41+
import future.keywords.every
42+
43+
default message := "Terrascan pre-commit hook is not configured for the repository"
44+
default allow := false
45+
46+
47+
# pre-commit hook
48+
precommit := file.read(".pre-commit-config.yaml")
49+
50+
parsed_data := parse_yaml(precommit)
51+
52+
allow if {
53+
some repo_id, hook_id
54+
repo_data := parsed_data.repos[repo_id]
55+
endswith(repo_data["repo"], "https://github.com/tenable/terrascan")
56+
hooks = repo_data["hooks"]
57+
hooks[hook_id].id == "terraform-pre-commit"
58+
}
59+
60+
message := "" if allow
61+
alert:
62+
type: security_advisory
63+
security_advisory: {}

0 commit comments

Comments
 (0)