Skip to content

Commit e237bc7

Browse files
committed
Fix bug with thread_cache sync and update thread_cache test cases
1 parent 388afc8 commit e237bc7

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

aikido_zen/storage/statistics/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def get_record(self):
4444
"blocked": self.attacks_blocked,
4545
},
4646
},
47-
"operations": self.operations,
47+
"operations": dict(self.operations),
4848
}
4949

5050
def import_from_record(self, record):

aikido_zen/thread/thread_cache_test.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_reset(thread_cache: ThreadCache):
6868
thread_cache.config.bypassed_ips.add("192.168.1.1")
6969
thread_cache.config.blocked_uids.add("user123")
7070
thread_cache.stats.increment_total_hits()
71-
thread_cache.stats.on_detected_attack(blocked=True)
71+
thread_cache.stats.on_detected_attack(blocked=True, operation="test")
7272

7373
thread_cache.reset()
7474

@@ -236,9 +236,11 @@ def test_renew_called_with_correct_args(mock_get_comms, thread_cache: ThreadCach
236236
# Setup initial state
237237
thread_cache.stats.increment_total_hits()
238238
thread_cache.stats.increment_total_hits()
239-
thread_cache.stats.on_detected_attack(blocked=True)
240-
thread_cache.stats.on_detected_attack(blocked=False)
241-
thread_cache.stats.on_detected_attack(blocked=False)
239+
thread_cache.stats.operations.register_call("op1", "sql_op")
240+
thread_cache.stats.operations.register_call("op2", "sql_op")
241+
thread_cache.stats.on_detected_attack(blocked=True, operation="op1")
242+
thread_cache.stats.on_detected_attack(blocked=False, operation="op1")
243+
thread_cache.stats.on_detected_attack(blocked=False, operation="op2")
242244
thread_cache.routes.initialize_route({"method": "GET", "route": "/test"})
243245
thread_cache.routes.increment_route({"method": "GET", "route": "/test"})
244246

@@ -269,6 +271,18 @@ def test_renew_called_with_correct_args(mock_get_comms, thread_cache: ThreadCach
269271
"aborted": 0,
270272
"attacksDetected": {"blocked": 1, "total": 3},
271273
},
274+
"operations": {
275+
"op1": {
276+
"attacksDetected": {"blocked": 1, "total": 2},
277+
"kind": "sql_op",
278+
"total": 1,
279+
},
280+
"op2": {
281+
"attacksDetected": {"blocked": 0, "total": 1},
282+
"kind": "sql_op",
283+
"total": 1,
284+
},
285+
},
272286
},
273287
"middleware_installed": False,
274288
"hostnames": [],
@@ -313,6 +327,7 @@ def test_sync_data_for_users(mock_get_comms, thread_cache: ThreadCache):
313327
"aborted": 0,
314328
"attacksDetected": {"total": 0, "blocked": 0},
315329
},
330+
"operations": {},
316331
},
317332
"middleware_installed": False,
318333
"hostnames": [],
@@ -362,6 +377,7 @@ def test_renew_called_with_empty_routes(mock_get_comms, thread_cache: ThreadCach
362377
"aborted": 0,
363378
"attacksDetected": {"total": 0, "blocked": 0},
364379
},
380+
"operations": {},
365381
},
366382
"middleware_installed": False,
367383
"hostnames": [],
@@ -399,6 +415,7 @@ def test_renew_called_with_no_requests(mock_get_comms, thread_cache: ThreadCache
399415
"aborted": 0,
400416
"attacksDetected": {"total": 0, "blocked": 0},
401417
},
418+
"operations": {},
402419
},
403420
"middleware_installed": False,
404421
"hostnames": [],

0 commit comments

Comments
 (0)