Skip to content

Commit 57d9169

Browse files
committed
Fix: WAF alarm tripped by BlockedIPs and BlockedUserAgents rules (#7205)
1 parent 6519c89 commit 57d9169

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

terraform/cloudwatch.tf.json.template.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from azul import (
44
config,
5+
iif,
56
)
67
from azul.deployment import (
78
aws,
@@ -23,6 +24,42 @@ def dashboard_body() -> str:
2324
return body
2425

2526

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+
2663
emit_tf({
2764
'data': [
2865
{
@@ -280,7 +317,7 @@ def dashboard_body() -> str:
280317
{
281318
'id': 'waf',
282319
'label': 'Percentage of blocked requests',
283-
'expression': 'm1/(m0+m1)*100',
320+
'expression': waf_blocked_alarm_expression(),
284321
'return_data': 'true',
285322
},
286323
*(
@@ -294,11 +331,11 @@ def dashboard_body() -> str:
294331
'dimensions': {
295332
'WebACL': '${aws_wafv2_web_acl.api_gateway.name}',
296333
'Region': config.region,
297-
'Rule': 'ALL'
334+
'Rule': rule
298335
}
299336
}
300337
}
301-
for i, metric in enumerate(['AllowedRequests', 'BlockedRequests'])
338+
for i, (metric, rule) in enumerate(waf_blocked_alarm_metrics())
302339
)
303340
]
304341
},

0 commit comments

Comments
 (0)