feat(cve-gate): no image goes out with a new fixable critical or high CVE
CI / Tests, lint (pull_request) Successful in 13s

The release gate of NetOrk/netork#1390. The verdict answers "does a rebuild
improve on the pin?"; a release needs the other question: does the candidate
bring a critical or high CVE that the last release of the same image does not
have and that has a fix? Both inventories go to netOrk's matcher in one call
(kb-server's POST /api/v1/match, one state of its data), and only new and
fixable blocks. A new one without a fix is listed: nobody can close it yet.

No verdict never passes, and an empty NETORK_LICENSE_KEY is said so before
Trivy runs. kb.netork.io's 401 and 403 come from the licence server's
forward-auth and now say what they mean: a licence key where a kb API key
belongs, an unknown key, or a plan without feature_match.
This commit is contained in:
Christian Manivong committed 2026-11-08 00:56:11 +01:00
1 parent adf80a7131
commit 1a7319bac4
10 files changed
+640 -10

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
{
"SchemaVersion": 2,
"ArtifactName": "registry.netork.io/netork/engine:main-abc1234",
"ArtifactType": "container_image",
"Metadata": {
"OS": {"Family": "debian", "Name": "13.1"},
"ImageID": "sha256:2222222222222222222222222222222222222222222222222222222222222222"
},
"Results": [
{
"Target": "registry.netork.io/netork/engine:main-abc1234 (debian 13.1)",
"Class": "os-pkgs",
"Type": "debian",
"Packages": [
{"ID": "openssl@3.5.1-1", "Name": "openssl", "Version": "3.5.1-1", "SrcName": "openssl", "SrcVersion": "3.5.1-1"},
{"ID": "zlib1g@1:1.3.dfsg+really1.3.1-2", "Name": "zlib1g", "Version": "1:1.3.dfsg+really1.3.1-2", "SrcName": "zlib", "SrcVersion": "1:1.3.dfsg+really1.3.1-2"}
]
},
{
"Target": "Python",
"Class": "lang-pkgs",
"Type": "python-pkg",
"Packages": [
{"ID": "jinja2@3.1.5", "Name": "jinja2", "Version": "3.1.5"},
{"ID": "urllib3@2.4.0", "Name": "urllib3", "Version": "2.4.0"}
]
}
]
}
+24
View File
@@ -0,0 +1,24 @@
{
"src": "netork-kb",
"corpus_revision": 48213,
"current": {"high": 2},
"candidate": {"critical": 1, "high": 3},
"findings": {
"current": [
{"cve": "CVE-2026-1111", "package": "openssl", "installed": "3.5.1-1", "severity": "high", "fixed": "3.5.1-1+deb13u1", "fix_available": true, "ecosystem": "Debian:13"},
{"cve": "CVE-2026-2222", "package": "jinja2", "installed": "3.1.5", "severity": "high", "fixed": "3.1.6", "fix_available": true, "ecosystem": "PyPI"}
],
"candidate": [
{"cve": "CVE-2026-1111", "package": "openssl", "installed": "3.5.1-1", "severity": "high", "fixed": "3.5.1-1+deb13u1", "fix_available": true, "ecosystem": "Debian:13"},
{"cve": "CVE-2026-2222", "package": "jinja2", "installed": "3.1.5", "severity": "high", "fixed": "3.1.6", "fix_available": true, "ecosystem": "PyPI"},
{"cve": "CVE-2026-3333", "package": "urllib3", "installed": "2.4.0", "severity": "high", "fixed": "2.5.0", "fix_available": true, "ecosystem": "PyPI"},
{"cve": "CVE-2026-4444", "package": "zlib1g", "installed": "1:1.3.dfsg+really1.3.1-2", "severity": "critical", "fixed": null, "fix_available": false, "ecosystem": "Debian:13"}
]
},
"closed": [],
"new": [
{"cve": "CVE-2026-3333", "package": "urllib3", "installed": "2.4.0", "severity": "high", "fixed": "2.5.0"},
{"cve": "CVE-2026-4444", "package": "zlib1g", "installed": "1:1.3.dfsg+really1.3.1-2", "severity": "critical", "fixed": null}
],
"point_release": null
}
+29
View File
@@ -0,0 +1,29 @@
{
"SchemaVersion": 2,
"ArtifactName": "registry.netork.io/netork/engine:0.49.0",
"ArtifactType": "container_image",
"Metadata": {
"OS": {"Family": "debian", "Name": "13.1"},
"ImageID": "sha256:1111111111111111111111111111111111111111111111111111111111111111"
},
"Results": [
{
"Target": "registry.netork.io/netork/engine:0.49.0 (debian 13.1)",
"Class": "os-pkgs",
"Type": "debian",
"Packages": [
{"ID": "openssl@3.5.1-1", "Name": "openssl", "Version": "3.5.1-1", "SrcName": "openssl", "SrcVersion": "3.5.1-1"},
{"ID": "zlib1g@1:1.3.dfsg+really1.3.1-1", "Name": "zlib1g", "Version": "1:1.3.dfsg+really1.3.1-1", "SrcName": "zlib", "SrcVersion": "1:1.3.dfsg+really1.3.1-1"}
]
},
{
"Target": "Python",
"Class": "lang-pkgs",
"Type": "python-pkg",
"Packages": [
{"ID": "jinja2@3.1.5", "Name": "jinja2", "Version": "3.1.5"},
{"ID": "urllib3@2.3.0", "Name": "urllib3", "Version": "2.3.0"}
]
}
]
}
+16 -1
View File
@@ -20,7 +20,7 @@ ACTIONS = sorted((ROOT / "actions").glob("*/action.yml"))
def test_the_actions():
names = [a.parent.name for a in ACTIONS]
assert names == ["bump", "judge", "sign", "verdict", "verify-base"]
assert names == ["bump", "cve-gate", "judge", "sign", "verdict", "verify-base"]
@pytest.mark.parametrize("action", ACTIONS, ids=lambda a: a.parent.name)
@@ -50,6 +50,21 @@ def test_a_verdict_that_could_not_be_reached_fails_the_step():
assert "cat report.md" in step["run"].splitlines()
def test_the_cve_gate_fails_the_step_when_it_blocks_or_cannot_judge():
"""The gate's exit code is the step's: 1 for a new fixable CVE, 2 for no verdict
(an empty NETORK_LICENSE_KEY included). The report reaches the log either way."""
spec = yaml.safe_load((ROOT / "actions/cve-gate/action.yml").read_text())
[step] = spec["runs"]["steps"]
lines = step["run"].splitlines()
assert {"release", "candidate", "arch"} <= set(spec["inputs"])
assert spec["outputs"]["passed"]["value"] == "${{ steps.gate.outputs.passed }}"
assert "python3 -m image_tools.cve_gate" in step["run"]
assert "cat cve-gate.md" in lines
assert lines[-1] == 'exit "$status"'
assert "NETORK_LICENSE_KEY" in spec["description"] and "secrets." not in step["run"]
def test_bumping_needs_its_token_from_the_caller():
text = (ROOT / "actions/bump/action.yml").read_text()
+248
View File
@@ -0,0 +1,248 @@
"""No image is pushed with a new fixable critical or high CVE (NetOrk/netork#1390).
Trivy lists the packages of the candidate and of the last release of the same
image, netOrk's matcher in kb-server judges both in one call (one state of its
data, kb-server#155), and the gate fails only on what is new **and** fixable:
a critical or high (CVE, package) the release does not have and that has a
fixed version. What the release already had is no regression, and a new one
nobody can fix yet would only stop every build until upstream ships a fix; both
are listed, neither blocks.
A gate that cannot ask the matcher does not pass: no image goes out on a guess.
The fixtures are two Trivy ``--list-all-pkgs`` reports and kb-server's answer
on them, as ``POST /api/v1/match`` gives it.
"""
from __future__ import annotations
import json
import subprocess
import urllib.error
from pathlib import Path
import pytest
from image_tools import verify
from image_tools.cve_gate import Gate, check, gate, render
FIXTURES = Path(__file__).parent / "fixtures" / "cve_gate"
RELEASE = "registry.netork.io/netork/engine:0.49.0"
CANDIDATE = "registry.netork.io/netork/engine:main-abc1234"
def _fixture(name: str) -> dict:
return json.loads((FIXTURES / name).read_text())
def _answer(new=(), current=None, candidate=None):
return {
"corpus_revision": 7,
"current": current or {"high": 1},
"candidate": candidate or {"high": 1},
"closed": [],
"new": list(new),
}
def _new(cve="CVE-2026-9999", severity="high", fixed="1.2.4"):
return {
"cve": cve,
"package": "libfoo",
"installed": "1.2.3",
"severity": severity,
"fixed": fixed,
}
class TestGate:
@pytest.mark.parametrize("severity", ["critical", "high", "HIGH"])
def test_a_new_fixable_critical_or_high_blocks(self, severity):
result = gate(_answer(new=[_new(severity=severity)]))
assert not result.passed
assert [f["cve"] for f in result.blocking] == ["CVE-2026-9999"]
def test_nothing_new_passes(self):
result = gate(_answer())
assert result.passed
assert result.blocking == [] and result.unfixable == []
@pytest.mark.parametrize("fixed", [None, ""])
def test_a_new_one_without_a_fix_is_listed_and_passes(self, fixed):
result = gate(_answer(new=[_new(fixed=fixed)]))
assert result.passed
assert [f["cve"] for f in result.unfixable] == ["CVE-2026-9999"]
@pytest.mark.parametrize("severity", ["medium", "low", "unknown", None])
def test_only_critical_and_high_count(self, severity):
"""kb-server lists only those as new; a weaker one slipping through blocks nothing."""
result = gate(_answer(new=[_new(severity=severity)]))
assert result.passed
assert result.blocking == [] and result.unfixable == []
def test_the_counts_and_the_corpus_state_are_kept(self):
result = gate(_answer(current={"high": 2}, candidate={"high": 3}))
assert result.counts == {"release": {"high": 2}, "candidate": {"high": 3}}
assert result.corpus_revision == 7
class TestCheck:
"""Against the fixtures: the release and the candidate as Trivy lists them."""
def _seams(self, answer=None, error=None):
inventories = {RELEASE: "release.trivy.json", CANDIDATE: "candidate.trivy.json"}
calls = {"sbom": [], "match": []}
def sbom(ref, platform=None):
calls["sbom"].append((ref, platform))
return _fixture(inventories[ref])
def match(current, candidate):
calls["match"].append((current, candidate))
if error:
raise error
return answer
return calls, {"sbom": sbom, "match": match}
def test_both_go_to_the_matcher_in_one_call_the_release_as_current(self):
calls, seams = self._seams(answer=_fixture("match.json"))
check(RELEASE, CANDIDATE, platform="linux/amd64", **seams)
assert calls["sbom"] == [(RELEASE, "linux/amd64"), (CANDIDATE, "linux/amd64")]
assert calls["match"] == [
(_fixture("release.trivy.json"), _fixture("candidate.trivy.json"))
]
def test_only_the_new_fixable_one_blocks(self):
"""urllib3 moved to a version with a fixable high: blocked. zlib's new
critical has no fix: listed. openssl's and jinja2's highs were in the
release already: not new, so neither listed nor blocking."""
_, seams = self._seams(answer=_fixture("match.json"))
result = check(RELEASE, CANDIDATE, **seams)
assert not result.passed
assert [(f["cve"], f["package"]) for f in result.blocking] == [("CVE-2026-3333", "urllib3")]
assert [(f["cve"], f["package"]) for f in result.unfixable] == [("CVE-2026-4444", "zlib1g")]
assert result.error is None
def test_a_candidate_that_is_the_release_passes(self):
_, seams = self._seams(answer=_answer())
assert check(RELEASE, CANDIDATE, **seams).passed
@pytest.mark.parametrize(
"error",
[
OSError("connection refused"),
ValueError("Expecting value: line 1 column 1"),
subprocess.CalledProcessError(1, "docker"),
urllib.error.URLError("timed out"),
],
)
def test_a_matcher_that_cannot_be_asked_does_not_pass(self, error):
_, seams = self._seams(error=error)
result = check(RELEASE, CANDIDATE, **seams)
assert not result.passed
assert result.error
def test_a_release_trivy_cannot_read_does_not_pass(self):
def sbom(ref, platform=None):
raise subprocess.CalledProcessError(1, "trivy", stderr="MANIFEST_UNKNOWN")
result = check(RELEASE, CANDIDATE, sbom=sbom, match=lambda a, b: _answer())
assert not result.passed
assert "MANIFEST_UNKNOWN" in result.error
def test_without_the_key_it_fails_before_anything_is_sent(self, monkeypatch):
monkeypatch.delenv("NETORK_LICENSE_KEY", raising=False)
calls, seams = self._seams()
result = check(RELEASE, CANDIDATE, sbom=seams["sbom"])
assert not result.passed
assert "NETORK_LICENSE_KEY" in result.error
assert calls["sbom"] == [], "Trivy runs only when there is someone to ask"
class TestRefusal:
"""kb.netork.io is behind the licence server's forward-auth: what a 401 or a 403
means is not obvious from the status alone, so the gate says it."""
def test_401_names_the_kind_of_key_it_needs(self):
text = verify.refusal(401, "nak_kb_ABCDEFGH")
assert "nak_kb_" in text and "NETORK_LICENSE_KEY" in text
def test_401_with_a_licence_key_says_so(self):
text = verify.refusal(401, "nlk_ABCDEFGH")
assert "licence key" in text
assert "ABCDEFGH" not in text, "the key itself never reaches a log"
def test_403_names_the_entitlement(self):
assert "feature_match" in verify.refusal(403, "nak_kb_ABCDEFGH")
def test_an_empty_key_is_refused_before_a_call(self, monkeypatch):
monkeypatch.setenv("NETORK_LICENSE_KEY", " ")
with pytest.raises(PermissionError, match="NETORK_LICENSE_KEY"):
verify.kb_key()
class TestReport:
def test_a_blocked_candidate_names_what_blocks_it_and_what_does_not(self):
result = gate(_fixture("match.json"))
text = render(result, RELEASE, CANDIDATE)
assert text.startswith("**Blocked**")
assert RELEASE in text and CANDIDATE in text
blocking, listed = text.split("### New without a fix")
assert "CVE-2026-3333" in blocking and "2.5.0" in blocking
assert "CVE-2026-4444" in listed
assert "48213" in text
def test_a_passing_candidate_says_so(self):
assert render(gate(_answer()), RELEASE, CANDIDATE).startswith("**Passes**")
def test_no_verdict_says_why(self):
text = render(Gate(False, error="PermissionError: boom"), RELEASE, CANDIDATE)
assert text.startswith("**No verdict**")
assert "boom" in text
@pytest.mark.parametrize(
("result", "code"),
[
(Gate(True), 0),
(Gate(False, blocking=[_new()]), 1),
(Gate(False, error="down"), 2),
],
)
def test_the_exit_code_tells_blocked_from_no_verdict(result, code):
assert result.exit_code == code
def test_the_result_is_json_for_the_workflow():
data = json.loads(gate(_fixture("match.json")).to_json())
assert data["passed"] is False
assert data["blocking"][0]["cve"] == "CVE-2026-3333"
assert set(data) == {
"passed",
"blocking",
"unfixable",
"counts",
"corpus_revision",
"error",
}