Schedule icon
TrivyCLI icon
Docker icon
OutputValues icon
Log icon
If icon
Fail icon

Trivy IaC Configuration Security Gate

Scan Terraform and Kubernetes IaC with Trivy for misconfigurations, aggregate findings by severity, and fail the gate on critical or high policy breaches.

Categories
CoreInfrastructure

Security misconfigurations in Infrastructure-as-Code (IaC) templates, such as publicly accessible object storage buckets, unrestricted security groups, or unhardened container runtime security contexts, are among the leading causes of cloud environment compromises. Catching these misconfigurations before deployment prevents vulnerabilities from ever reaching production infrastructure.

This blueprint provides an automated pre-deployment security gate using Aqua Security Trivy (io.kestra.plugin.trivy.cli.TrivyCLI). It scans Terraform files and Kubernetes manifests directly in the execution workspace, parses the JSON vulnerability report with Pebble and jq, tallies findings across CRITICAL, HIGH, MEDIUM, and LOW severity tiers, prints an itemized summary, and halts the workflow via io.kestra.plugin.core.execution.Fail when misconfigurations meet or exceed the chosen severity threshold.

The included Terraform and Kubernetes fixtures allow the flow to run out of the box with zero external dependencies.

How it works

  1. scan_iac (io.kestra.plugin.trivy.cli.TrivyCLI) stages the IaC fixtures in the container workspace via inputFiles and runs trivy config --format json --output trivy-report.json --severity CRITICAL,HIGH,MEDIUM,LOW . using the pinned aquasec/trivy:0.58.1 image. By omitting --exit-code 1, Trivy emits the full structured report without aborting prematurely on detected findings, while fatal container runtime failures or invalid flags immediately fail the task.
  2. summarize_findings (io.kestra.plugin.core.output.OutputValues) extracts the structured metrics from trivy-report.json using Pebble's read(), fromJson(), and jq filters: total target count, total findings count, severity breakdowns, and computes the threshold count via Pebble arithmetic based on inputs.severity_threshold.
  3. log_findings (io.kestra.plugin.core.log.Log) prints an audit summary with the severity distribution and lists up to 20 qualifying findings including check ID, target file, check title, and remediation resolution.
  4. check_empty_scan (io.kestra.plugin.core.flow.If) validates that at least one IaC target was scanned. If zero targets were evaluated, fail_empty_scan (io.kestra.plugin.core.execution.Fail) halts execution to prevent false-positive passes.
  5. evaluate_security_gate (io.kestra.plugin.core.flow.If) inspects the threshold findings count:
    • If findings exist and fail_on_findings is true: fail_security_gate (io.kestra.plugin.core.execution.Fail) blocks deployment with an itemized severity failure message.
    • If findings exist and fail_on_findings is false: warn_gate_bypassed (io.kestra.plugin.core.log.Log, level: WARN) flags the security debt without stopping execution.
    • If zero qualifying findings exist: log_gate_passed (io.kestra.plugin.core.log.Log, level: INFO) confirms compliance for deployment.
  6. outputs exposes the raw JSON report URI and metric counts so downstream pipeline stages or auditing systems can consume them.

What you get

  • Automated, zero-infrastructure IaC policy enforcement before changes reach cloud providers or Kubernetes clusters.
  • Multi-framework support covering Terraform (.tf), Kubernetes manifests (.yaml), Dockerfiles, and CloudFormation out of the box.
  • Configurable severity gating (CRITICAL, HIGH, MEDIUM, LOW) with graceful bypass toggles for non-blocking audits.
  • Complete severity telemetry and itemized remediation guidance logged directly in Kestra execution logs.
  • False-pass protection guarding against empty or missing scan targets.

Who it's for

  • Platform Engineers and DevOps teams building pre-deployment validation gates.
  • Cloud Security Engineers (SecOps) enforcing CIS benchmarks and cloud compliance guardrails.
  • Development teams shifting security left without requiring commercial SaaS scanner accounts.

Prerequisites

  • A Kestra worker capable of executing Docker containers (Docker task runner).
  • Outbound internet access on the worker so Trivy can update its misconfiguration checks bundle (trivy-checks from ghcr.io/aquasecurity/trivy-checks); if running air-gapped, Trivy automatically falls back to its embedded checks bundle.

Secrets

  • None. This blueprint operates entirely on local declarative manifests and does not require third-party credentials.

Quick start

  1. Execute the flow with default settings (use_insecure_sample: true, severity_threshold: HIGH, fail_on_findings: true). The flow runs the insecure sample fixtures, detects 10 findings at or above HIGH severity (such as unrestricted SSH ingress, unencrypted S3 buckets, and privileged containers), and halts at fail_security_gate.
  2. Re-run the flow with use_insecure_sample: false. The flow scans the hardened Terraform and Kubernetes configurations, detects 0 findings at HIGH or CRITICAL severity (with 5 MEDIUM and 9 LOW informational findings), logs a PASSED confirmation, and completes green.
  3. Run with use_insecure_sample: true and fail_on_findings: false to observe the non-blocking audit mode logging a WARN without halting execution.

How to scan your own IaC

To scan your organization's actual infrastructure repositories instead of the self-contained demo fixtures:

  • Replace the inline inputFiles in scan_iac by wrapping tasks inside an io.kestra.plugin.core.flow.WorkingDirectory task with an upstream io.kestra.plugin.git.Clone task pulling your Git repository.
  • Alternatively, sync your manifests using Kestra Namespace Files (io.kestra.plugin.git.SyncNamespaceFiles) and pass the directory into TrivyCLI.
  • Point the Trivy command at specific subdirectories (e.g. trivy config --format json --output trivy-report.json terraform/environments/prod).

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.