diff --git a/roles/sys-ctl-hlth-runtime/files/python/script.py b/roles/sys-ctl-hlth-runtime/files/python/script.py index 76149e5cbd..7b3c827841 100755 --- a/roles/sys-ctl-hlth-runtime/files/python/script.py +++ b/roles/sys-ctl-hlth-runtime/files/python/script.py @@ -1527,10 +1527,13 @@ def trivy_os_args( names an OS release of :attr:`ScanConfig.trivy_unmatched_os` whose ``until`` has not passed, else ``[]``. """ - cyclonedx = sbom_paths(layout, target)[1] - if "grype" not in scanners or not cyclonedx.is_file(): + if "grype" not in scanners: return [] - release = sbom_os(_read_json(cyclonedx)) + try: + cyclonedx = _read_json(sbom_paths(layout, target)[1]) + except UNREADABLE_JSON: + return [] + release = sbom_os(cyclonedx) for entry in config.trivy_unmatched_os: if (entry.name, entry.version) == release and today <= entry.until: print( @@ -1930,6 +1933,21 @@ def _read_json(path: Path) -> dict: return json.loads(path.read_text(encoding="utf-8")) +UNREADABLE_JSON = (OSError, ValueError, RecursionError) + + +def _readable_json(path: Path) -> bool: + """ + Returns: + whether ``path`` holds a whole JSON document + """ + try: + _read_json(path) + except UNREADABLE_JSON: + return False + return True + + def missing_databases(engine: str, databases: Path) -> list[str]: """Name every file of a provided database that is not there.""" return [ @@ -2030,7 +2048,7 @@ def scan( target for target in targets if target.kind == "host" - or not all(path.is_file() for path in sbom_paths(layout, target)) + or not all(_readable_json(path) for path in sbom_paths(layout, target)) ] results = run_lanes( [[sbom_step(layout, images[SBOM_BUILDER], target)] for target in pending], diff --git a/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py b/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py index 7b12aa9089..d366c9ab0e 100644 --- a/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py +++ b/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py @@ -1428,6 +1428,34 @@ class TestScan(TestCase): (["syft container quay.io/centos/centos:stream10: exited 2: boom"], False), ) + def test_an_sbom_cut_off_mid_write_is_built_again(self): + self.targets = [CENTOS_TARGET] + syft, cyclonedx = SCRIPT.sbom_paths(self.layout, CENTOS_TARGET) + syft.write_text("", encoding="utf-8") + cyclonedx.write_text(json.dumps(CENTOS_10), encoding="utf-8") + with contextlib.redirect_stdout(io.StringIO()): + _findings, errors, runner = self._scan(_config(targets=["container"]), {}) + self.assertEqual(errors, []) + self.assertTrue(any("docker:sha256:abc" in call for call in runner.calls)) + + def test_without_syft_an_unreadable_sbom_keeps_the_os_packages_in_trivy(self): + self.targets = [CENTOS_TARGET] + for name, content in (("empty", ""), ("too deep", "[" * 100_000)): + with self.subTest(name): + SCRIPT.sbom_paths(self.layout, CENTOS_TARGET)[1].write_text( + content, encoding="utf-8" + ) + _findings, errors, runner = self._scan( + _config( + targets=["container"], + engines=["trivy", "grype"], + trivy_unmatched_os=UNMATCHED_OS, + ), + {}, + ) + (trivy,) = [call for call in runner.calls if "--image-src" in call] + self.assertEqual((errors, "--pkg-types" in trivy), ([], False)) + def test_a_failed_sbom_skips_the_sbom_readers_of_that_target(self): _findings, errors, runner = self._scan( _config(targets=["host"], engines=["syft", "grype"]),