Skip to content

Commit 1259465

Browse files
committed
Different rate limits depending on HTTP method (#5555)
1 parent 08eff88 commit 1259465

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

src/azul/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,8 @@ def docker_images(self) -> dict[str, str]:
15031503

15041504
waf_rate_rule_name = 'RateRule'
15051505

1506+
waf_expensive_rate_rule_name = 'ExpensiveRateRule'
1507+
15061508

15071509
config: Config = Config() # yes, the type hint does help PyCharm
15081510

terraform/api_gateway.tf.json.template.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,34 @@ def for_domain(cls, domain):
204204
'rule': [
205205
{
206206
'priority': 0,
207+
'name': 'LabelPostRequests',
208+
'action': {
209+
'count': {}
210+
},
211+
'rule_label': {
212+
'name': 'azul:expensive'
213+
},
214+
'statement': {
215+
'byte_match_statement': {
216+
'field_to_match': {
217+
'method': {}
218+
},
219+
'positional_constraint': 'EXACTLY',
220+
'search_string': 'POST',
221+
'text_transformation': {
222+
'priority': 0,
223+
'type': 'NONE'
224+
}
225+
}
226+
},
227+
'visibility_config': {
228+
'metric_name': 'LabelPostRequests',
229+
'sampled_requests_enabled': True,
230+
'cloudwatch_metrics_enabled': True
231+
}
232+
},
233+
{
234+
'priority': 1,
207235
'name': 'BlockedIPs',
208236
'action': {
209237
'block': {}
@@ -220,15 +248,39 @@ def for_domain(cls, domain):
220248
}
221249
},
222250
{
223-
'priority': 1,
251+
'priority': 2,
252+
'name': config.waf_expensive_rate_rule_name,
253+
'action': {
254+
'block': {}
255+
},
256+
'statement': {
257+
'rate_based_statement': {
258+
'limit': 100, # limit must be between 100 and 20,000,000
259+
'aggregate_key_type': 'IP',
260+
'scope_down_statement': {
261+
'label_match_statement': {
262+
'scope': 'LABEL',
263+
'key': 'azul:expensive'
264+
}
265+
}
266+
}
267+
},
268+
'visibility_config': {
269+
'metric_name': config.waf_expensive_rate_rule_name,
270+
'sampled_requests_enabled': True,
271+
'cloudwatch_metrics_enabled': True
272+
}
273+
},
274+
{
275+
'priority': 3,
224276
'name': config.waf_rate_rule_name,
225277
'action': {
226278
'block': {}
227279
},
228280
'statement': {
229281
'rate_based_statement': {
230282
'limit': 1000, # limit must be between 100 and 20,000,000
231-
'aggregate_key_type': 'IP'
283+
'aggregate_key_type': 'IP',
232284
}
233285
},
234286
'visibility_config': {
@@ -238,7 +290,7 @@ def for_domain(cls, domain):
238290
}
239291
},
240292
{
241-
'priority': 2,
293+
'priority': 4,
242294
'name': 'AWS-CommonRuleSet',
243295
'override_action': {
244296
'none': {}
@@ -274,7 +326,7 @@ def for_domain(cls, domain):
274326
}
275327
},
276328
{
277-
'priority': 3,
329+
'priority': 5,
278330
'name': 'AWS-AmazonIpReputationList',
279331
'override_action': {
280332
'none': {}
@@ -292,7 +344,7 @@ def for_domain(cls, domain):
292344
}
293345
},
294346
{
295-
'priority': 4,
347+
'priority': 6,
296348
'name': 'AWS-UnixRuleSet',
297349
'override_action': {
298350
'none': {}

terraform/cloudwatch.tf.json.template.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,25 @@ def prod_qualified_resource_name(name: str) -> str:
367367
},
368368
'alarm_actions': ['${data.aws_sns_topic.monitoring.arn}'],
369369
'ok_actions': ['${data.aws_sns_topic.monitoring.arn}'],
370+
},
371+
'waf_expensive_rate_blocked': {
372+
'alarm_name': config.qualified_resource_name('waf_expensive_rate_blocked'),
373+
'comparison_operator': 'GreaterThanThreshold',
374+
'threshold': 0,
375+
'datapoints_to_alarm': 1,
376+
'evaluation_periods': 1,
377+
'period': 5 * 60,
378+
'metric_name': 'BlockedRequests',
379+
'namespace': 'AWS/WAFV2',
380+
'statistic': 'Sum',
381+
'treat_missing_data': 'notBreaching',
382+
'dimensions': {
383+
'WebACL': '${aws_wafv2_web_acl.api_gateway.name}',
384+
'Region': config.region,
385+
'Rule': config.waf_expensive_rate_rule_name
386+
},
387+
'alarm_actions': ['${data.aws_sns_topic.monitoring.arn}'],
388+
'ok_actions': ['${data.aws_sns_topic.monitoring.arn}'],
370389
}
371390
}
372391
}

0 commit comments

Comments
 (0)