Skip to main content

10 - Infrastructure as Code Scanning - IaC Security and Terraform Policy

Terraform scanning (Checkov, tfsec), CloudFormation/ARM security, misconfig detection, policy enforcement, secrets in IaC, best practices.

What do you think about this article?

At a Glance

Terraform scanning (Checkov, tfsec), CloudFormation/ARM security, misconfig detection, policy enforcement, secrets in IaC, best practices.

IaC Scanning: Infrastructure as Code Security

Infrastructure as Code (IaC) has revolutionized infrastructure management, but it has also introduced a new type of risk: misconfigurations. A public S3 bucket, an overly permissive security group, a database without encryption at rest are all configuration errors that can be intercepted before deployment with IaC scanning.

IaC scanning tools analyze Terraform, CloudFormation, ARM templates, Kubernetes manifests, and other IaC formats looking for insecure configurations, comparing them against best practices and compliance standards like CIS Benchmarks.

What You'll Learn

  • Why IaC scanning is fundamental for cloud security
  • Checkov: multi-framework IaC scanner
  • tfsec: Terraform-specific analysis
  • KICS: universal scanner by Checkmarx
  • Custom policies for Terraform with Sentinel and Conftest
  • CI/CD pipeline integration

Most Common Misconfigurations

Cloud misconfigurations are the cause of 65% of security incidents in cloud environments. Here are the most frequent ones:

Top 10 Cloud Misconfigurations

# Misconfiguration Risk
1Public storage bucketData breach, data leakage
2Security group 0.0.0.0/0Unauthorized access
3Database without encryptionCompliance violation, data theft
4Logging disabledImpossible forensics
5Overly permissive IAM policiesPrivilege escalation
6Versioning disabled on storageData loss, ransomware
7SSH open to the worldBrute force, server compromise
8Secrets in IaC variablesCredential exposure
9No backup configuredPermanent data loss
10TLS not enforcedMan-in-the-middle, data interception

Checkov: Multi-Framework Scanner

Checkov by Bridgecrew (now Prisma Cloud) is the most comprehensive IaC scanner, with over 1000 built-in policies for Terraform, CloudFormation, ARM, Kubernetes, Helm, Dockerfile, and Serverless. It also supports custom policies in Python and YAML.


# Install Checkov
pip install checkov

# Scan Terraform files
checkov -d terraform/ --framework terraform

# Scan with SARIF output
checkov -d terraform/ -o sarif --output-file checkov-results.sarif

# Scan with severity filter
checkov -d terraform/ --check HIGH --check CRITICAL

# Scan a Dockerfile
checkov -f Dockerfile --framework dockerfile

# Scan Kubernetes manifests
checkov -d k8s/ --framework kubernetes

# Skip specific checks
checkov -d terraform/ --skip-check CKV_AWS_18,CKV_AWS_19
      

Checkov Integration in GitHub Actions


# .github/workflows/iac-scanning.yml
name: IaC Security Scan
on:
  pull_request:
    paths:
      - "terraform/**"
      - "k8s/**"
      - "Dockerfile"

jobs:
  checkov:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Checkov Scan
        uses: bridgecrewio/checkov-action@master
        with:
          directory: terraform/
          framework: terraform
          output_format: sarif
          output_file_path: checkov-results.sarif
          soft_fail: false
          check: HIGH,CRITICAL

      - name: Upload SARIF
        if: always()
        uses: github/codeql-action/upload-sarif@v3
        with:
          sarif_file: checkov-results.sarif
      

tfsec: Terraform-Specific Analysis

tfsec (now part of Trivy) is a dedicated Terraform scanner that analyzes HCL files looking for security misconfigurations. It's particularly fast and accurate, with support for Terraform modules and variables.


# Install tfsec
brew install tfsec

# Basic scan
tfsec terraform/

# Scan with SARIF output
tfsec terraform/ --format sarif --out tfsec-results.sarif

# Scan with minimum severity
tfsec terraform/ --minimum-severity HIGH

# Exclude specific checks
tfsec terraform/ --exclude aws-s3-enable-versioning
      

KICS: Universal Scanner

KICS (Keeping Infrastructure as Code Secure) by Checkmarx is an open source scanner supporting over 15 IaC platforms. It's particularly useful for organizations using multiple IaC technologies that want a single tool.


# Run KICS with Docker
docker run -v /path/to/project:/path checkmarx/kics:latest scan \
  -p /path \
  -o /path/results \
  --report-formats sarif,json \
  --fail-on high,critical
      

Custom Policy for Terraform

Beyond built-in policies, it's often necessary to create organization-specific custom policies. Here's an example with Conftest/Rego:


# policy/terraform/aws_security.rego
package terraform.aws

# All S3 buckets must have encryption
deny[msg] {
  resource := input.resource.aws_s3_bucket[name]
  not resource.server_side_encryption_configuration
  msg := sprintf("S3 bucket '%s' must have encryption enabled", [name])
}

# RDS must have backup enabled
deny[msg] {
  resource := input.resource.aws_db_instance[name]
  resource.backup_retention_period == 0
  msg := sprintf("RDS instance '%s' must have backup enabled (retention > 0)", [name])
}

# No security group with ingress 0.0.0.0/0 on SSH
deny[msg] {
  resource := input.resource.aws_security_group[name]
  ingress := resource.ingress[_]
  ingress.from_port <= 22
  ingress.to_port >= 22
  cidr := ingress.cidr_blocks[_]
  cidr == "0.0.0.0/0"
  msg := sprintf("Security group '%s' must not allow SSH (22) from 0.0.0.0/0", [name])
}
      

Terraform Policy Enforcement with Sentinel

IaC Scanning Tools Comparison

  • Checkov: most complete, 1000+ policies, multi-framework, custom Python/YAML policies
  • tfsec: Terraform-specific, fast, integrated in Trivy
  • KICS: universal, 15+ platforms, great for multi-IaC environments
  • Conftest: flexible, uses Rego, great for custom policies
  • Sentinel: native Terraform Cloud, integrated policy enforcement

IaC Scanning Best Practices

  1. Scan on every PR: every IaC change must be validated before merge
  2. Severity-based blocking: block deploys for HIGH and CRITICAL, warnings for MEDIUM
  3. Custom policies: create organization-specific standard policies
  4. Baseline and progression: establish a baseline and progressively reduce findings
  5. Pre-commit hooks: run tfsec/checkov locally before push
  6. Drift detection: monitor drift between IaC state and actual infrastructure

Conclusions

IaC scanning is essential for preventing misconfigurations that are the leading cause of cloud security incidents. With Checkov, tfsec, and custom policies, organizations can automatically validate every infrastructure change before it reaches production.

In the next article, we'll explore Runtime Security, analyzing real-time monitoring with Falco, eBPF, and anomaly detection systems to protect production applications.

Frequently asked questions

What is the main risk introduced by Infrastructure as Code (IaC) management?

Misconfigurations.

How do IaC scanning tools analyze infrastructure configurations?

They compare them against best practices and compliance standards like CIS Benchmarks.

What is Checkov, and what types of IaC formats does it support?

Checkov is a multi-framework scanner that supports Terraform, CloudFormation, ARM templates, Kubernetes manifests, Helm, Dockerfile, Serverless, and more.

Why are custom policies necessary in IaC scanning?

Because built-in policies may not cover organization-specific requirements.

Sources and further reading

Try These Tools

Free developer tools related to this topic.

Commenti

Caricamento commenti...

Sign in to comment

Discuss this article in the community

Join the Telegram group to discuss with other developers, ask questions and share your experiences.

Did you like this article?

Explore other content on the blog or discover my projects