-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_finding_query_runtime.py
More file actions
230 lines (170 loc) · 6.83 KB
/
Copy pathtest_finding_query_runtime.py
File metadata and controls
230 lines (170 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""Tests for finding query runtime orchestration."""
from __future__ import annotations
from security_scanner.core.finding.model import Disposition, Finding, Verdict
from security_scanner.runtime.finding_query import (
FindingQueryRequest,
read_findings,
)
from security_scanner.storage.adapters.nosql_db.transport import DynamoDbCompatibleConfig
def _finding(line_start: int, verdict: str) -> Finding:
return Finding.create(
repo_full_name="fake-org/fake-repo",
rule_id="aws-access-key-id",
file_path="config/settings.py",
line_start=line_start,
raw_secret="AKIAFAKEEXAMPLE000000",
source_tool="gitleaks",
scan_run_id="scan_abc12345",
rule_pack_version="secret-rules-0.1.0",
triage_verdict=verdict,
)
def test_read_findings_reads_all_when_scan_run_id_absent():
"""Query runtime reads the full store when no scan-run filter is requested."""
findings = [object()]
class FakeReader:
def __init__(self) -> None:
self.calls: list[tuple[str, str | None]] = []
def read_all(self):
self.calls.append(("read_all", None))
return findings
def read_for_scan_run(self, scan_run_id):
self.calls.append(("read_for_scan_run", scan_run_id))
return []
reader = FakeReader()
result = read_findings(
FindingQueryRequest(storage_backend="jsonl", jsonl_path="findings.jsonl"),
store=reader,
)
assert result == findings
assert reader.calls == [("read_all", None)]
def test_read_findings_uses_scan_run_filter_when_present():
"""Query runtime delegates scan-run filtering to the storage reader seam."""
findings = [object()]
class FakeReader:
def __init__(self) -> None:
self.scan_run_ids: list[str] = []
def read_all(self):
raise AssertionError("scan-run query should not call read_all")
def read_for_scan_run(self, scan_run_id):
self.scan_run_ids.append(scan_run_id)
return findings
reader = FakeReader()
result = read_findings(
FindingQueryRequest(
storage_backend="dynamodb",
scan_run_id="scan_test0001",
dynamodb_config=DynamoDbCompatibleConfig(table_name="SecurityScannerLocal"),
),
store=reader,
)
assert result == findings
assert reader.scan_run_ids == ["scan_test0001"]
def test_read_findings_builds_jsonl_reader_from_factory():
"""JSONL query requests pass the findings path to the storage factory."""
findings = [object()]
calls = []
class FakeReader:
def read_all(self):
return findings
def read_for_scan_run(self, scan_run_id):
raise AssertionError(scan_run_id)
def fake_store_factory(backend, **kwargs):
calls.append((backend, kwargs))
return FakeReader()
result = read_findings(
FindingQueryRequest(storage_backend="jsonl", jsonl_path="custom.jsonl"),
store_factory=fake_store_factory,
)
assert result == findings
assert calls == [("jsonl", {"jsonl_path": "custom.jsonl"})]
def test_read_findings_builds_nosql_reader_from_factory():
"""DynamoDB-compatible query requests pass explicit config to the factory."""
findings = [object()]
config = DynamoDbCompatibleConfig(table_name="SecurityScannerLocal")
calls = []
class FakeReader:
def read_all(self):
return findings
def read_for_scan_run(self, scan_run_id):
raise AssertionError(scan_run_id)
def fake_store_factory(backend, **kwargs):
calls.append((backend, kwargs))
return FakeReader()
result = read_findings(
FindingQueryRequest(storage_backend="dynamodb", dynamodb_config=config),
store_factory=fake_store_factory,
)
assert result == findings
assert calls == [("dynamodb", {"dynamodb_config": config})]
def test_read_findings_normalizes_jsonl_backend_alias_for_factory():
findings = [object()]
calls = []
class FakeReader:
def read_all(self):
return findings
def read_for_scan_run(self, scan_run_id):
raise AssertionError(scan_run_id)
def fake_store_factory(backend, **kwargs):
calls.append((backend, kwargs))
return FakeReader()
result = read_findings(
FindingQueryRequest(storage_backend="JSONL", jsonl_path="custom.jsonl"),
store_factory=fake_store_factory,
)
assert result == findings
assert calls == [("jsonl", {"jsonl_path": "custom.jsonl"})]
def test_read_findings_normalizes_nosql_backend_alias_for_factory():
findings = [object()]
calls = []
class FakeReader:
def read_all(self):
return findings
def read_for_scan_run(self, scan_run_id):
raise AssertionError(scan_run_id)
def fake_store_factory(backend, **kwargs):
calls.append((backend, kwargs))
return FakeReader()
result = read_findings(
FindingQueryRequest(storage_backend="dynamodb_compatible"),
store_factory=fake_store_factory,
)
assert result == findings
assert calls == [("dynamodb-compatible", {"dynamodb_config": None})]
def test_read_findings_filters_by_disposition_when_requested():
"""The read-API seam slices findings by disposition for the dashboard (FR-11)."""
unreviewed = _finding(1, Verdict.NEEDS_REVIEW.value)
verified = _finding(2, Verdict.TRUE_POSITIVE.value)
false_positive = _finding(3, Verdict.FALSE_POSITIVE.value)
class FakeReader:
def read_all(self):
return [unreviewed, verified, false_positive]
def read_for_scan_run(self, scan_run_id):
raise AssertionError(scan_run_id)
result = read_findings(
FindingQueryRequest(
storage_backend="jsonl",
jsonl_path="findings.jsonl",
dispositions=[Disposition.UNREVIEWED.value, Disposition.VERIFIED.value],
),
store=FakeReader(),
)
assert result == [unreviewed, verified]
def test_read_findings_disposition_filter_applies_to_scan_run_reads():
"""Disposition filtering composes with the scan-run filter."""
verified = _finding(2, Verdict.TRUE_POSITIVE.value)
false_positive = _finding(3, Verdict.FALSE_POSITIVE.value)
class FakeReader:
def read_all(self):
raise AssertionError("scan-run query should not call read_all")
def read_for_scan_run(self, scan_run_id):
return [verified, false_positive]
result = read_findings(
FindingQueryRequest(
storage_backend="dynamodb",
scan_run_id="scan_abc12345",
dynamodb_config=DynamoDbCompatibleConfig(table_name="SecurityScannerLocal"),
dispositions=[Disposition.FALSE_POSITIVE.value],
),
store=FakeReader(),
)
assert result == [false_positive]