Skip to content

Lesson 7 of 12 · 20m

IaC Scanning: Kill the Misconfig in the Diff

The public S3 bucket is born as one line in a Terraform PR weeks before deploy — trivy config and Checkov gate the diff, and tfstate carries plaintext secrets.

The misconfig that opens your database to the internet isn't born in the cloud console — it's born in a pull request, as one line in a Terraform file, weeks before anyone deploys it. That's the leverage: infrastructure-as-code turns "someone clicked the wrong thing in a console" into "a diff a scanner can read," and a diff can be gated.

The wrong mental model is "misconfiguration is an ops problem found by auditing the environment." Console audits find what exists. IaC scanning finds what will exist — and it comments on the line while the author still remembers writing it.

What the scanner reads

Tools like trivy config and Checkov parse Terraform, CloudFormation, Kubernetes manifests, Helm, and Dockerfiles, then match resources against policy-as-code rule sets. tfsec's checks were merged into Trivy in 2024 — install Trivy, not the abandoned standalone binary. A typical gate:

trivy config --severity HIGH,CRITICAL --exit-code 1 terraform/
# checkov's --check takes check IDs — severity words like HIGH,CRITICAL
# only filter behind a Prisma Cloud API key (--bc-api-key); keyless, gate on names
checkov -d terraform/ --framework terraform --check CKV_AWS_20,CKV_AWS_24

What it catches, concretely:

resource "aws_security_group_rule" "ssh" {
  type        = "ingress"
  from_port   = 22
  to_port     = 22
  cidr_blocks = ["0.0.0.0/0"]        # every finding engine on earth flags this
}

resource "aws_s3_bucket" "reports" {
  acl = "public-read"                # aws_s3_bucket_public_access_block exists for a reason
}
containers:
- name: app
  securityContext:
    privileged: true                 # container = host, more or less
  image: registry.internal/app:latest

Security groups wide open on 22 or 3389, public S3 buckets and SAS tokens, privileged: true and hostNetwork: true in pod specs, runAsUser: 0, missing encryption flags on RDS/EBS/azurerm_storage_account, :latest image tags, plaintext secrets in user_data and environment blocks. These aren't exotic — they're the rule families every IaC scanner ships in its first hundred checks.

Why it earns its keep

Public-bucket and open-ACL misconfigs are the recurring mechanism behind a decade of data leaks — the Verizon/NICE Systems exposure in 2017 put up to 14 million subscriber records in a public S3 bucket — UpGuard's count; Verizon acknowledged six million — and the pattern hasn't slowed since. Capital One's 2019 breach ran through an over-permissive IAM role attached to a WAF EC2 instance — an infrastructure decision, not an application bug. A trivy config check for wildcard IAM actions and public buckets costs CI seconds; the incident costs years.

Two traps nobody warns you about

  • Terraform state is a secrets store. terraform.tfstate carries plaintext resource attributes — including any secret you passed to user_data, a random provider output, or a database password. Lock down the state backend (private bucket, encryption, no public CI artifacts), and never terraform show -json into a log.
  • Plan output leaks. CI logs that echo terraform plan print + password = "hunter2" for everyone with Actions read access. Mask it, or run plans where the output isn't world-readable.

Where it fits

Gate PRs that touch *.tf, main.yml under kubernetes/, and Dockerfile — the findings are cheap, fast, and mostly true positives. Custom rules ride along: Checkov custom checks in Python or YAML, OPA/Rego via conftest test -p policy/ for the policies no pack knows — "every load balancer needs waf_enabled", "no namespace outside prod-* may request a public IP."

One honest limit: IaC scanning sees the intent, not the drift. The Terraform may be clean while the live account has a security group someone opened by hand in March. Cloud-side audit tools — Prowler, ScoutSuite, aws config conformance packs — close that gap from the other direction, and they belong on the scheduled side of the pipeline.

Field notes

  • Scan the modules, not just the root repo — your modules/vpc/ is where 0.0.0.0/0 hides.
  • Kubernetes YAML gets scanned by the same tools; kube-linter and kubesec add pod-security-specific checks for securityContext gaps.
  • A finding in IaC is one line to fix. This is the highest fix-per-dollar ratio in the whole course — gate it hard.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.