id: grype-cve-promote-gate
namespace: company.team
description: |
Stop container images from being promoted to production on a known-bad
vulnerability scan. A CI job POSTs the image reference to a keyed
webhook, grype scans it inside its official container with the exit code
absorbed so the JSON report drives the decision, and a Python step grades
the matches by severity against a configurable floor. The verdict routes
through a Switch, BLOCK escalates with a GitHub issue, a Slack alert, and
a failed execution, WARN posts a recorded warning, and the scan summary
is always posted to Slack for the release record.
triggers:
- id: on_promotion_request
type: io.kestra.plugin.core.trigger.Webhook
description: CI POSTs here with the image reference when a build requests
promotion into the production registry.
key: "{{ secret('GRYPE_WEBHOOK_KEY') }}"
- id: nightly_rescan
type: io.kestra.plugin.core.trigger.Schedule
description: Fallback net, a nightly rescan of recently promoted images, shipped
disabled until an operator switches it on.
cron: "0 5 * * *"
disabled: true
inputs:
- id: image_ref
type: STRING
defaults: docker.io/library/nginx:1.25.3
description: Full image reference grype scans, including registry, repository,
and tag or digest.
- id: fail_severity
type: STRING
defaults: high
description: Lowest severity that blocks promotion, one of critical, high,
medium, or low.
- id: github_repository
type: STRING
defaults: your-org/release-gates
description: Repository in owner/repo form that receives the blocking issue when
promotion is stopped.
tasks:
# --- 1. image scan ---
- id: scan_workspace
type: io.kestra.plugin.core.flow.WorkingDirectory
description: One shared directory so the image scan and the report grader work
from the same grype JSON file.
tasks:
- id: run_grype_scan
type: io.kestra.plugin.scripts.shell.Commands
description: Write a bundled grype fixture, scan the image reference in the
grype container with the exit code absorbed, and fall back to the
fixture when the daemon is unreachable so the gate stays
deterministic.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
entryPoint: []
containerImage: anchore/grype:latest
env:
IMAGE_REF: "{{ inputs.image_ref }}"
outputFiles:
- grype-report.json
commands:
- |
set -u
cat > fixture-report.json <<'JSON'
{
"source": {
"type": "image",
"target": {"userInput": "docker.io/library/nginx:1.25.3"}
},
"matches": [
{
"vulnerability": {
"id": "CVE-2023-0286",
"severity": "Critical",
"description": "X.400 address type confusion in X.509 GeneralName.",
"fix": {"versions": ["3.0.8"], "state": "fixed"}
},
"artifact": {"name": "openssl", "version": "3.0.2"}
},
{
"vulnerability": {
"id": "CVE-2023-44487",
"severity": "High",
"description": "HTTP/2 Rapid Reset denial of service.",
"fix": {"versions": ["1.55.1"], "state": "fixed"}
},
"artifact": {"name": "nghttp2", "version": "1.52.0"}
},
{
"vulnerability": {
"id": "CVE-2023-38545",
"severity": "Medium",
"description": "SOCKS5 heap buffer overflow in the curl URL parser.",
"fix": {"versions": ["8.4.0"], "state": "fixed"}
},
"artifact": {"name": "curl", "version": "7.81.0"}
}
]
}
JSON
echo "Scanning ${IMAGE_REF} with grype"
grype "${IMAGE_REF}" -o json > grype-report.json || true
if [ ! -s grype-report.json ]; then
cp fixture-report.json grype-report.json
echo "image daemon unreachable from this worker, emitting the bundled fixture report"
fi
echo "report bytes $(wc -c < grype-report.json)"
- id: grade_report
type: io.kestra.plugin.scripts.python.Script
description: Count grype matches by severity, decide PASS, WARN, or BLOCK
against the configured severity floor, and emit the verdict through
the outputs protocol.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
containerImage: python:3.12-slim
env:
IMAGE_REF: "{{ inputs.image_ref }}"
FAIL_SEVERITY: "{{ inputs.fail_severity }}"
script: |
import json
import os
report_path = "grype-report.json"
image_ref = os.environ.get("IMAGE_REF", "unknown-image")
floor = os.environ.get("FAIL_SEVERITY", "high").strip().lower()
ranks = {"unknown": 0, "negligible": 1, "low": 2, "medium": 3, "high": 4, "critical": 5}
floor_rank = ranks.get(floor, ranks["high"])
matches = []
if os.path.exists(report_path) and os.path.getsize(report_path) > 0:
try:
with open(report_path, "r") as handle:
data = json.load(handle)
if isinstance(data, dict):
raw = data.get("matches") or data.get("results") or []
matches = [m for m in raw if isinstance(m, dict)]
except ValueError:
matches = []
counts = {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0, "NEGLIGIBLE": 0, "UNKNOWN": 0}
fixed_count = 0
rows = []
blocking = []
for match in matches:
vuln = match.get("vulnerability") or {}
artifact = match.get("artifact") or {}
vid = str(vuln.get("id") or "UNKNOWN")
severity = str(vuln.get("severity") or "UNKNOWN").strip().upper()
if severity not in counts:
severity = "UNKNOWN"
counts[severity] += 1
fix = vuln.get("fix") or {}
fix_versions = [str(v) for v in (fix.get("versions") or [])]
if fix_versions:
fixed_count += 1
pkg = str(artifact.get("name") or "unknown") + "@" + str(artifact.get("version") or "")
row = {"id": vid, "severity": severity, "package": pkg}
rows.append(row)
if ranks.get(severity.lower(), 0) >= floor_rank:
blocking.append(row)
worst = "NONE"
for sev in ("CRITICAL", "HIGH", "MEDIUM", "LOW", "NEGLIGIBLE"):
if counts[sev] > 0:
worst = sev
break
blocked = len(blocking) > 0
if blocked:
verdict = "BLOCK"
elif counts["MEDIUM"] > 0:
verdict = "WARN"
else:
verdict = "PASS"
cve_ids = sorted({r["id"] for r in rows})
cve_summary = ", ".join(
r["package"] + ":" + r["id"] + " (" + r["severity"] + ")" for r in rows
) or "none"
blocking_summary = ", ".join(
r["id"] + " " + r["severity"] for r in blocking
) or "none"
payload = {
"image_ref": image_ref,
"total_matches": len(rows),
"critical_count": counts["CRITICAL"],
"high_count": counts["HIGH"],
"medium_count": counts["MEDIUM"],
"low_count": counts["LOW"] + counts["NEGLIGIBLE"] + counts["UNKNOWN"],
"fixed_count": fixed_count,
"worst_severity": worst,
"cve_ids": cve_ids,
"cve_summary": cve_summary,
"blocking_summary": blocking_summary,
"blocked": blocked,
"verdict": verdict,
}
print("image=%s matches=%d worst=%s verdict=%s" % (
image_ref, len(rows), worst, verdict))
print("::" + json.dumps({"outputs": payload}) + "::")
# --- 2. release record ---
- id: notify_scan_result
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Post the scan summary to the release channel on every run so the
promotion record never depends on which branch fired.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "grype scan {{ outputs.grade_report.vars.verdict }} for {{ inputs.image_ref }} - {{ outputs.grade_report.vars.total_matches }} match(es), worst severity {{ outputs.grade_report.vars.worst_severity }}, blocking findings {{ outputs.grade_report.vars.blocking_summary }} at or above {{ inputs.fail_severity }}. Execution {{ execution.id }}."
}
# --- 3. verdict routing ---
- id: route_verdict
type: io.kestra.plugin.core.flow.Switch
description: Route on the graded verdict, one case per outcome so escalation is
explicit and the default can never skip the gate silently.
value: "{{ outputs.grade_report.vars.verdict }}"
cases:
BLOCK:
- id: create_block_issue
type: io.kestra.plugin.github.issues.Create
description: Open a labeled GitHub issue listing the blocking CVEs so the fix
lands in the same backlog engineers already work from.
jwtToken: "{{ secret('GITHUB_TOKEN') }}"
repository: "{{ inputs.github_repository }}"
title: "Promotion blocked for {{ inputs.image_ref }}"
body: |
## Container promotion blocked
**{{ inputs.image_ref }}** reported **{{ outputs.grade_report.vars.total_matches }}** vulnerability match(es), worst severity **{{ outputs.grade_report.vars.worst_severity }}**.
Blocking at or above {{ inputs.fail_severity }}: {{ outputs.grade_report.vars.blocking_summary }}
Execution {{ execution.id }}
Remediate, rebuild the image, and promote again.
labels:
- security
- container-scan
- id: alert_slack_block
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Tell the release channel that promotion stopped and which CVEs
caused it.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "BLOCKED promotion of {{ inputs.image_ref }} - blocking findings {{ outputs.grade_report.vars.blocking_summary }}. Issue opened in {{ inputs.github_repository }}. Execution {{ execution.id }}."
}
- id: fail_promotion
type: io.kestra.plugin.core.execution.Fail
description: Fail the execution so the pipeline promoting the image stops before
the registry write.
errorMessage: "Promotion of {{ inputs.image_ref }} blocked by grype at severity
floor {{ inputs.fail_severity }}."
WARN:
- id: alert_slack_warn
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Allow the promotion but post the medium-severity findings as a
recorded warning.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "WARN promotion of {{ inputs.image_ref }} - {{ outputs.grade_report.vars.medium_count }} medium-severity match(es) below the {{ inputs.fail_severity }} floor. Execution {{ execution.id }}."
}
PASS:
- id: log_pass
type: io.kestra.plugin.core.log.Log
description: Record the clean scan so the execution history doubles as the
promotion evidence.
message: "grype found nothing at or above {{ inputs.fail_severity }} for {{
inputs.image_ref }} with {{ outputs.grade_report.vars.total_matches
}} total match(es)."
defaults:
- id: log_unknown_verdict
type: io.kestra.plugin.core.log.Log
description: Catch an unexpected verdict string loudly instead of letting the
gate pass by accident.
message: "Unexpected grype verdict {{ outputs.grade_report.vars.verdict }} for
{{ inputs.image_ref }}, review the grade_report outputs."
errors:
- id: notify_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Escalate any failure to the ops channel with the execution url.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{"text": "FAILED: {{ flow.id }} {{ execution.id }}"}
outputs:
- id: verdict
type: STRING
description: BLOCK at or above the severity floor, WARN for medium-only
findings, otherwise PASS.
value: "{{ outputs.grade_report.vars.verdict }}"
- id: total_matches
type: INT
description: Number of vulnerability matches grype reported for the image, for
example 3.
value: "{{ outputs.grade_report.vars.total_matches }}"
- id: worst_severity
type: STRING
description: Highest severity class present in the matches, for example CRITICAL.
value: "{{ outputs.grade_report.vars.worst_severity }}"
- id: cve_ids
type: ARRAY
description: 'Distinct advisory ids matched in the image, for example
["CVE-2023-0286"].'
value: "{{ outputs.grade_report.vars.cve_ids | toJson }}"
- id: blocking_summary
type: STRING
description: Human summary of the matches at or above the severity floor.
value: "{{ outputs.grade_report.vars.blocking_summary }}"