Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ns8-module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 54 additions & 5 deletions packaging/ns8/assert-multiarc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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},
Expand All @@ -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)}"},
Expand Down
121 changes: 121 additions & 0 deletions packaging/ns8/test_assert_multiarc.py
Original file line number Diff line number Diff line change
@@ -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 ""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src is assigned and then deleted without ever being read, and inspect is reached through an inline __import__("inspect") twice when the module already imports four other stdlib modules at the top. This is the one test that enforces "the three paths cannot drift", so it should read as the deliberate check it is. Add import inspect to the import block and delete the dead pair.

Technical details
# Tidy test_every_fetch_path_normalises

## Affected sites
- packaging/ns8/test_assert_multiarc.py:17-20 — import block, needs `import inspect`
- packaging/ns8/test_assert_multiarc.py:76-78 — `src = ...` / `del src` dead pair
- packaging/ns8/test_assert_multiarc.py:79 — second `__import__("inspect")`

## Required outcome
- `inspect` is imported at module level, in the existing stdlib group.
- The loop body is the single `assertIn` against `inspect.getsource(...)`.
- No assigned-then-deleted local remains.

## Do not change
- The assertion itself. `inspect.getsource` is the right tool here: the
  invariant is "every fetch path normalises", which is a property of the source
  text, and a behavioural test would need the network the offline suite
  deliberately avoids.
Suggested change
src = getattr(am, name).__doc__ or ""
self.assertIn("normalize_repo", inspect.getsource(getattr(am, name)))

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()
Loading