id: trivy-iac-configuration-security-gate
namespace: company.team
description: |
Scan Infrastructure-as-Code (Terraform, Kubernetes manifests) with Trivy for
misconfigurations before deployment, report findings by severity, and fail the gate
when findings at or above a chosen severity threshold are detected.
inputs:
- id: severity_threshold
type: SELECT
displayName: Severity Threshold
description: Minimum finding severity that triggers a security gate failure.
values:
- LOW
- MEDIUM
- HIGH
- CRITICAL
defaults: HIGH
- id: use_insecure_sample
type: BOOL
displayName: Use Insecure Sample
description: When true, injects known misconfigurations (unencrypted S3, open
SG, privileged container) to watch the gate fail. When false, compliant
fixtures run and the gate passes.
defaults: true
- id: fail_on_findings
type: BOOL
displayName: Fail on Findings
description: When true, terminates the flow as FAILED when qualifying
misconfigurations are found. When false, logs a warning and finishes
green.
defaults: true
triggers:
- id: nightly_iac_compliance_audit
type: io.kestra.plugin.core.trigger.Schedule
description: Nightly scheduled audit to catch new misconfiguration rules against
infrastructure definitions; shipped disabled.
cron: "0 4 * * *"
disabled: true
tasks:
- id: scan_iac
type: io.kestra.plugin.trivy.cli.TrivyCLI
description: Scan Infrastructure-as-Code manifests for security
misconfigurations using Aqua Security Trivy. By omitting --exit-code 1,
Trivy completes with exit code 0 when misconfigurations are detected so
the full JSON report is produced for evaluation, while genuine execution
crashes or container runtime errors exit non-zero and fail the task.
containerImage: aquasec/trivy:0.58.1
timeout: PT5M
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
inputFiles:
main.tf: |
{% if inputs.use_insecure_sample %}
resource "aws_s3_bucket" "demo_bucket" {
bucket = "demo-unencrypted-data-bucket"
}
resource "aws_security_group" "demo_sg" {
name = "wide-open-sg"
description = "Security group with unrestricted inbound SSH access"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
{% else %}
resource "aws_kms_key" "s3_key" {
description = "KMS key for S3 bucket encryption"
deletion_window_in_days = 7
enable_key_rotation = true
}
resource "aws_s3_bucket" "demo_bucket" {
bucket = "demo-secure-data-bucket"
}
resource "aws_s3_bucket_server_side_encryption_configuration" "demo_encryption" {
bucket = aws_s3_bucket.demo_bucket.id
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = aws_kms_key.s3_key.arn
sse_algorithm = "aws:kms"
}
}
}
resource "aws_s3_bucket_public_access_block" "demo_pab" {
bucket = aws_s3_bucket.demo_bucket.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_security_group" "demo_sg" {
name = "restricted-sg"
description = "Security group with restricted inbound access"
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["10.0.0.0/16"]
}
}
{% endif %}
deployment.yaml: |
{% if inputs.use_insecure_sample %}
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-workload
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: app
image: cgr.dev/chainguard/nginx:1.27.4
securityContext:
privileged: true
allowPrivilegeEscalation: true
runAsNonRoot: false
{% else %}
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-workload
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
containers:
- name: app
image: cgr.dev/chainguard/nginx:1.27.4
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
{% endif %}
outputFiles:
- trivy-report.json
commands:
- trivy config --format json --output trivy-report.json --severity
CRITICAL,HIGH,MEDIUM,LOW .
- id: summarize_findings
type: io.kestra.plugin.core.output.OutputValues
description: Parse Trivy JSON scan report with Pebble and jq, calculate target
counts, aggregate findings across CRITICAL, HIGH, MEDIUM, and LOW
severities, and compute the threshold count via Pebble arithmetic.
values:
scanned_targets: >-
{{ fromJson(read(outputs.scan_iac.outputFiles['trivy-report.json'])) |
jq('[(.Results // [])[]] | length') | first }}
total_findings: >-
{{ fromJson(read(outputs.scan_iac.outputFiles['trivy-report.json'])) |
jq('[(.Results // [])[] | (.Misconfigurations // [])[]] | length') |
first }}
critical_count: >-
{{ fromJson(read(outputs.scan_iac.outputFiles['trivy-report.json'])) |
jq('[(.Results // [])[] | (.Misconfigurations // [])[] |
select(.Severity == "CRITICAL")] | length') | first }}
high_count: >-
{{ fromJson(read(outputs.scan_iac.outputFiles['trivy-report.json'])) |
jq('[(.Results // [])[] | (.Misconfigurations // [])[] |
select(.Severity == "HIGH")] | length') | first }}
medium_count: >-
{{ fromJson(read(outputs.scan_iac.outputFiles['trivy-report.json'])) |
jq('[(.Results // [])[] | (.Misconfigurations // [])[] |
select(.Severity == "MEDIUM")] | length') | first }}
low_count: >-
{{ fromJson(read(outputs.scan_iac.outputFiles['trivy-report.json'])) |
jq('[(.Results // [])[] | (.Misconfigurations // [])[] |
select(.Severity == "LOW")] | length') | first }}
threshold_count: >-
{% set c =
fromJson(read(outputs.scan_iac.outputFiles['trivy-report.json'])) |
jq('[(.Results // [])[] | (.Misconfigurations // [])[] |
select(.Severity == "CRITICAL")] | length') | first %} {% set h =
fromJson(read(outputs.scan_iac.outputFiles['trivy-report.json'])) |
jq('[(.Results // [])[] | (.Misconfigurations // [])[] |
select(.Severity == "HIGH")] | length') | first %} {% set m =
fromJson(read(outputs.scan_iac.outputFiles['trivy-report.json'])) |
jq('[(.Results // [])[] | (.Misconfigurations // [])[] |
select(.Severity == "MEDIUM")] | length') | first %} {% set l =
fromJson(read(outputs.scan_iac.outputFiles['trivy-report.json'])) |
jq('[(.Results // [])[] | (.Misconfigurations // [])[] |
select(.Severity == "LOW")] | length') | first %} {% if
inputs.severity_threshold == 'CRITICAL' %}{{ (c | number) }} {% elseif
inputs.severity_threshold == 'HIGH' %}{{ (c | number) + (h | number) }}
{% elseif inputs.severity_threshold == 'MEDIUM' %}{{ (c | number) + (h |
number) + (m | number) }} {% else %}{{ (c | number) + (h | number) + (m
| number) + (l | number) }} {% endif %}
all_findings: >-
{{ fromJson(read(outputs.scan_iac.outputFiles['trivy-report.json'])) |
jq('[(.Results // [])[] as $r | ($r.Misconfigurations // [])[] | {id:
.ID, title: .Title, severity: .Severity, target: $r.Target, resolution:
(.Resolution // "N/A")}]') | first | toJson }}
- id: log_findings
type: io.kestra.plugin.core.log.Log
description: Print an itemized summary of misconfigurations at or above the
configured severity threshold, capped to the first 20 findings.
message: |
Trivy IaC Security Scan Summary:
- Scanned Targets: {{ outputs.summarize_findings.values.scanned_targets }}
- Total Misconfigurations: {{ outputs.summarize_findings.values.total_findings }} (CRITICAL: {{ outputs.summarize_findings.values.critical_count }}, HIGH: {{ outputs.summarize_findings.values.high_count }}, MEDIUM: {{ outputs.summarize_findings.values.medium_count }}, LOW: {{ outputs.summarize_findings.values.low_count }})
- Misconfigurations at or above '{{ inputs.severity_threshold }}' threshold: {{ outputs.summarize_findings.values.threshold_count }}
{% if inputs.severity_threshold == 'CRITICAL' %}
{% set qualifying = ['CRITICAL'] %}
{% elseif inputs.severity_threshold == 'HIGH' %}
{% set qualifying = ['CRITICAL', 'HIGH'] %}
{% elseif inputs.severity_threshold == 'MEDIUM' %}
{% set qualifying = ['CRITICAL', 'HIGH', 'MEDIUM'] %}
{% else %}
{% set qualifying = ['CRITICAL', 'HIGH', 'MEDIUM', 'LOW'] %}
{% endif %}
{% set displayed = 0 %}
{% for f in fromJson(outputs.summarize_findings.values.all_findings) %}
{% if qualifying contains f.severity and displayed < 20 %}
- [{{ f.severity }}] {{ f.id }} in {{ f.target }}: {{ f.title }} (Resolution: {{ f.resolution }})
{% set displayed = displayed + 1 %}
{% endif %}
{% endfor %}
{% if (outputs.summarize_findings.values.threshold_count | number) == 0 %}
No misconfigurations detected at or above '{{ inputs.severity_threshold }}' threshold.
{% endif %}
- id: check_empty_scan
type: io.kestra.plugin.core.flow.If
description: Guard against false passes when zero configuration targets were
discovered or scanned by Trivy.
condition: "{{ (outputs.summarize_findings.values.scanned_targets | number) == 0 }}"
then:
- id: fail_empty_scan
type: io.kestra.plugin.core.execution.Fail
description: Fail execution when no IaC configuration files were discovered or
scanned.
errorMessage: "Trivy scan produced no results: zero IaC configuration targets
were discovered or scanned. Ensure target directory contains valid
Terraform, Kubernetes, or Dockerfile manifests."
- id: evaluate_security_gate
type: io.kestra.plugin.core.flow.If
description: Evaluate findings at or above the severity threshold against the
security gate policy.
condition: "{{ (outputs.summarize_findings.values.threshold_count | number) > 0 }}"
then:
- id: gate_breach
type: io.kestra.plugin.core.flow.If
description: Enforce gate failure if fail_on_findings is enabled; otherwise log
a warning and complete execution green.
condition: "{{ inputs.fail_on_findings }}"
then:
- id: fail_security_gate
type: io.kestra.plugin.core.execution.Fail
description: Halt workflow execution with an error when misconfigurations meet
or exceed the severity threshold.
errorMessage: "Security gate FAILED: detected {{
outputs.summarize_findings.values.threshold_count }}
misconfiguration(s) at or above '{{ inputs.severity_threshold }}'
severity (CRITICAL: {{
outputs.summarize_findings.values.critical_count }}, HIGH: {{
outputs.summarize_findings.values.high_count }}, MEDIUM: {{
outputs.summarize_findings.values.medium_count }}, LOW: {{
outputs.summarize_findings.values.low_count }})."
else:
- id: warn_gate_bypassed
type: io.kestra.plugin.core.log.Log
description: Log a warning when findings exceed the threshold but
fail_on_findings is disabled.
level: WARN
message: "Security gate WARNING: detected {{
outputs.summarize_findings.values.threshold_count }}
misconfiguration(s) at or above '{{ inputs.severity_threshold }}'
severity, but fail_on_findings is false. Continuing without
blocking deployment."
else:
- id: log_gate_passed
type: io.kestra.plugin.core.log.Log
description: Log successful verification confirming zero misconfigurations at or
above the severity threshold.
level: INFO
message: "Security gate PASSED: 0 misconfigurations detected at or above '{{
inputs.severity_threshold }}' severity across {{
outputs.summarize_findings.values.scanned_targets }} IaC target(s).
Ready for deployment."
outputs:
- id: json_report
type: URI
value: "{{ outputs.scan_iac.outputFiles['trivy-report.json'] }}"
- id: threshold_findings_count
type: INT
value: "{{ outputs.summarize_findings.values.threshold_count }}"
- id: total_findings_count
type: INT
value: "{{ outputs.summarize_findings.values.total_findings }}"