2
2
3
3
from azul import (
4
4
config ,
5
+ iif ,
5
6
)
6
7
from azul .deployment import (
7
8
aws ,
@@ -23,6 +24,42 @@ def dashboard_body() -> str:
23
24
return body
24
25
25
26
27
+ def waf_blocked_alarm_metrics () -> list [tuple [str , str ]]:
28
+ # The blocking WAF rules to be evaluated by the 'waf_blocked' alarm, i.e.
29
+ # requests blocked by WAF rules omitted from this list will not trigger the
30
+ # alarm.
31
+ blocking_rules = [
32
+ config .waf_rate_rule_name ,
33
+ config .waf_rate_alarm_rule_name ,
34
+ 'AWS-CommonRuleSet' ,
35
+ 'AWS-AmazonIpReputationList' ,
36
+ 'AWS-UnixRuleSet' ,
37
+ * iif (config .waf_file_download_limit , [
38
+ 'FileDownloadRateLimit'
39
+ ]),
40
+ * iif (config .waf_bot_control , [
41
+ 'AWS-AWSManagedRulesBotControlRuleSet' ,
42
+ 'BlockVerifiedBotsRule'
43
+ ])
44
+ ]
45
+ return [
46
+ # Note the first metric is for the allowed requests …
47
+ ('AllowedRequests' , 'ALL' ),
48
+ # … followed by metrics for each included block rule.
49
+ * [('BlockedRequests' , rule ) for rule in blocking_rules ]
50
+ ]
51
+
52
+
53
+ def waf_blocked_alarm_expression () -> str :
54
+ """
55
+ Return an expression used by the 'waf_blocked' alarm giving the percentage
56
+ of blocked requests, where 'm0' is the metric for all allowed requests, and
57
+ 'm1'… represents each evaluated blocking rule. e.g. "m1+m2/(m0+m1+m2)*100"
58
+ """
59
+ msum = '+' .join (f'm{ i } ' for i in range (1 , len (waf_blocked_alarm_metrics ())))
60
+ return f'{ msum } /(m0+{ msum } )*100'
61
+
62
+
26
63
emit_tf ({
27
64
'data' : [
28
65
{
@@ -280,7 +317,7 @@ def dashboard_body() -> str:
280
317
{
281
318
'id' : 'waf' ,
282
319
'label' : 'Percentage of blocked requests' ,
283
- 'expression' : 'm1/(m0+m1)*100' ,
320
+ 'expression' : waf_blocked_alarm_expression () ,
284
321
'return_data' : 'true' ,
285
322
},
286
323
* (
@@ -294,11 +331,11 @@ def dashboard_body() -> str:
294
331
'dimensions' : {
295
332
'WebACL' : '${aws_wafv2_web_acl.api_gateway.name}' ,
296
333
'Region' : config .region ,
297
- 'Rule' : 'ALL'
334
+ 'Rule' : rule
298
335
}
299
336
}
300
337
}
301
- for i , metric in enumerate ([ 'AllowedRequests' , 'BlockedRequests' ] )
338
+ for i , ( metric , rule ) in enumerate (waf_blocked_alarm_metrics () )
302
339
)
303
340
]
304
341
},
0 commit comments