ARES - authorized red-team engagement automation with dashboard, campaign scope, module orchestration, OPSEC controls, encrypted vault, and reporting.
Python active-directorycloud-securitydefensive-validationfastapigood-first-issuemitre-attackopsecreactred-teamreportingsecurity
fix(performance): eliminate N+1 queries in export_backup() (AUD-005)
Mafifrizi546da222c751
diff --git a/ares/db/database.py b/ares/db/database.pyindex 775e0a3..3351b37 100644--- a/ares/db/database.py+++ b/ares/db/database.py@@ -1304,18 +1304,26 @@ class AresDatabase:async with self._conn.execute("SELECT * FROM campaigns ORDER BY created_at DESC") as cur:campaigns = [dict(r) for r in await cur.fetchall()]+ async with self._conn.execute("SELECT * FROM findings ORDER BY discovered_at DESC") as cur:+ all_findings = [dict(r) for r in await cur.fetchall()]++ async with self._conn.execute("SELECT * FROM hosts ORDER BY first_seen") as cur:+ all_hosts = [dict(r) for r in await cur.fetchall()]++ from collections import defaultdict++ findings_by_campaign = defaultdict(list)+ for f in all_findings:+ findings_by_campaign[f.get("campaign_id")].append(f)++ hosts_by_campaign = defaultdict(list)+ for h in all_hosts:+ hosts_by_campaign[h.get("campaign_id")].append(h)+for campaign in campaigns:cid = campaign["id"]- async with self._conn.execute(- "SELECT * FROM findings WHERE campaign_id=? ORDER BY discovered_at DESC",- (cid,),- ) as cur:- campaign["_findings"] = [dict(r) for r in await cur.fetchall()]- async with self._conn.execute(- "SELECT * FROM hosts WHERE campaign_id=? ORDER BY first_seen",- (cid,),- ) as cur:- campaign["_hosts"] = [dict(r) for r in await cur.fetchall()]+ campaign["_findings"] = findings_by_campaign.get(cid, [])+ campaign["_hosts"] = hosts_by_campaign.get(cid, [])export = {"export_version": "1.0",@@ -1329,6 +1337,9 @@ class AresDatabase:logger.info("db_export_complete", path=output_path, campaigns=len(campaigns))return output_path+ # Alias for export_json+ export_backup = export_json+async def close(self) -> None:async with self._lifecycle_lock:await self._close_locked()diff --git a/tests/unit/test_export_backup_performance_aud005.py b/tests/unit/test_export_backup_performance_aud005.pynew file mode 100644index 0000000..325c7f9--- /dev/null+++ b/tests/unit/test_export_backup_performance_aud005.py@@ -0,0 +1,124 @@+"""+Performance and verification tests for AUD-005:+Eliminating N+1 queries in AresDatabase.export_backup() / export_json().+Verifies that 100 campaigns are exported in exactly 3 batch queries instead of 201.+"""+import json+from pathlib import Path+import pytest++from ares.db.database import AresDatabase+++async def test_export_backup_executes_batch_queries_not_n_plus_one(tmp_path: Path):+ """+ Verify that export_backup() executes exactly 3 SQL queries+ regardless of the number of campaigns (tested with 100 campaigns).+ """+ db = AresDatabase(":memory:")+ await db.connect()++ try:+ # Seed 100 campaigns with findings and hosts+ campaign_count = 100+ for i in range(campaign_count):+ cid = f"camp-{i:03d}"+ # 1. Insert campaign+ await db._conn.execute(+ """+ INSERT INTO campaigns (id, name, operator, created_at, status)+ VALUES (?, ?, ?, datetime('now'), 'active')+ """,+ (cid, f"Campaign {i}", "operator_test"),+ )+ # 2. Insert 2 findings for this campaign+ await db._conn.execute(+ """+ INSERT INTO findings (id, campaign_id, title, description, severity, discovered_at, module_id)+ VALUES (?, ?, ?, 'Test description', 'high', datetime('now'), 'test.module')+ """,+ (f"fnd-{i:03d}-1", cid, f"Finding 1 for {cid}"),+ )+ await db._conn.execute(+ """+ INSERT INTO findings (id, campaign_id, title, description, severity, discovered_at, module_id)+ VALUES (?, ?, ?, 'Test description', 'medium', datetime('now'), 'test.module')+ """,+ (f"fnd-{i:03d}-2", cid, f"Finding 2 for {cid}"),+ )+ # 3. Insert 1 host for this campaign+ await db._conn.execute(+ """+ INSERT INTO hosts (id, campaign_id, ip_address, hostname, first_seen)+ VALUES (?, ?, ?, ?, datetime('now'))+ """,+ (f"host-{i:03d}", cid, f"10.0.{i // 256}.{i % 256}", f"host-{i}.corp"),+ )++ await db._conn.commit()++ # Intercept queries executed during export+ query_log = []+ original_execute = db._conn.execute++ def counting_execute(sql, *args, **kwargs):+ query_log.append(str(sql).strip())+ return original_execute(sql, *args, **kwargs)++ db._conn.execute = counting_execute++ export_file = str(tmp_path / "test_backup.json")+ res_path = await db.export_backup(output_path=export_file)++ # Revert intercepted execute+ db._conn.execute = original_execute++ # 1. Total queries must be exactly 3 (Campaigns, Findings, Hosts)+ # In N+1 architecture this would be 1 + 2*100 = 201 queries.+ assert len(query_log) == 3, f"Expected 3 queries, got {len(query_log)}: {query_log}"+ assert any("FROM campaigns" in q for q in query_log)+ assert any("FROM findings" in q for q in query_log)+ assert any("FROM hosts" in q for q in query_log)++ # 2. Verify output JSON structure and integrity+ assert Path(res_path).exists()+ with open(res_path, "r", encoding="utf-8") as fh:+ exported_data = json.load(fh)++ assert exported_data["export_version"] == "1.0"+ assert "schema_version" in exported_data+ assert "exported_at" in exported_data+ assert len(exported_data["campaigns"]) == 100++ # Verify findings and hosts grouped correctly+ for camp in exported_data["campaigns"]:+ cid = camp["id"]+ findings = camp.get("_findings", [])+ hosts = camp.get("_hosts", [])+ assert len(findings) == 2+ assert all(f["campaign_id"] == cid for f in findings)+ assert len(hosts) == 1+ assert hosts[0]["campaign_id"] == cid++ finally:+ await db.close()+++async def test_export_backup_empty_db_structure(tmp_path: Path):+ """Verify that export_backup on empty database still executes 3 queries and outputs empty list."""+ db = AresDatabase(":memory:")+ await db.connect()++ try:+ export_file = str(tmp_path / "empty_backup.json")+ res_path = await db.export_backup(output_path=export_file)++ with open(res_path, "r", encoding="utf-8") as fh:+ exported_data = json.load(fh)++ assert exported_data["export_version"] == "1.0"+ assert exported_data["campaigns"] == []+ finally:+ await db.close()