Policies to validate Terraform module sources across multiple policy engines.
Edit allowed-sources.yaml to customize allowed module sources. Sentinel policy has the source list inline.
allowed_sources:
registry:
- "cloudposse/*"
- "terraform-aws-modules/*"
- "aws-ia/*"
git:
- "github.com/cloudposse/*"
- "github.com/terraform-aws-modules/*"
- "github.com/aws-ia/*"The Sentinel policy validates module sources in HCP Terraform runs:
# sentinel.hcl
policy "module-source-policy" {
source = "./module-source-policy.sentinel"
enforcement_level = "hard-mandatory"
}# Run specific check
checkov -f main.tf --external-checks-dir checkov/ --check CKV_TF_MODULE_SOURCE
# Run all checks (includes custom check)
checkov -f main.tf --external-checks-dir checkov/# Test policy with example inputs
cd examples && ./test-opa.sh
# Run policy against Terraform plan JSON
opa exec --decision terraform/module_sources/deny --bundle opa/ --fail-non-empty plan.jsonAdd to .pre-commit-config.yaml:
repos:
- repo: local
hooks:
- id: validate-module-sources
name: Validate module sources
entry: ./pre-commit/validate-module-sources.sh
language: script
files: \.tf$examples/pass/pass.tf- Module sources that should pass validation (5 modules)examples/fail/fail.tf- Module sources that should fail validation (4 unauthorized + 1 allowed)examples/pass/opa/input.json- Complete Terraform plan JSON for passing casesexamples/fail/opa/input.json- Complete Terraform plan JSON for failing casesexamples/pass/plan.json- Source Terraform plan fileexamples/fail/plan.json- Source Terraform plan file
