diff --git a/.github/workflows/ns8-module.yml b/.github/workflows/ns8-module.yml index e5c5d4a8..b6c44de5 100644 --- a/.github/workflows/ns8-module.yml +++ b/.github/workflows/ns8-module.yml @@ -34,6 +34,11 @@ jobs: run: | python3 -m py_compile imageroot/actions/configure-module/20configure imageroot/actions/get-configuration/20read python3 -m py_compile assert-multiarc.py + python3 -m py_compile test_assert_multiarc.py + # Offline tests, so a broken invariant fails on every push rather than + # waiting for a release to notice. The registry round-trip tests in the + # same file are opt-in and skipped unless NS8_REGISTRY_TESTS=1. + python3 -m unittest discover -s . -p 'test_*.py' python3 -c "import json, glob; [json.load(open(f)) for f in glob.glob('imageroot/actions/*/*.json') + glob.glob('ui/public/*.json') + ['ui/public/i18n/en/translation.json']]" bash -n imageroot/actions/configure-module/80start_services bash -n build-images.sh diff --git a/packaging/ns8/assert-multiarc.py b/packaging/ns8/assert-multiarc.py index 533e7f80..c9b3f744 100644 --- a/packaging/ns8/assert-multiarc.py +++ b/packaging/ns8/assert-multiarc.py @@ -72,12 +72,59 @@ class Fail(Exception): """A check failed in a way worth reporting verbatim.""" +def normalize_repo(repo): + """Reduce any spelling of a repository to the one form this script uses. + + Two independent normalisations, because two independent things go wrong. + + 1. A leading registry host. This script builds absolute URLs itself, + `https://ghcr.io/v2/{repo}/...`, so a caller that passes + `ghcr.io/calnode/ns8-calnode` is supplying a host that does not belong in + that path at all. ghcr happens to tolerate the redundant segment, which + is exactly why it went unnoticed, but it does not tolerate it combined + with a capital: see the routing note below. + + Only the host this script actually talks to is stripped. An arbitrary + dotted first segment is NOT stripped, because a dotted owner is a legal + image name and `my.org/team/image` must survive intact. + + 2. Case. ghcr's token endpoint answers HTTP 400 for an uppercase scope, and + GitHub reports the owner as `github.repository_owner`, verbatim `Calnode`. + + ROUTING NOTE, measured against ghcr rather than assumed. Given a path with no + extra leading segment, the repository is matched case-insensitively: + + /v2/calnode/ns8-calnode/manifests/latest -> 200 + /v2/Calnode/ns8-calnode/manifests/latest -> 200 + + Given ONE extra leading segment, that segment is tolerated whatever it is, + and the segments after it are then matched case-sensitively: + + /v2/ghcr.io/calnode/ns8-calnode/manifests/latest -> 200 + /v2/quay.io/calnode/ns8-calnode/manifests/latest -> 200 + /v2/ghcr.io/Calnode/ns8-calnode/manifests/latest -> 404 + /v2/quay.io/Calnode/ns8-calnode/manifests/latest -> 404 + + Those 404s are GitHub's edge router, not the registry: the body is + `404 page not found` in text/plain, whereas a genuinely absent repository + returns a JSON MANIFEST_UNKNOWN. So the failure is routing, and it only + manifests when a caller supplies the redundant host. Which is precisely the + input this function removes. + + ns8-module.yml passes `ghcr.io/${{ github.repository_owner }}/ns8-calnode`, + so prefix plus capital-C, the one combination that fails, and that made main + red on every push while the image itself was present and multi-arch. + """ + repo = repo.strip() + host = urllib.parse.urlsplit(REGISTRY).netloc + prefix = host + "/" + if repo.lower().startswith(prefix.lower()): + repo = repo[len(prefix):] + return repo.lower() + + def token(repo): - # ghcr's token endpoint requires the repository path LOWERCASE and answers - # HTTP 400 otherwise. The owner arrives from GitHub as `github.repository_owner` - # verbatim, which is `Calnode` for this repo, so normalise here rather than - # trusting every caller to remember: a case slip here fails a correct release. - repo = repo.lower() + repo = normalize_repo(repo) if repo not in _tokens: url = f"{REGISTRY}/token?scope=repository:{repo}:pull&service=ghcr.io" with urllib.request.urlopen(url, timeout=30) as f: @@ -87,6 +134,7 @@ def token(repo): def get(repo, reference): """GET a manifest, returning (body, mediaType).""" + repo = normalize_repo(repo) req = urllib.request.Request( f"{REGISTRY}/v2/{repo}/manifests/{reference}", headers={"Authorization": f"Bearer {token(repo)}", "Accept": ACCEPT}, @@ -96,6 +144,7 @@ def get(repo, reference): def get_blob(repo, digest): + repo = normalize_repo(repo) req = urllib.request.Request( f"{REGISTRY}/v2/{repo}/blobs/{digest}", headers={"Authorization": f"Bearer {token(repo)}"}, diff --git a/packaging/ns8/test_assert_multiarc.py b/packaging/ns8/test_assert_multiarc.py new file mode 100644 index 00000000..58b8dc2c --- /dev/null +++ b/packaging/ns8/test_assert_multiarc.py @@ -0,0 +1,121 @@ +"""Tests for the repository-argument handling in assert-multiarc.py. + +Run offline, so they are safe to enforce on every push: + + python3 -m unittest discover -s packaging/ns8 -p 'test_*.py' -v + +The registry round-trip tests are opt-in, because CI should not depend on +ghcr's availability to decide whether a source change is valid: + + NS8_REGISTRY_TESTS=1 python3 -m unittest discover -s packaging/ns8 -p 'test_*.py' -v + +The four spellings below are the ones that matter. Three resolve and one does +not, and the one that does not is the exact string ns8-module.yml passes, which +made main red while the image itself was present and correct. +""" + +import importlib.util +import os +import pathlib +import unittest + +_SPEC = importlib.util.spec_from_file_location( + "assert_multiarc", + pathlib.Path(__file__).resolve().parent / "assert-multiarc.py", +) +am = importlib.util.module_from_spec(_SPEC) +_SPEC.loader.exec_module(am) + + +class NormalizeRepo(unittest.TestCase): + def test_the_four_spellings_agree(self): + """Every spelling of one repository must reduce to one canonical form. + + This is the regression. `ghcr.io/Calnode/ns8-calnode` used to 404 while + the other three resolved, because a redundant leading host segment makes + GitHub's edge router match the segments after it case-sensitively. + """ + forms = [ + "calnode/ns8-calnode", + "Calnode/ns8-calnode", + "ghcr.io/calnode/ns8-calnode", + "ghcr.io/Calnode/ns8-calnode", + ] + canonical = {am.normalize_repo(f) for f in forms} + self.assertEqual(canonical, {"calnode/ns8-calnode"}) + + def test_host_prefix_is_case_insensitive(self): + for host in ("ghcr.io", "GHCR.IO", "Ghcr.io"): + self.assertEqual( + am.normalize_repo(f"{host}/Calnode/ns8-calnode"), + "calnode/ns8-calnode", + ) + + def test_dotted_owner_is_not_mistaken_for_a_host(self): + """A dotted first segment is a legal owner, not a registry host. + + Stripping it would silently rewrite `my.org/team/image` into a different + repository, which is far worse than the 404 this function prevents. + """ + self.assertEqual(am.normalize_repo("my.org/team/image"), "my.org/team/image") + self.assertEqual(am.normalize_repo("My.Org/Team/Image"), "my.org/team/image") + + def test_two_segment_names_are_untouched_apart_from_case(self): + self.assertEqual(am.normalize_repo("Calnode/Ns8-Calnode"), "calnode/ns8-calnode") + + def test_whitespace_is_trimmed(self): + self.assertEqual(am.normalize_repo(" calnode/ns8-calnode\n"), "calnode/ns8-calnode") + + def test_normalisation_is_idempotent(self): + once = am.normalize_repo("ghcr.io/Calnode/ns8-calnode") + self.assertEqual(am.normalize_repo(once), once) + + def test_every_fetch_path_normalises(self): + """get(), get_blob() and token() must all agree, not just one of them.""" + for name in ("get", "get_blob"): + src = getattr(am, name).__doc__ or "" + self.assertIn("normalize_repo", __import__("inspect").getsource(getattr(am, name))) + del src + self.assertIn("normalize_repo", __import__("inspect").getsource(am.token)) + + +class RegistryRoundTrip(unittest.TestCase): + """Opt-in: proves the four spellings produce identical live outcomes.""" + + REPO = "calnode/ns8-calnode" + TAG = os.environ.get("NS8_REGISTRY_TEST_TAG", "latest") + + def setUp(self): + if not os.environ.get("NS8_REGISTRY_TESTS"): + self.skipTest("set NS8_REGISTRY_TESTS=1 to run against ghcr.io") + + def test_all_four_spellings_resolve_to_the_same_index(self): + bodies = {} + for form in ( + self.REPO, + "Calnode/ns8-calnode", + f"ghcr.io/{self.REPO}", + f"ghcr.io/Calnode/ns8-calnode", + ): + body, _ = am.get(form, self.TAG) + bodies[form] = body + digests = {form: body["manifests"][0]["digest"] for form, body in bodies.items()} + self.assertEqual( + len(set(digests.values())), + 1, + f"the four spellings resolved to different images: {digests}", + ) + + def test_every_spelling_passes_the_real_check(self): + for form in ( + self.REPO, + "Calnode/ns8-calnode", + f"ghcr.io/{self.REPO}", + f"ghcr.io/Calnode/ns8-calnode", + ): + with self.subTest(repo=form): + am.check_tag(form, self.TAG, ["amd64", "arm64"], False, None) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file