-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_rules.py
More file actions
300 lines (278 loc) · 10 KB
/
Copy pathtest_rules.py
File metadata and controls
300 lines (278 loc) · 10 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
231
232
233
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
300
from __future__ import annotations
import pandas as pd
import pytest
from telemetry_lab.rules import apply_rules
def _rule_validation_features() -> pd.DataFrame:
return pd.DataFrame(
[
{
"window_start": pd.Timestamp("2026-03-10T10:00:00Z"),
"window_end": pd.Timestamp("2026-03-10T10:01:00Z"),
"event_count": 10,
"error_count": 4,
"error_rate": 0.40,
"unique_sources": 4,
"unique_targets": 2,
"high_severity_count": 2,
"login_fail_count": 4,
"malware_alert_count": 0,
}
]
)
def test_apply_rules_triggers_expected_alerts() -> None:
features = pd.DataFrame(
[
{
"window_start": pd.Timestamp("2026-03-10T10:00:00Z"),
"window_end": pd.Timestamp("2026-03-10T10:01:00Z"),
"event_count": 12,
"error_count": 5,
"error_rate": 0.42,
"unique_sources": 4,
"unique_targets": 2,
"high_severity_count": 1,
"login_fail_count": 6,
"malware_alert_count": 0,
},
{
"window_start": pd.Timestamp("2026-03-10T10:00:10Z"),
"window_end": pd.Timestamp("2026-03-10T10:01:10Z"),
"event_count": 14,
"error_count": 6,
"error_rate": 0.43,
"unique_sources": 11,
"unique_targets": 3,
"high_severity_count": 4,
"login_fail_count": 8,
"malware_alert_count": 0,
},
{
"window_start": pd.Timestamp("2026-03-10T10:00:20Z"),
"window_end": pd.Timestamp("2026-03-10T10:01:20Z"),
"event_count": 10,
"error_count": 5,
"error_rate": 0.50,
"unique_sources": 12,
"unique_targets": 3,
"high_severity_count": 4,
"login_fail_count": 9,
"malware_alert_count": 2,
},
]
)
config = {
"high_error_rate": {"threshold": 0.30, "severity": "medium"},
"login_fail_burst": {"threshold": 8, "severity": "high"},
"high_severity_spike": {"threshold": 3, "severity": "high"},
"persistent_high_error": {
"threshold": 0.25,
"consecutive_windows": 2,
"severity": "medium",
},
"source_spread_spike": {
"absolute_threshold": 10,
"multiplier": 1.5,
"severity": "medium",
},
"rare_event_repeat": {
"threshold": 2,
"event_types": ["malware_alert"],
"severity": "high",
},
}
alerts = apply_rules(features, config)
assert "high_error_rate" in set(alerts["rule_name"])
assert "login_fail_burst" in set(alerts["rule_name"])
assert "high_severity_spike" in set(alerts["rule_name"])
assert "persistent_high_error" in set(alerts["rule_name"])
assert "source_spread_spike" in set(alerts["rule_name"])
assert "rare_event_repeat_malware_alert" in set(alerts["rule_name"])
def test_apply_rules_suppresses_repeated_same_rule_within_cooldown() -> None:
features = pd.DataFrame(
[
{
"window_start": pd.Timestamp("2026-03-10T10:00:00Z"),
"window_end": pd.Timestamp("2026-03-10T10:01:00Z"),
"event_count": 10,
"error_count": 4,
"error_rate": 0.40,
"unique_sources": 4,
"unique_targets": 2,
"high_severity_count": 0,
"login_fail_count": 0,
"malware_alert_count": 0,
},
{
"window_start": pd.Timestamp("2026-03-10T10:00:10Z"),
"window_end": pd.Timestamp("2026-03-10T10:01:10Z"),
"event_count": 11,
"error_count": 5,
"error_rate": 0.45,
"unique_sources": 5,
"unique_targets": 2,
"high_severity_count": 0,
"login_fail_count": 0,
"malware_alert_count": 0,
},
{
"window_start": pd.Timestamp("2026-03-10T10:01:00Z"),
"window_end": pd.Timestamp("2026-03-10T10:02:00Z"),
"event_count": 12,
"error_count": 6,
"error_rate": 0.50,
"unique_sources": 6,
"unique_targets": 2,
"high_severity_count": 0,
"login_fail_count": 0,
"malware_alert_count": 0,
},
]
)
alerts = apply_rules(
features,
{
"cooldown_seconds": 60,
"high_error_rate": {"threshold": 0.30, "severity": "medium"},
"persistent_high_error": {
"threshold": 1.0,
"consecutive_windows": 10,
"severity": "medium",
},
},
)
assert list(alerts["rule_name"]) == ["high_error_rate", "high_error_rate"]
assert list(alerts["alert_time"]) == [
pd.Timestamp("2026-03-10T10:01:00Z"),
pd.Timestamp("2026-03-10T10:02:00Z"),
]
def test_apply_rules_scopes_same_rule_cooldown_by_source_when_present() -> None:
features = pd.DataFrame(
[
{
"window_start": pd.Timestamp("2026-03-10T10:00:00Z"),
"window_end": pd.Timestamp("2026-03-10T10:01:00Z"),
"source": "host_a",
"event_count": 10,
"error_count": 4,
"error_rate": 0.40,
"unique_sources": 4,
"unique_targets": 2,
"high_severity_count": 0,
"login_fail_count": 0,
"malware_alert_count": 0,
},
{
"window_start": pd.Timestamp("2026-03-10T10:00:10Z"),
"window_end": pd.Timestamp("2026-03-10T10:01:10Z"),
"source": "host_b",
"event_count": 11,
"error_count": 5,
"error_rate": 0.45,
"unique_sources": 5,
"unique_targets": 2,
"high_severity_count": 0,
"login_fail_count": 0,
"malware_alert_count": 0,
},
{
"window_start": pd.Timestamp("2026-03-10T10:00:20Z"),
"window_end": pd.Timestamp("2026-03-10T10:01:20Z"),
"source": "host_a",
"event_count": 12,
"error_count": 6,
"error_rate": 0.50,
"unique_sources": 6,
"unique_targets": 2,
"high_severity_count": 0,
"login_fail_count": 0,
"malware_alert_count": 0,
},
]
)
alerts = apply_rules(
features,
{
"cooldown_seconds": 60,
"high_error_rate": {"threshold": 0.30, "severity": "medium"},
"persistent_high_error": {
"threshold": 1.0,
"consecutive_windows": 10,
"severity": "medium",
},
},
)
assert list(alerts["rule_name"]) == ["high_error_rate", "high_error_rate"]
assert list(alerts["alert_time"]) == [
pd.Timestamp("2026-03-10T10:01:00Z"),
pd.Timestamp("2026-03-10T10:01:10Z"),
]
def test_apply_rules_keeps_different_rules_during_same_cooldown_window() -> None:
features = pd.DataFrame(
[
{
"window_start": pd.Timestamp("2026-03-10T10:00:00Z"),
"window_end": pd.Timestamp("2026-03-10T10:01:00Z"),
"event_count": 12,
"error_count": 5,
"error_rate": 0.42,
"unique_sources": 4,
"unique_targets": 2,
"high_severity_count": 4,
"login_fail_count": 8,
"malware_alert_count": 0,
},
{
"window_start": pd.Timestamp("2026-03-10T10:00:10Z"),
"window_end": pd.Timestamp("2026-03-10T10:01:10Z"),
"event_count": 14,
"error_count": 6,
"error_rate": 0.43,
"unique_sources": 5,
"unique_targets": 2,
"high_severity_count": 5,
"login_fail_count": 9,
"malware_alert_count": 0,
},
]
)
alerts = apply_rules(
features,
{
"cooldown_seconds": 60,
"high_error_rate": {"threshold": 0.30, "severity": "medium"},
"login_fail_burst": {"threshold": 8, "severity": "high"},
"high_severity_spike": {"threshold": 3, "severity": "high"},
"persistent_high_error": {
"threshold": 1.0,
"consecutive_windows": 10,
"severity": "medium",
},
},
)
assert list(alerts["rule_name"]) == [
"high_error_rate",
"high_severity_spike",
"login_fail_burst",
]
@pytest.mark.parametrize(
("rules_config", "message"),
[
([], "Rules config must be a mapping"),
({"cooldown_seconds": True}, "cooldown_seconds"),
({"cooldown_seconds": -1}, "cooldown_seconds"),
({"high_error_rate": True}, "high_error_rate"),
({"high_error_rate": {"threshold": True}}, "high_error_rate.threshold"),
({"login_fail_burst": {"threshold": 0}}, "login_fail_burst.threshold"),
(
{"source_spread_spike": {"multiplier": float("inf")}},
"source_spread_spike.multiplier",
),
(
{"rare_event_repeat": {"event_types": "malware_alert"}},
"rare_event_repeat.event_types",
),
],
)
def test_apply_rules_rejects_invalid_direct_rule_config(rules_config, message) -> None:
with pytest.raises(ValueError, match=message):
apply_rules(_rule_validation_features(), rules_config)