feat(scripts): deny the production image set in the Collection's workflows
The pins check now fails when any file under .gitea/workflows mentions git.rkdr.net/defame1297/, so the environment repo's production images can never appear in the Collection's CI. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
ec15abf400
commit
359e016307
2 files changed
+32
-1
No files matched your search
@@ -11,7 +11,10 @@ cannot make a tag immutable), so an image whose content changes under an unchang
|
||||
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.
|
||||
|
||||
pins Every `git.rkdr.net/mortis/<name>:<version>@sha256:<digest>` in the consumer
|
||||
pins No file under .gitea/workflows may mention `git.rkdr.net/defame1297/`: the production
|
||||
image set of the environment repo never belongs in this CI (the `mortis` side is the
|
||||
untrusted one, ADR 0004 Runner isolation).
|
||||
Every `git.rkdr.net/mortis/<name>:<version>@sha256:<digest>` in the consumer
|
||||
workflows must name an image of the matrix, with a well-formed digest and a
|
||||
version that is not newer than the matrix (a pin lags the matrix between the PR
|
||||
that bumps the version and the PR that moves the pin; it can never lead it).
|
||||
@@ -59,6 +62,9 @@ PIN_RE = re.compile(r"git\.rkdr\.net/mortis/(?P<name>[a-z0-9._-]+):(?P<version>[
|
||||
ANY_PIN_RE = re.compile(
|
||||
r"""(?<![@\w.-])(?<!http://)(?<!https://)git\.rkdr\.net(?::[0-9]+)?/mortis/[A-Za-z0-9._/-]+"""
|
||||
r"""(?:[:@][^\s,)\]}'"]*)?""", re.IGNORECASE)
|
||||
# The production image namespace of the environment repo; any spelling of the host's case.
|
||||
FOREIGN_RE = re.compile(r"git\.rkdr\.net(?::[0-9]+)?/defame1297/", re.IGNORECASE)
|
||||
WORKFLOW_DIR = ".gitea/workflows"
|
||||
# Semver 2.0.0: no leading zeros in numeric identifiers (core or prerelease).
|
||||
_NUM = r"(?:0|[1-9][0-9]*)"
|
||||
_PRE_ID = r"(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)"
|
||||
@@ -260,6 +266,13 @@ def changed_images(head_matrix, changed, read_head_file):
|
||||
return result
|
||||
|
||||
|
||||
def check_foreign_images(workflow_texts):
|
||||
"""Errors for each workflow file that mentions the production image namespace, comments included."""
|
||||
return [f"{fname}: mentions git.rkdr.net/defame1297/, the production image set; the Collection's CI "
|
||||
"must never use or name it" for fname, text in sorted(workflow_texts.items())
|
||||
if FOREIGN_RE.search(text)]
|
||||
|
||||
|
||||
def check_bump(base_matrix, head_matrix, changed, read_head_file):
|
||||
errors = []
|
||||
for name, hits in changed_images(head_matrix, changed, read_head_file).items():
|
||||
@@ -320,6 +333,8 @@ def run(argv):
|
||||
raise CheckError(f"{REQUIRED_CONSUMER} is missing: it runs this check and is a required consumer")
|
||||
texts = {f: pathlib.Path(f).read_text() for f in CONSUMERS if pathlib.Path(f).exists()}
|
||||
errors = check_pins(matrix, texts)
|
||||
workflows = sorted(pathlib.Path(WORKFLOW_DIR).glob("*.y*ml")) if pathlib.Path(WORKFLOW_DIR).is_dir() else []
|
||||
errors += check_foreign_images({str(f): f.read_text() for f in workflows})
|
||||
note = " (checked: " + ", ".join(texts) + ")"
|
||||
elif len(argv) == 3 and argv[1] == "bump":
|
||||
if not REF_RE.fullmatch(argv[2]):
|
||||
|
||||
@@ -73,6 +73,14 @@ class PureTest(unittest.TestCase):
|
||||
def matrix(self, mol="0.1.0", ci="0.2.0"):
|
||||
return civ.matrix_of(IMAGE_YML.format(mol=mol, ci=ci))
|
||||
|
||||
def test_foreign_production_images_are_denied(self):
|
||||
for text in ("image: git.rkdr.net/defame1297/gitea-ansible-runner:1@sha256:" + "a" * 64,
|
||||
"# GIT.RKDR.NET:443/Defame1297/x", "image: git.rkdr.net/defame1297/y"):
|
||||
self.assertEqual(len(civ.check_foreign_images({"w.yml": text})), 1, text)
|
||||
for text in ("image: git.rkdr.net/mortis/ci:0.2.0", "https://git.rkdr.net/defame1297x",
|
||||
"see Defame1297/ansible-homelab-mono"):
|
||||
self.assertEqual(civ.check_foreign_images({"w.yml": text}), [], text)
|
||||
|
||||
def test_pins_ok_and_lagging(self):
|
||||
pin = f"image: git.rkdr.net/mortis/ci:0.1.0@{DIGEST}\n"
|
||||
self.assertEqual(civ.check_pins(self.matrix(), {"lint.yml": pin}), [])
|
||||
@@ -243,6 +251,14 @@ class GitTest(unittest.TestCase):
|
||||
self.assertIn(text, err)
|
||||
self.assertEqual(len(err.strip().splitlines()), 1, err)
|
||||
|
||||
def test_pins_cli_denies_a_production_image_in_any_workflow(self):
|
||||
self.write(".gitea/workflows/lint.yml", "name: Lint\n")
|
||||
self.assertEqual(self.bump_args("pins")[0], 0)
|
||||
self.write(".gitea/workflows/other.yaml", "container: git.rkdr.net/defame1297/runner:1\n")
|
||||
code, err = self.bump_args("pins")
|
||||
self.assertEqual(code, 1)
|
||||
self.assertIn("other.yaml: mentions git.rkdr.net/defame1297/", err)
|
||||
|
||||
def test_moved_input_out_of_the_set_fails(self):
|
||||
run(self.repo, "git", "mv", "images/mol/Dockerfile", "elsewhere.dockerfile")
|
||||
self.commit("move out")
|
||||
|
||||
Reference in new issue
Block a user