fix(ci): resolve security findings and improve Trivy reporting

This commit is contained in:
TakuyaYagam1 committed 2026-10-05 17:30:21 +03:00
1 parent 138666a7bb
commit e9f56a6aa2
9 files changed
+110 -12

No files matched your search

+77
View File
@@ -98,6 +98,83 @@ class SecurityChecksTest(unittest.TestCase):
(reports / "govulncheck.txt").read_text(), "Synthetic scan result\n"
)
def test_trivy_reports_are_visible_without_masking_failures(self) -> None:
cases = [
("clean", 0, 0, True, True),
("findings", 1, 0, True, False),
("scan_error", 17, 0, False, False),
("conversion_error", 0, 7, True, False),
("both_errors", 17, 7, True, False),
("missing_report", 0, 0, False, False),
]
docker = self.folder / "docker"
docker.write_text(
"#!/usr/bin/env bash\n"
'if [ "$1" = build ]; then exit 0; fi\n'
'if [ "$1" = image ]; then echo sha256:fixture; exit 0; fi\n'
"exit 2\n"
)
docker.chmod(0o700)
for target in ("filesystem", "images"):
for name, scan_code, convert_code, write_report, success in cases:
with self.subTest(target=target, case=name):
reports = self.folder / f"{target}-{name}"
scanner = self.folder / "trivy"
scanner.write_text(
f"#!{sys.executable}\n"
"import json, sys\n"
"from pathlib import Path\n"
"args = sys.argv[1:]\n"
"if '--version' in args: sys.exit(0)\n"
"if args[0] == 'convert':\n"
" assert args[1:3] == ['--format', 'table']\n"
" assert args[3:5] == ['--scanners', 'vuln,misconfig']\n"
" report = json.loads(Path(args[-1]).read_text())\n"
f" if {convert_code}:\n"
" print('Report conversion failed', file=sys.stderr)\n"
f" sys.exit({convert_code})\n"
" print('Scan table:', report['result'])\n"
" sys.exit(0)\n"
"assert args[0] in ('fs', 'image')\n"
f"if {write_report}:\n"
" output = Path(args[args.index('--output') + 1])\n"
" output.write_text(json.dumps({'result': "
f"{'HIGH CVE-2099-0001' if scan_code else 'No findings'!r}}}))\n"
f"sys.exit({scan_code})\n"
)
scanner.chmod(0o700)
result = subprocess.run(
[
"make",
target,
f"TRIVY={scanner}",
f"DOCKER={docker}",
f"REPORTS={reports}",
],
cwd=SECURITY,
capture_output=True,
text=True,
check=False,
)
self.assertEqual(result.returncode == 0, success, result.stderr)
report_count = 1 if target == "filesystem" else 3
self.assertEqual(
len(list(reports.glob("trivy-*.json"))),
report_count if write_report else 0,
)
if write_report and not convert_code:
self.assertEqual(result.stdout.count("Scan table:"), report_count)
self.assertIn(
"HIGH CVE-2099-0001" if scan_code else "No findings",
result.stdout,
)
else:
self.assertNotIn("Scan table:", result.stdout)
if write_report and convert_code:
self.assertIn("Report conversion failed", result.stderr)
if scan_code and target == "filesystem":
self.assertIn(f"Error {scan_code}", result.stderr)
def test_failed_preflight_removes_previous_scan_reports(self) -> None:
outputs = {
"filesystem": ["trivy-filesystem.json"],