add container names

This commit is contained in:
David Duran Rosich committed 2026-10-23 22:47:04 +02:00
1 parent f6e723a63e
commit 3003585a04
3 files changed
+93 -15

No files matched your search

+51 -13
View File
@@ -439,13 +439,31 @@ class ScanTarget:
image: str
containers: list[str] = field(default_factory=list)
@property
def container_names(self) -> str:
return ", ".join(self.containers)
@property
def container_label(self) -> str:
return "Container" if len(self.containers) == 1 else "Containers"
@property
def container_suffix(self) -> str:
if not self.containers:
return ""
if len(self.containers) == 1:
return f" (used by container {self.containers[0]})"
return f" (used by containers {', '.join(self.containers)})"
return f" (used by {self.container_label.lower()} {self.container_names})"
@property
def notification_context(self) -> str:
if not self.containers:
return ""
return f" [{self.container_label.lower()}: {self.container_names}]"
@property
def container_detail(self) -> str:
if not self.containers:
return ""
return f"{self.container_label}: {self.container_names}"
def collect_targets(settings: Settings) -> list[ScanTarget]:
@@ -609,9 +627,17 @@ def notify(settings: Settings, title: str, body: str) -> bool:
return True
def build_body(image: str, result: ScanResult, report_path: str, max_findings: int) -> str:
lines = [
f"Image: {image}",
def build_body(
image: str,
result: ScanResult,
report_path: str,
max_findings: int,
container_detail: str = "",
) -> str:
lines = [f"Image: {image}"]
if container_detail:
lines.append(container_detail)
lines += [
f"Critical: {result.critical} | High: {result.high} | "
f"Medium: {result.medium} | Low: {result.low}",
f"Fixable (critical/high): {result.fixable}",
@@ -664,11 +690,17 @@ def scan_image(target: ScanTarget, settings: Settings, stats: RunStats) -> None:
if status != 0:
stats.scan_errors += 1
if settings.notify_on_scan_error:
body = (
f"Image: {image}\n\n"
"Trivy could not complete the scan. Check the scanner logs for details.\n"
)
if not notify(settings, f"Scan failed: {image}", body):
lines = [f"Image: {image}"]
if target.container_detail:
lines.append(target.container_detail)
lines += [
"",
"Trivy could not complete the scan. Check the scanner logs for details.",
"",
]
body = "\n".join(lines)
title = f"Scan failed: {image}{target.notification_context}"
if not notify(settings, title, body):
stats.notify_errors += 1
return
@@ -693,10 +725,16 @@ def scan_image(target: ScanTarget, settings: Settings, stats: RunStats) -> None:
if notify_hits:
title = (
f"{settings.apprise_title_prefix}: {image} "
f"{settings.apprise_title_prefix}: {image}{target.notification_context} "
f"({result.critical} critical, {result.high} high)"
)
body = build_body(image, result, report_path, settings.max_findings)
body = build_body(
image,
result,
report_path,
settings.max_findings,
target.container_detail,
)
if not notify(settings, title, body):
stats.notify_errors += 1
else: