diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 762faff..7d06c68 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -91,6 +91,12 @@ jobs: ls -l /tmp/dist sha256sum /tmp/dist/*.tar.gz + # The archive is what consumers get: only the allow list of scripts/check_tarball.py. + - name: Check the archive contents + run: | + export PATH="/opt/venv/bin:$PATH" + python3 scripts/check_tarball.py /tmp/dist/mortis-kamino-*.tar.gz + # galaxy-importer without ansible-test and without Docker (ADR 0004, Collection # repo gates). The importer reads its configuration from this file. - name: Run galaxy-importer diff --git a/changelogs/fragments/collection-tarball-contents.yml b/changelogs/fragments/collection-tarball-contents.yml new file mode 100644 index 0000000..05d9ebe --- /dev/null +++ b/changelogs/fragments/collection-tarball-contents.yml @@ -0,0 +1,5 @@ +minor_changes: + - >- + The Collection archive no longer contains extensions/ (the image-smoke molecule scenario), + the roles' molecule scenarios, CLAUDE.md or other repository tooling; the Build workflow + now fails when the archive holds anything outside an allow list. diff --git a/galaxy.yml b/galaxy.yml index 7bb9fd3..1232eda 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -19,13 +19,27 @@ homepage: https://git.rkdr.net/mortis/kamino issues: https://git.rkdr.net/mortis/kamino/issues dependencies: {} build_ignore: + # Repository tooling that is not part of the Collection. scripts/check_tarball.py fails + # the build if anything outside its allowlist ends up in the archive, so a new top-level + # file is a decision: ignore it here or allow it there. + - .ansible + - .claude - .gitea - .gitignore - .pre-commit-config.yaml + - .reuse + - .venv - .yamllint - .ansible-lint + - CLAUDE.md - CONTRIBUTING.md + - HANDOFF.md + - extensions - images - scripts - requirements-dev.txt + - gitleaks-report.json + - importer_result.json + - tests/scripts + - 'roles/*/molecule' - '*.tar.gz' diff --git a/scripts/check_tarball.py b/scripts/check_tarball.py new file mode 100755 index 0000000..310e18b --- /dev/null +++ b/scripts/check_tarball.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +# SPDX-FileCopyrightText: Defame1297 and contributors +# SPDX-License-Identifier: GPL-3.0-or-later +"""Assert that a built Collection tarball holds only files meant for consumers. + + scripts/check_tarball.py dist/mortis-kamino-X.Y.Z.tar.gz + +galaxy.yml `build_ignore` is a deny list: a new file at the repository root (a tool's +cache, an editor's directory, an agent's settings) ships silently. This check is the +allow list. A top-level entry that is not in ALLOWED_TOP, or a path under an entry that +matches FORBIDDEN, fails the build. When the Collection gains a plugin type or a +directory consumers should get, add it to ALLOWED_TOP in the same change. +""" +import fnmatch +import sys +import tarfile + +ALLOWED_TOP = { + "COPYING", "FILES.json", "LICENSES", "MANIFEST.json", "NOTICE", "README.md", + "REUSE.toml", "changelogs", "docs", "meta", "plugins", "roles", "tests", +} +# Checked on every member, whatever its top-level directory. +FORBIDDEN = [ + "*/molecule/*", "*/molecule", "tests/scripts*", "*.pyc", "*__pycache__*", "*.tar.gz", + "*.pem", "*.key", "*.vault*", "*vault-pass*", "*/.git/*", "*.env", "*importer_result.json", +] + + +def problems(names): + found = [] + for name in sorted(names): + path = name.rstrip("/") + if path.startswith("./"): + path = path[2:] + if not path: + continue + top = path.split("/", 1)[0] + if top not in ALLOWED_TOP: + found.append(f"{path}: top-level entry '{top}' is not allowed in the Collection archive") + elif any(fnmatch.fnmatch(path, pat) for pat in FORBIDDEN): + found.append(f"{path}: matches a forbidden pattern") + return found + + +def main(argv): + if len(argv) != 2: + sys.stderr.write(__doc__) + return 2 + with tarfile.open(argv[1]) as tar: + names = tar.getnames() + bad = problems(names) + if bad: + sys.stderr.write("Unexpected files in the Collection archive:\n " + "\n ".join(bad) + "\n") + return 1 + print(f"{argv[1]}: {len(names)} entries, all allowed") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/tests/scripts/test_check_tarball.py b/tests/scripts/test_check_tarball.py new file mode 100644 index 0000000..d3eefdc --- /dev/null +++ b/tests/scripts/test_check_tarball.py @@ -0,0 +1,28 @@ +# SPDX-FileCopyrightText: Defame1297 and contributors +# SPDX-License-Identifier: GPL-3.0-or-later +"""Run with: python3 -m unittest discover -s tests/scripts""" +import pathlib +import sys +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2] / "scripts")) +import check_tarball # noqa: E402 + + +class TarballTest(unittest.TestCase): + def test_clean(self): + self.assertEqual(check_tarball.problems( + ["README.md", "roles/", "roles/a/tasks/main.yml", "LICENSES/MIT.txt", "tests/sanity/x.txt"]), []) + + def test_stray_top_level(self): + for name in (".venv/lib/a.py", "CLAUDE.md", ".claude/settings.json", "importer_result.json", "extensions/m.yml"): + self.assertEqual(len(check_tarball.problems([name])), 1, name) + + def test_forbidden_inside_allowed(self): + self.assertEqual(len(check_tarball.problems(["roles/a/molecule/default/molecule.yml"])), 1) + self.assertEqual(len(check_tarball.problems(["roles/a/files/k.pem"])), 1) + self.assertEqual(len(check_tarball.problems(["tests/scripts/t.py"])), 1) + + +if __name__ == "__main__": + unittest.main()