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

+47 -9
View File
@@ -24,6 +24,7 @@ import argparse
import json
import os
import subprocess
import urllib.error
import urllib.request
from collections.abc import Callable, Iterable
from dataclasses import asdict, dataclass, field
@@ -127,8 +128,9 @@ def render(verdict: Verdict, current: str, candidate: str) -> str:
return "\n".join(lines).rstrip() + "\n"
def trivy_sbom(ref: str) -> dict: # pragma: no cover - container seam
"""Trivy's package inventory of *ref* (no vulnerability DB)."""
def trivy_sbom(ref: str, platform: str | None = None) -> dict: # pragma: no cover - container seam
"""Trivy's package inventory of *ref* (no vulnerability DB); of the *platform*
variant (``linux/arm64``) when *ref* is an index of several."""
proc = subprocess.run(
[
"docker",
@@ -150,6 +152,7 @@ def trivy_sbom(ref: str) -> dict: # pragma: no cover - container seam
"--list-all-pkgs",
"--scanners",
"license",
*(["--platform", platform] if platform else []),
ref,
],
capture_output=True,
@@ -160,20 +163,55 @@ def trivy_sbom(ref: str) -> dict: # pragma: no cover - container seam
return json.loads(proc.stdout)
def kb_match(current: dict, candidate: dict) -> dict: # pragma: no cover - network seam
def kb_key() -> str:
"""The key kb.netork.io's matcher is asked with; a PermissionError when there is none."""
key = os.environ.get("NETORK_LICENSE_KEY", "").strip()
if not key:
raise PermissionError(
"NETORK_LICENSE_KEY is empty: the matcher cannot be asked. "
"Set the secret to a kb API key (nak_kb_…) whose plan grants feature_match."
)
return key
def refusal(status: int, key: str) -> str:
"""What kb.netork.io's 401 or 403 means, for the log; never the key itself.
The licence server's forward-auth answers before kb-server does: 401 is a key
it does not know (a licence key always is one, it takes per-service API keys
only), 403 a known key without the plan for it. kb-server's own 403 is a plan
without ``feature_match``, which no sold plan grants.
"""
if status == 401:
kind = (
"NETORK_LICENSE_KEY holds a licence key (nlk_…), which kb.netork.io never accepts"
if key.startswith("nlk_")
else "kb.netork.io does not know the key in NETORK_LICENSE_KEY"
)
return f"401: {kind}. It must be a kb API key (nak_kb_…) of an instance with feature_match."
return (
f"{status}: the key in NETORK_LICENSE_KEY is known, but its plan does not grant "
"kb's feature_match (or kb at all)."
)
def kb_match(current: dict, candidate: dict, key: str | None = None) -> dict: # pragma: no cover
"""kb-server's verdict on both inventories, in one call (kb-server#155)."""
key = key or kb_key()
body = json.dumps({"current": current, "candidate": candidate}).encode()
request = urllib.request.Request(
f"{KB_URL}/api/v1/match",
data=body,
method="POST",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ['NETORK_LICENSE_KEY']}",
},
headers={"Content-Type": "application/json", "Authorization": f"Bearer {key}"},
)
with urllib.request.urlopen(request, timeout=TIMEOUT) as response: # noqa: S310 - https
return json.load(response)
try:
with urllib.request.urlopen(request, timeout=TIMEOUT) as response: # noqa: S310 - https
return json.load(response)
except urllib.error.HTTPError as exc:
if exc.code in (401, 403):
raise PermissionError(refusal(exc.code, key)) from exc
raise
def debian_version_of(ref: str) -> str: # pragma: no cover - container seam