diff --git a/tests/unit/python/utils/update/test_source.py b/tests/unit/python/utils/update/test_source.py index f5ba65c76a..e9bbeeadf6 100644 --- a/tests/unit/python/utils/update/test_source.py +++ b/tests/unit/python/utils/update/test_source.py @@ -110,6 +110,35 @@ bundle: pattern: 'part (v[0-9.]+-v[0-9.]+)' """ +SUPPRESSED = """\ +--- +first: + image: example/first + version: "v1.0.0" + update: + type: npm + package: first + security: + suppressions: + # nocheck: cve stale tag + - image_tag: "v1.0.0" + until: 2026-11-30 + # nocheck: cve accepted + - id: CVE-2026-1234 + until: 2026-11-30 +second: + image: example/second + version: "v2.0.0" + update: + type: npm + package: second + security: + suppressions: + # nocheck: cve stale tag + - image_tag: "v2.0.0" + until: 2026-11-30 +""" + def _repo(services: str = SERVICES) -> Path: tmp = Path(tempfile.mkdtemp()) @@ -198,6 +227,32 @@ class TestOutdated(unittest.TestCase): self.assertEqual(module.outdated([self._entry(root)], root), []) +class TestSuppressions(unittest.TestCase): + def test_a_moved_version_drops_its_image_tag_suppressions_and_every_pin_moves( + self, + ) -> None: + root = _repo(SUPPRESSED) + latest = {"first": "v1.1.0", "second": "v2.1.0"} + with ( + mock.patch.object(module, "manifest_exists", return_value=True), + mock.patch.object( + module, + "candidates", + side_effect=lambda entry, _: [latest[entry.entity]], + ), + ): + updates = module.outdated(module.collect_entries(root), root) + module.apply_updates(updates) + + config = root / "roles" / "web-app-example" / ROLE_FILE_META_SERVICES + written = config.read_text() # nocheck: cache-read just rewritten here + self.assertIn('version: "v1.1.0"', written) + self.assertIn('version: "v2.1.0"', written) + self.assertNotIn("image_tag", written) + self.assertIn("id: CVE-2026-1234", written) + self.assertEqual(written.count("security:"), 1) + + class TestTemplate(unittest.TestCase): def _outdated(self, services: str, upstream: str) -> list[str]: root = _repo(services) diff --git a/utils/update/source.py b/utils/update/source.py index c8eabb8949..409edc2c4f 100644 --- a/utils/update/source.py +++ b/utils/update/source.py @@ -42,6 +42,7 @@ from utils.cache.files import read_text from utils.cache.yaml import load_yaml from utils.docker.registry import manifest_exists from utils.roles.mapping import ROLE_FILE_META_SERVICES +from utils.security.suppressions import drop_image_tag_entries from utils.update import checksum from utils.update.addons import GITHUB_RELEASES_CATALOG, iter_addon_files from utils.update.base import ( @@ -475,6 +476,7 @@ def bump_archive(line: str, current: str, latest: str) -> str: def apply_updates(updates: Iterable[VersionSourceUpdate]) -> list[Path]: """Rewrite every pin to its newer version and return the changed files.""" changed: list[Path] = [] + moved: dict[Path, list[str]] = {} for update in updates: path = update.entry.config_path lines = path.read_text( # nocheck: cache-read read-modify-write: a cached read serves the pre-write content when two pins of one file are bumped in the same run @@ -496,4 +498,13 @@ def apply_updates(updates: Iterable[VersionSourceUpdate]) -> list[Path]: path.write_text("".join(lines), encoding="utf-8") if path not in changed: changed.append(path) + if not update.entry.addon and update.entry.key == DEFAULT_KEY: + moved.setdefault(path, []).append(update.entry.entity) + for path, services in moved.items(): + lines = path.read_text( # nocheck: cache-read read-modify-write of the file the loop above just rewrote + encoding="utf-8" + ).splitlines(keepends=True) + for service in services: + lines = drop_image_tag_entries(lines, service) + path.write_text("".join(lines), encoding="utf-8") return changed