From 8563d236dc29f86c3a5f303184e0b4a9d38ba540 Mon Sep 17 00:00:00 2001 From: Omar Sy Date: Thu, 18 Jun 2026 08:30:42 +0200 Subject: [PATCH] fix(greffer-updater): make resolve_digest buildx-version-robust + bump 0.3.7 provenance.resolve_digest used `docker buildx imagetools inspect --format '{{.Manifest.Digest}}'`, which on buildx >= ~0.35 silently falls back to the verbose listing instead of the bare digest -> resolve_digest returns None -> verify_and_pull refuses EVERY remote update ("cannot resolve digest"). The mocked unit tests never caught it (they stubbed the stdout); confirmed live: the updater's buildx 0.34.1 emits the digest, but a newer local buildx emits the listing -> a future alpine buildx bump in the updater image would silently break all self-updates. Parse `--format '{{json .Manifest}}'` and read its `digest` instead (the image-index digest CI cosign-signs). Verified the JSON form returns the SAME digest on both buildx 0.34.1 (current updater) and newer, so it is behavior-preserving now and robust to a bump. Regression test added for the verbose-fallback (non-JSON) case. Bump 0.3.6 -> 0.3.7. --- app/__init__.py | 2 +- cli/greffer_cli/updater/provenance.py | 26 +++++++++++++++---- cli/tests/test_updater_provenance.py | 37 ++++++++++++++++++++++++--- pyproject.toml | 2 +- tests/test_workers_register.py | 4 +-- 5 files changed, 58 insertions(+), 13 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index a26937b9..a9633b6c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -2,4 +2,4 @@ # per-greffon ``min_greffer_version`` compatibility gate can refuse deploying an # L4 (or any version-floored) greffon onto a greffer too old to run it. Keep in # sync with pyproject.toml. Single source for the register report. -__version__ = "0.3.6" +__version__ = "0.3.7" diff --git a/cli/greffer_cli/updater/provenance.py b/cli/greffer_cli/updater/provenance.py index 3fd9da46..2d16b7cb 100644 --- a/cli/greffer_cli/updater/provenance.py +++ b/cli/greffer_cli/updater/provenance.py @@ -13,6 +13,8 @@ from __future__ import annotations +import json + from .. import compose # Baked into the greffer-updater image at build time (the publish CI commits @@ -25,17 +27,31 @@ def resolve_digest(ref: str) -> str | None: - """Resolve ``greffon/:`` to its content digest (``sha256:<64hex>``), - or None. Uses ``docker buildx imagetools inspect`` so it needs no pull.""" + """Resolve ``greffon/:`` to its image-INDEX digest + (``sha256:<64hex>``, the manifest-list digest CI cosign-signs), or None. + Uses ``docker buildx imagetools inspect`` so it needs no pull. + + Parses ``--format '{{json .Manifest}}'`` and reads its ``digest`` rather than + ``--format '{{.Manifest.Digest}}'``: the latter is buildx-version-fragile (on + buildx >= ~0.35 it silently falls back to the verbose listing instead of + emitting the bare digest, so the whole verify-then-pull would refuse). The + JSON form is stable across the buildx versions tested (the updater's 0.34.1 + and newer).""" res = compose._run( ["docker", "buildx", "imagetools", "inspect", ref, - "--format", "{{.Manifest.Digest}}"], + "--format", "{{json .Manifest}}"], timeout=60, ) if not res.ok: return None - digest = res.stdout.strip() - if not digest.startswith("sha256:"): + try: + manifest = json.loads(res.stdout) + except (json.JSONDecodeError, ValueError): + # A buildx that ignored --format and printed the verbose listing, or any + # non-JSON: fail closed rather than misread a digest. + return None + digest = manifest.get("digest") if isinstance(manifest, dict) else None + if not isinstance(digest, str) or not digest.startswith("sha256:"): return None hexpart = digest[len("sha256:"):] return digest if len(hexpart) == 64 and all( diff --git a/cli/tests/test_updater_provenance.py b/cli/tests/test_updater_provenance.py index eb6459c8..4afad391 100644 --- a/cli/tests/test_updater_provenance.py +++ b/cli/tests/test_updater_provenance.py @@ -8,10 +8,19 @@ from __future__ import annotations +import json + from greffer_cli import compose from greffer_cli.updater import provenance +def _manifest(digest: str) -> compose.CommandResult: + """A `docker buildx imagetools inspect --format '{{json .Manifest}}'` body.""" + return compose.CommandResult( + 0, json.dumps({"mediaType": "application/vnd.oci.image.index.v1+json", + "digest": digest, "size": 1609}), "") + + def _ok(out: str = "") -> compose.CommandResult: return compose.CommandResult(0, out, "") @@ -22,15 +31,35 @@ def _fail() -> compose.CommandResult: # --- digest resolution ----------------------------------------------- -def test_resolve_digest_ok(monkeypatch): - monkeypatch.setattr(compose, "_run", lambda a, **k: _ok("sha256:" + "a" * 64 + "\n")) +def test_resolve_digest_reads_index_digest_from_manifest_json(monkeypatch): + monkeypatch.setattr(compose, "_run", lambda a, **k: _manifest("sha256:" + "a" * 64)) assert provenance.resolve_digest("greffon/greffer:0.3.5") == "sha256:" + "a" * 64 + # and it asks for the JSON manifest, not the fragile {{.Manifest.Digest}} + seen = {} + monkeypatch.setattr(compose, "_run", + lambda a, **k: (seen.update(args=a), _manifest("sha256:" + "b" * 64))[1]) + provenance.resolve_digest("greffon/greffer:0.3.5") + assert "{{json .Manifest}}" in seen["args"] -def test_resolve_digest_rejects_malformed(monkeypatch): +def test_resolve_digest_rejects_malformed_digest(monkeypatch): for bad in ("", "sha256:zz", "deadbeef", "sha256:" + "a" * 63, "sha256:" + "g" * 64): - monkeypatch.setattr(compose, "_run", lambda a, _b=bad, **k: _ok(_b)) + monkeypatch.setattr(compose, "_run", lambda a, _b=bad, **k: _manifest(_b)) assert provenance.resolve_digest("greffon/greffer:0.3.5") is None + + +def test_resolve_digest_handles_verbose_fallback_and_errors(monkeypatch): + # REGRESSION: on a newer buildx, `--format` is ignored and the verbose + # listing is printed; the old {{.Manifest.Digest}} code read that as a digest + # and returned None. Now non-JSON fails closed cleanly. + monkeypatch.setattr(compose, "_run", + lambda a, **k: _ok("Name: docker.io/greffon/greffer:0.3.6\n" + "Digest: sha256:" + "a" * 64 + "\n")) + assert provenance.resolve_digest("greffon/greffer:0.3.5") is None + # JSON without a digest field + monkeypatch.setattr(compose, "_run", lambda a, **k: _ok('{"mediaType": "x"}')) + assert provenance.resolve_digest("greffon/greffer:0.3.5") is None + # subprocess failure monkeypatch.setattr(compose, "_run", lambda a, **k: _fail()) assert provenance.resolve_digest("greffon/greffer:0.3.5") is None diff --git a/pyproject.toml b/pyproject.toml index 7c6d881e..251bfb4a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "greffer" -version = "0.3.6" +version = "0.3.7" description = "" authors = ["Greffon "] license = "AGPL-3.0-only" diff --git a/tests/test_workers_register.py b/tests/test_workers_register.py index 29222a2f..3feadde2 100644 --- a/tests/test_workers_register.py +++ b/tests/test_workers_register.py @@ -78,12 +78,12 @@ def test_post_register_version_defaults_to_app_version(settings: Settings) -> No from app import __version__ # Sanity: the fixture didn't set GREFFER_VERSION, so the default applies. - assert settings.greffer_version == __version__ == "0.3.6" + assert settings.greffer_version == __version__ == "0.3.7" with patch("app.workers.register.requests") as mock_requests: mock_requests.post.return_value.status_code = 200 _post_register(settings, "10.0.0.1", "tok") kwargs = mock_requests.post.call_args.kwargs - assert kwargs["json"]["version"] == "0.3.6" + assert kwargs["json"]["version"] == "0.3.7" def test_post_register_version_overridable_via_env(