Skip to content

Commit 36e3109

Browse files
authored
[fix] Top 5 failing (#640)
1 parent c5717bf commit 36e3109

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

fixbackend/inventory/inventory_service.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,9 @@ async def issues_since(
589589
)
590590

591591
async def benchmark_summary(
592-
benmark_account_summaries: Dict[BenchmarkId, Dict[CloudAccountId, BenchmarkAccountSummary]]
593-
) -> Tuple[
594-
BenchmarkById, Dict[ReportSeverity, Set[SecurityCheckId]], Dict[SecurityCheckId, Set[BenchmarkId]]
595-
]:
592+
bench_account_summaries: Dict[BenchmarkId, Dict[CloudAccountId, BenchmarkAccountSummary]]
593+
) -> Tuple[BenchmarkById, Dict[SecurityCheckId, Set[BenchmarkId]]]:
596594
summaries: BenchmarkById = {}
597-
failed_checks_by_severity: Dict[ReportSeverity, Set[SecurityCheckId]] = defaultdict(set)
598595
benchmark_by_check_id: Dict[SecurityCheckId, Set[BenchmarkId]] = defaultdict(set)
599596
benchmarks = await self.client.benchmarks(db, short=True, with_checks=True)
600597
for b in benchmarks:
@@ -607,15 +604,13 @@ async def benchmark_summary(
607604
clouds=b["clouds"],
608605
description=b["description"],
609606
nr_of_checks=len(b["report_checks"]),
610-
account_summary=benmark_account_summaries.get(benchmark_id, {}),
607+
account_summary=bench_account_summaries.get(benchmark_id, {}),
611608
)
612609
summaries[summary.id] = summary
613610
for check in b["report_checks"]:
614611
check_id = SecurityCheckId(check["id"])
615-
severity = ReportSeverity(check["severity"])
616-
failed_checks_by_severity[severity].add(check_id)
617612
benchmark_by_check_id[check_id].add(benchmark_id)
618-
return summaries, failed_checks_by_severity, benchmark_by_check_id
613+
return summaries, benchmark_by_check_id
619614

620615
async def timeseries_infected() -> TimeSeries:
621616
start = now - timedelta(days=62 if is_free else 14)
@@ -628,13 +623,21 @@ async def timeseries_infected() -> TimeSeries:
628623
return TimeSeries(name="infected_resources", start=start, end=now, granularity=granularity, data=data)
629624

630625
async def top_issues(
631-
checks_by_severity: Dict[ReportSeverity, Set[SecurityCheckId]],
632626
benchmark_by_check_id: Dict[SecurityCheckId, Set[BenchmarkId]],
633627
benchmarks: Dict[BenchmarkId, BenchmarkSummary],
634628
num: int,
635629
) -> List[Json]:
636-
check_ids = dict_values_by(checks_by_severity, lambda x: ReportSeverityPriority[x])
637-
top = list(islice(check_ids, num))
630+
query = (
631+
"aggregate(/security.issues[*].check, /security.issues[*].severity: sum(1) as count): "
632+
"/security.has_issues==true"
633+
)
634+
async with self.client.aggregate(db, query) as ctx:
635+
all_failing = sorted(
636+
[e async for e in ctx],
637+
key=lambda x: (ReportSeverityPriority[x["group"]["severity"]], x["count"]),
638+
reverse=True,
639+
)
640+
top = list(islice((a["group"]["check"] for a in all_failing), num))
638641
checks = await self.client.checks(db, check_ids=top)
639642
for check in checks:
640643
check["benchmarks"] = [
@@ -657,7 +660,7 @@ def overall_score(
657660
return total_score // total_accounts if total_accounts > 0 else 100
658661

659662
(
660-
(benchmarks, failed_checks_by_severity, benchmark_by_check_id),
663+
(benchmarks, benchmark_by_check_id),
661664
vulnerable_changed,
662665
compliant_changed,
663666
infected_resources_ts,
@@ -669,7 +672,7 @@ def overall_score(
669672
)
670673

671674
# get issues for the top 5 issue_ids
672-
tops = await top_issues(failed_checks_by_severity, benchmark_by_check_id, benchmarks, num=5)
675+
tops = await top_issues(benchmark_by_check_id, benchmarks, num=5)
673676

674677
# sort top changed account by score
675678
vulnerable_changed.accounts_selection.sort(

tests/fixbackend/inventory/inventory_service_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ async def mock(request: Request) -> Response:
130130
)
131131
elif request.url.path == "/graph/fix/search/list" and content.startswith("search is(account) and /metadata.exported_at>="): # fmt: skip
132132
return nd_json_response(accounts_json)
133-
elif request.url.path == "/graph/fix/search/aggregate" and content.startswith("search /security.has_issues==true"): # fmt: skip
133+
elif request.url.path == "/graph/fix/search/aggregate" and "/security.has_issues==true" in content: # fmt: skip
134134
return nd_json_response(
135-
[{"group": {"check_id": "aws_c1", "severity": "low", "account_id": "123", "account_name": "t1", "cloud": "aws"}, "count": 8}, # fmt: skip
136-
{"group": {"check_id": "gcp_c2", "severity": "critical", "account_id": "234", "account_name": "t2", "cloud": "gcp"}, "count": 2}] # fmt: skip
135+
[{"group": {"check": "aws_c1", "severity": "low"}, "count": 8}, # fmt: skip
136+
{"group": {"check": "gcp_c2", "severity": "critical"}, "count": 2}] # fmt: skip
137137
)
138138
elif request.url.path == "/graph/fix/node/some_node_id":
139139
return json_response(azure_virtual_machine_resource_json)

0 commit comments

Comments
 (0)