Commit af0d486
committed
fix(container-scanner): handle empty/malformed grype output without traceback
Three failure modes in the container sub-scanner runners produced
either a JSONDecodeError traceback or a silent downgrade of scanner
coverage:
1. Grype writes a 0-byte ``grype-results.json`` (common when its
catalog/image-resolution step fails after the output handle is
created). ``output_file.exists()`` is True, so the existing
"no output" guard skipped, and ``json.loads("")`` blew up with a
bare JSONDecodeError that propagated to the user.
2. Grype's ``except Exception: logger.exception(...); return []``
path swallowed the error after dumping a Python traceback to
stderr — engine never recorded ``scanner_errors["grype"]``,
summary reported "Grype: 0 findings" as if the scan succeeded.
3. Trivy had a similar shape: ``logger.exception`` on parse failure
spammed a traceback even though the runtime contract was correct.
Fix
- New shared helper ``_validate_scanner_output(scanner_name,
output_file, result)`` validates in order: returncode == 0, file
exists, file size > 0. On any failure, logs at ERROR level
(clipped stderr, no traceback) and raises a single
``RuntimeError(f"<scanner> scan failed (exit N): <stderr>")``
shape so the orchestrator records it under ``scanner_errors``
consistently.
- ``_run_grype`` and ``_run_trivy`` both call the helper after
their subprocess returns, then translate JSON parse errors into
the same ``RuntimeError`` shape (instead of bare JSONDecodeError
or silent ``return []``).
- ``logger.exception(...)`` → ``logger.error(...)`` on parse
failure paths so users don't see a Python traceback for an
expected scanner failure mode.
Behavior preserved
- Healthy scans (zero exit, JSON-parseable output) take the same
fast path through the parser.
- The orchestrator's existing try/except around ``_run_grype`` /
``_run_trivy`` already catches ``RuntimeError`` and records it
under ``scanner_errors``; this PR makes the runners *actually*
raise that shape instead of silencing failures.
- Trivy still propagates findings when grype fails, and vice versa
— partial scan coverage is preserved while the failed scanner is
surfaced separately.
Tests (+13)
- ``TestValidateScannerOutput`` (6 cases): helper acceptance matrix —
healthy run silent, non-zero exit raises with stderr breadcrumb,
missing file raises, 0-byte file raises, stderr+0-byte combo
raises with breadcrumb, short stderr preserved verbatim.
- ``TestRunGrype`` (4 cases, the user's exact acceptance matrix):
non-zero exit + empty file, zero exit + empty file, malformed
JSON, valid JSON happy path.
- ``TestRunTrivy`` (2 cases): mirror coverage — same failure-mode
contract via the shared validator.
- ``TestOrchestratorRecordsScannerError`` (1 case): end-to-end
assertion that grype's RuntimeError flows up to
``scanner_errors["grype"]`` AND trivy's findings still propagate
in the same scan. Closes the loop on "preserve partial results
from other scanners but clearly surface the failure."
Validation
- Full SDK suite: 1451 passed (+13 from this PR), 8 skipped.1 parent 267e4be commit af0d486
2 files changed
Lines changed: 398 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
232 | 233 | | |
233 | 234 | | |
234 | 235 | | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
235 | 300 | | |
236 | 301 | | |
237 | 302 | | |
| |||
318 | 383 | | |
319 | 384 | | |
320 | 385 | | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
| 386 | + | |
330 | 387 | | |
331 | 388 | | |
332 | 389 | | |
333 | | - | |
334 | | - | |
335 | | - | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
336 | 401 | | |
337 | 402 | | |
338 | 403 | | |
| |||
407 | 472 | | |
408 | 473 | | |
409 | 474 | | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
| 475 | + | |
419 | 476 | | |
420 | 477 | | |
421 | 478 | | |
422 | | - | |
423 | | - | |
424 | | - | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
425 | 495 | | |
426 | 496 | | |
427 | 497 | | |
| |||
0 commit comments