mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 14:30:52 +00:00
Merge commit 'c57939d717dff40909102d3e091be7593fcdfa2b' into fix/codescan/python-hygiene
This commit is contained in:
80 files changed
+1979
-202
No files matched your search
@@ -1,6 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import datetime as dt
|
||||
import io
|
||||
import json
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
@@ -118,7 +120,7 @@ class TestSteps(TestCase):
|
||||
self.assertNotIn("/var/run/docker.sock:/var/run/docker.sock:ro", step.argv)
|
||||
|
||||
def test_trivy_reads_the_sbom_tree(self):
|
||||
step = scan_main.trivy_step(P, self.layout, self.target)
|
||||
step = scan_main.trivy_step(P, self.layout, self.target, [])
|
||||
self.assertEqual(step.argv[step.argv.index("sbom") :][-1], "/sbom/abc.cdx.json")
|
||||
|
||||
|
||||
@@ -190,7 +192,7 @@ class TestReport(TestCase):
|
||||
[("r", "s", "CVE-2026-109618")],
|
||||
)
|
||||
|
||||
def test_a_finding_the_role_only_warns_on_still_fails_the_mirror_scan(self):
|
||||
def test_a_finding_the_role_only_warns_on_is_still_reported(self):
|
||||
verdict = P.Verdict(
|
||||
self._finding("HIGH"), self.target, ("r", "s"), None, False, "warn"
|
||||
)
|
||||
@@ -202,8 +204,59 @@ class TestReport(TestCase):
|
||||
self.assertLessEqual(set(config.severity.values()), set(P.ACTIONS))
|
||||
|
||||
|
||||
class TestExitCode(TestCase):
|
||||
def _run(self, errors):
|
||||
finding = P.Finding(
|
||||
"grype",
|
||||
"abc",
|
||||
"php-cli",
|
||||
"8.4.26",
|
||||
"8.4.27",
|
||||
("CVE-2026-109618",),
|
||||
"CRITICAL",
|
||||
)
|
||||
p = mock.MagicMock()
|
||||
p.verdicts.return_value = [
|
||||
P.Verdict(finding, TestReport.target, ("r", "s"), None, False, "fail")
|
||||
]
|
||||
with (
|
||||
tempfile.TemporaryDirectory() as tmp,
|
||||
mock.patch.object(scan_main, "pipeline", return_value=p),
|
||||
mock.patch.object(
|
||||
scan_main, "scan_config", return_value=SimpleNamespace(workers=1)
|
||||
),
|
||||
mock.patch.object(scan_main, "iter_role_images", return_value=[]),
|
||||
mock.patch.object(
|
||||
scan_main,
|
||||
"resolve_digests",
|
||||
return_value=({}, MIRROR_SOURCES, list(errors)),
|
||||
),
|
||||
mock.patch.object(scan_main, "targets_of", return_value=[]),
|
||||
mock.patch.object(scan_main, "user_runtime", return_value=("docker",)),
|
||||
mock.patch.object(scan_main, "scan_tree", return_value=([], [])),
|
||||
mock.patch.object(scan_main, "suppressions_of", return_value={}),
|
||||
mock.patch("builtins.print"),
|
||||
):
|
||||
return scan_main.main(
|
||||
[
|
||||
"--cache-dir",
|
||||
f"{tmp}/cache",
|
||||
"--report",
|
||||
f"{tmp}/report.json",
|
||||
"--workers",
|
||||
"1",
|
||||
]
|
||||
)
|
||||
|
||||
def test_an_unsuppressed_finding_alone_passes(self):
|
||||
self.assertEqual(self._run([]), 0)
|
||||
|
||||
def test_an_engine_error_fails(self):
|
||||
self.assertEqual(self._run(["trivy img: exited 1"]), 1)
|
||||
|
||||
|
||||
class TestScanTree(TestCase):
|
||||
def test_a_cached_sbom_is_not_rebuilt(self):
|
||||
def _scan_tree(self, cyclonedx, config):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
for name in ("db", "sbom", "run"):
|
||||
@@ -225,7 +278,7 @@ class TestScanTree(TestCase):
|
||||
)
|
||||
(root / "sbom" / "abc.syft.json").write_text("{}", encoding="utf-8")
|
||||
(root / "sbom" / "abc.cdx.json").write_text(
|
||||
json.dumps({"components": []}), encoding="utf-8"
|
||||
json.dumps(cyclonedx), encoding="utf-8"
|
||||
)
|
||||
calls = []
|
||||
|
||||
@@ -233,14 +286,41 @@ class TestScanTree(TestCase):
|
||||
calls.append(list(argv))
|
||||
return SimpleNamespace(returncode=0, stdout="", stderr="")
|
||||
|
||||
config = scan_main.scan_config(P, 1)
|
||||
scan_main.scan_tree(
|
||||
P, [target], {"sha256:abc": "m"}, config, layout, runner
|
||||
)
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
scan_main.scan_tree(
|
||||
P, [target], {"sha256:abc": "m"}, config, layout, runner
|
||||
)
|
||||
return calls
|
||||
|
||||
def test_a_cached_sbom_is_not_rebuilt(self):
|
||||
calls = self._scan_tree({"components": []}, scan_main.scan_config(P, 1))
|
||||
self.assertFalse(
|
||||
any(any(arg.startswith("registry:") for arg in call) for call in calls)
|
||||
)
|
||||
|
||||
def test_a_centos_10_sbom_runs_trivy_on_its_library_packages_only(self):
|
||||
config = P.scan_config(
|
||||
{
|
||||
"targets": ["container"],
|
||||
"engines": ["trivy", "grype", "osv", "syft"],
|
||||
"severity": {"CRITICAL": "fail"},
|
||||
"fixed_only": True,
|
||||
"workers": 1,
|
||||
"db_update": True,
|
||||
"trivy_unmatched_os": [
|
||||
{"name": "centos", "version": "10", "until": "9999-12-31"}
|
||||
],
|
||||
}
|
||||
)
|
||||
cyclonedx = {
|
||||
"components": [
|
||||
{"type": "operating-system", "name": "centos", "version": "10"}
|
||||
]
|
||||
}
|
||||
calls = self._scan_tree(cyclonedx, config)
|
||||
(trivy,) = [call for call in calls if "/sbom/abc.cdx.json" in call]
|
||||
self.assertEqual(trivy[trivy.index("--pkg-types") + 1], "library")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user