Engineering
Policy as Code for Release Gates: Fail Builds on What Matters
A release gate that fails on every critical gets switched off. Write a policy that blocks exploited, fixable risk, warns on the rest and explains itself.
Most release gates start as one line in a pipeline: fail the build if the
scanner reports a critical. Within a month, a critical with no available fix
blocks an urgent release, someone adds || true, and the gate is gone.
Policy as code for release gates is the practice of writing that decision
down as a versioned, reviewable policy that fails a build on what matters,
warns on the rest, and says why. This guide covers the design rules that
keep a gate switched on, a working Rego policy for Trivy output, and how to
roll it out without stopping every team on day one.
What makes a release gate get switched off?
Gates fail socially before they fail technically. Four patterns account for most of it.
Blocking on things nobody can fix. A critical in a base-image package with no fixed version cannot be resolved by the developer whose build failed. Failing their build teaches them that the gate is noise.
Blocking on severity alone. Severity says how bad a vulnerability would be, not whether anyone is exploiting it. A gate that treats every critical the same blocks on the unexploited ones while giving a known-exploited high a pass.
Unexplained failures. "Policy check failed" with a link to a dashboard the developer cannot open is an invitation to bypass.
Exceptions with no expiry. A suppression added during an incident and never revisited is a permanent hole that nobody remembers approving.
A good gate is designed against each of these, and the design is easier to see in code than in a wiki page.
What should a release gate actually fail on?
Fail on risk that is both serious and actionable, and warn on everything else. A reasonable first policy:
| Condition | Effect | Why |
|---|---|---|
| Any finding in the CISA KEV catalog | Fail | Exploited in the wild, whatever its CVSS |
| Critical severity with a fixed version available | Fail | Serious, and the fix exists today |
| A committed secret | Fail | No version of this is acceptable |
| Critical with no fix | Warn | Visible, tracked, not the developer's to fix |
| High severity, more than five findings | Warn | A trend worth watching, not a blocker |
The table is a starting point. What matters is that every row has a reason a developer would accept, and that "fail" means "you can and should fix this before shipping".
How do you write the policy in Rego?
Conftest runs Open Policy Agent's Rego policies
against structured files, including a scanner's JSON report. Rules named
deny fail the run, rules named warn report without failing, and each rule
returns a message.
First assemble the input: the scan, the KEV identifiers and your exception list in one document, so the policy has everything it needs and nothing is fetched at evaluation time.
trivy image --scanners vuln,secret --format json --output scan.json "$IMAGE"
curl -sf -o kev.json \
https://www.cisa.gov/sites/default/files/feeds/known_exploited_vulnerabilities.json
jq --slurpfile kev kev.json --slurpfile exc exceptions.json \
'. + {kev: [$kev[0].vulnerabilities[].cveID], exceptions: $exc[0]}' \
scan.json > input.jsonExceptions are data, with an owner, a reason and an expiry:
[
{
"id": "CVE-2023-45853",
"reason": "minizip is not installed in this image; VEX issued",
"owner": "payments-team",
"expires": "2026-12-31T00:00:00Z"
}
]Then the policy, in policy/release.rego:
package main
kev := {id | some id in input.kev}
vulns contains v if {
some result in input.Results
some v in result.Vulnerabilities
}
secrets contains {"rule": s.RuleID, "target": result.Target} if {
some result in input.Results
some s in result.Secrets
}
excepted(id) if {
some e in input.exceptions
e.id == id
time.now_ns() < time.parse_rfc3339_ns(e.expires)
}
fixable(v) if object.get(v, "FixedVersion", "") != ""
deny contains msg if {
some v in vulns
v.VulnerabilityID in kev
not excepted(v.VulnerabilityID)
msg := sprintf("%s in %s %s is known exploited (CISA KEV); fixed in %s",
[v.VulnerabilityID, v.PkgName, v.InstalledVersion, object.get(v, "FixedVersion", "no release yet")])
}
deny contains msg if {
some v in vulns
v.Severity == "CRITICAL"
fixable(v)
not excepted(v.VulnerabilityID)
msg := sprintf("%s in %s %s is critical and fixed in %s",
[v.VulnerabilityID, v.PkgName, v.InstalledVersion, v.FixedVersion])
}
deny contains msg if {
some s in secrets
msg := sprintf("secret %s committed in %s", [s.rule, s.target])
}
warn contains msg if {
some v in vulns
v.Severity == "CRITICAL"
not fixable(v)
msg := sprintf("%s in %s has no fix yet; track it", [v.VulnerabilityID, v.PkgName])
}
warn contains msg if {
count({v | some v in vulns; v.Severity == "HIGH"}) > 5
msg := "more than five high-severity findings; plan a base-image update"
}Run it:
conftest test --policy policy/ input.jsonEach deny message names the CVE, the package, the installed version and the
fix, which is everything a developer needs to act without opening another
tool. An expired exception stops matching on its own, so the finding fails
again on the next build after the expiry date rather than staying hidden.
Notice what the policy does not do. It does not call the network, and it does
not read the clock except to compare exception expiry. Given the same
input.json, it returns the same answer, which means you can store the input
and replay any past decision.
How do you test a policy?
The same way as any other code: with fixtures that pin down the behavior you
care about. Put tests next to the policy, in policy/release_test.rego, and
run them with conftest verify --policy policy/:
package main
kev_finding := {
"kev": ["CVE-2023-4863"],
"exceptions": [],
"Results": [{"Target": "img", "Vulnerabilities": [{
"VulnerabilityID": "CVE-2023-4863", "PkgName": "libwebp",
"InstalledVersion": "1.2.4", "FixedVersion": "1.3.2", "Severity": "HIGH",
}]}],
}
test_known_exploited_high_fails if {
msgs := deny with input as kev_finding
count(msgs) == 1
}
test_expired_exception_does_not_hide_it if {
msgs := deny with input as object.union(kev_finding, {"exceptions": [{
"id": "CVE-2023-4863", "reason": "test", "owner": "test",
"expires": "2020-01-01T00:00:00Z",
}]})
count(msgs) == 1
}Write a test for every rule, and one for every bug report. When someone says "the gate blocked us on something it should not have", the fix starts with a failing test that reproduces their input.
How do you wire it into the pipeline?
Keep two failures apart: "the policy says no" and "we could not decide". They need different responses, and a gate that reports both as a red build trains people to retry until it goes green.
set -euo pipefail
if ! trivy image --scanners vuln,secret --format json --output scan.json "$IMAGE"; then
echo "::error::scan did not complete; this is not a policy result" >&2
exit 2
fi
# ... assemble input.json as above ...
conftest test --policy policy/ --output github input.jsonIf the KEV download fails, fail the job as an infrastructure error. Building the input with an empty KEV list would quietly turn the KEV rule off, which is the most dangerous possible way for the gate to degrade.
Version the policy directory like code: changes go through review, and the commit that changed a rule is the record of who decided it and why.
How do you roll a gate out without stopping every team?
Start in warn-only mode. Run the policy on every build for two or three weeks
with failures reported but not enforced, either by renaming deny to warn
or by letting the step continue on error. Count what would have failed and
why. Fix the policy where it is wrong, and fix the images where the policy is
right.
Then enforce one rule at a time, starting with the one teams will agree with
most (usually KEV), and publish the date before each change. Conftest's
--fail-on-warn flag is useful at the end of this process for a pipeline
that should treat warnings as failures, such as a release branch.
What should every gate decision record?
Enough to answer an auditor's question a year later: which policy version ran, against which input, at what time, what it decided, and which rules fired and which did not. A gate that only records failures cannot prove what was checked when it passed. Store the input document and the policy commit next to the build, and the decision becomes reproducible.
How does TrustIvan gate a release?
TrustIvan makes the same decisions with
declarative rules rather than a language.
There is no expression evaluator and no user code. A rule is a set of
conditions that must all hold (min_severity, kinds such as vulnerability
or secret, fixabilities, statuses, identifiers, older_than_days,
min_count, known_exploited, min_epss and min_risk_score) plus an
effect: fail, warn, or ignore to exempt named findings from every other
rule. The first three rows of the table above are three rules. One rule kind
is about the image rather than its findings: signed_by lists up to ten
public keys and fails unless the scan found a key-based cosign signature by
one of them, with keyless signatures reported as not verified; see
image signatures. When the data a
risk condition needs is unavailable, such as no KEV catalog loaded, the
finding does not match and the verdict says so, so a pass is never read as
"nothing is exploited".
Policy versions are immutable, and at most one policy is enabled per scope. The verdict is returned in the response body, never as the HTTP status, so a pipeline can tell "fail" from "could not answer"; it reports every rule, fired or not, with match counts and a sample of the findings responsible; and it is never recomputed, so two stages asking about the same build get the same answer. Suppressed findings are downgraded to a warning by default rather than silently passing. The pipeline tutorial walks through it end to end.
See also policy and verdicts, the machine API a pipeline calls, and prioritizing container vulnerabilities with EPSS and KEV for where the threat signals in these rules come from.