Skip to content

Commit

Permalink
Merge pull request #1553 from govuk-one-login/AUT-2697/frontend-endpo…
Browse files Browse the repository at this point in the history
…int-rate-limiting

AUT-2697: Enable rate limiting of frontend urls
  • Loading branch information
whi-tw authored Apr 15, 2024
2 parents 1b3196d + 4861af0 commit 5edcfc6
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 23 deletions.
42 changes: 21 additions & 21 deletions ci/terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ci/terraform/site.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "= 5.34.0"
version = "= 5.45.0"
}
random = {
source = "hashicorp/random"
Expand Down
18 changes: 18 additions & 0 deletions ci/terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,21 @@ variable "alb_idle_timeout" {
description = "Frontend Application Load Balancer idle timeout"
default = 60
}

variable "rate_limited_endpoints" {
description = "List of endpoints that should be rate limited by session and IP"
type = list(string)
default = []
}

variable "rate_limited_endpoints_rate_limit_period" {
description = "Period in seconds for rate limiting for rate limited endpoints"
type = number
default = 120
}

variable "rate_limited_endpoints_requests_per_period" {
description = "Number of requests per period allowed for rate limited endpoints"
type = number
default = 100000
}
104 changes: 103 additions & 1 deletion ci/terraform/waf.tf
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,109 @@ resource "aws_wafv2_web_acl" "frontend_alb_waf_regional_web_acl" {
}
}

rule {
name = "BlockMoreThan100CheckYourEmailRequestsFromIPPer5Minutes"
priority = 21
rule_label {
name = "MoreThan100CheckYourEmailRequestsFromIPPer5Minutes"
}

action {
block {}
}

statement {
rate_based_statement {
limit = var.environment == "staging" ? 20000000 : var.rate_limited_endpoints_requests_per_period
evaluation_window_sec = var.rate_limited_endpoints_rate_limit_period
aggregate_key_type = "IP"


scope_down_statement {
or_statement {
dynamic "statement" {
for_each = var.rate_limited_endpoints
content {
byte_match_statement {
positional_constraint = "STARTS_WITH"
search_string = statement.value
field_to_match {
uri_path {}
}
text_transformation {
priority = 0
type = "LOWERCASE"
}
}
}
}
}
}
}
}

visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "${replace(var.environment, "-", "")}FrontendAlbWafMoreThan100CheckYourEmailRequestsFromIPPer5Minutes"
sampled_requests_enabled = true
}
}

rule {
name = "BlockMoreThan100CheckYourEmailRequestsFromApsSessionPer5Minutes"
priority = 22

rule_label {
name = "MoreThan100CheckYourEmailRequestsFromApsSessionPer5Minutes"
}

action {
block {}
}

statement {
rate_based_statement {
limit = var.environment == "staging" ? 20000000 : var.rate_limited_endpoints_requests_per_period
evaluation_window_sec = var.rate_limited_endpoints_rate_limit_period
aggregate_key_type = "CUSTOM_KEYS"
custom_key {
cookie {
name = "aps"
text_transformation {
priority = 0
type = "URL_DECODE"
}
}
}
scope_down_statement {
or_statement {
dynamic "statement" {
for_each = var.rate_limited_endpoints
content {
byte_match_statement {
positional_constraint = "STARTS_WITH"
search_string = statement.value
field_to_match {
uri_path {}
}
text_transformation {
priority = 0
type = "LOWERCASE"
}
}
}
}
}
}
}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "${replace(var.environment, "-", "")}FrontendAlbWafMoreThan100CheckYourEmailRequestsFromApsSessionPer5Minutes"
sampled_requests_enabled = true
}
}

rule {
override_action {
none {}
Expand Down Expand Up @@ -275,7 +378,6 @@ resource "aws_wafv2_web_acl" "frontend_alb_waf_regional_web_acl" {
}
}
}

visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "${replace(var.environment, "-", "")}FrontendAlbWafContactUsCount"
Expand Down

0 comments on commit 5edcfc6

Please sign in to comment.