Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rationalise ratelimit exemption rules. #1810

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions terraform/projects/infra-public-wafs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ No modules.
| [aws_cloudwatch_log_group.public_backend_waf](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
| [aws_cloudwatch_log_group.public_bouncer_waf](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
| [aws_cloudwatch_log_group.public_cache_waf](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
| [aws_shield_protection.ckan_public_lb](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/shield_protection) | resource |
| [aws_shield_protection.deploy_public_lb](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/shield_protection) | resource |
| [aws_shield_protection.graphite_public_lb](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/shield_protection) | resource |
| [aws_shield_protection.licensify_backend_public_lb](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/shield_protection) | resource |
| [aws_shield_protection.licensify_frontend_public_lb](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/shield_protection) | resource |
| [aws_shield_protection.monitoring_public_lb](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/shield_protection) | resource |
| [aws_shield_protection.prometheus_public_lb](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/shield_protection) | resource |
| [aws_wafv2_ip_set.external_partner_ips](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_ip_set) | resource |
| [aws_wafv2_ip_set.govuk_requesting_ips](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_ip_set) | resource |
| [aws_wafv2_ip_set.high_request_rate](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_ip_set) | resource |
| [aws_wafv2_ip_set.nat_gateway_ips](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_ip_set) | resource |
| [aws_wafv2_regex_pattern_set.x_always_block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_regex_pattern_set) | resource |
| [aws_wafv2_rule_group.x_always_block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_rule_group) | resource |
Expand All @@ -43,7 +42,6 @@ No modules.
| [aws_wafv2_web_acl.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl) | resource |
| [aws_wafv2_web_acl.licensify_backend_public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl) | resource |
| [aws_wafv2_web_acl.licensify_frontend_public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl) | resource |
| [aws_wafv2_web_acl_association.ckan_public_web_acl](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl_association) | resource |
| [aws_wafv2_web_acl_association.deploy_public_web_acl](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl_association) | resource |
| [aws_wafv2_web_acl_association.graphite_public_web_acl](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl_association) | resource |
| [aws_wafv2_web_acl_association.licensify_backend_public_web_acl](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl_association) | resource |
Expand All @@ -67,7 +65,7 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_allow_external_ips"></a> [allow\_external\_ips](#input\_allow\_external\_ips) | An array of CIDR blocks that are our partners using to send traffic to us | `list(string)` | n/a | yes |
| <a name="input_allow_high_request_rate_from_cidrs"></a> [allow\_high\_request\_rate\_from\_cidrs](#input\_allow\_high\_request\_rate\_from\_cidrs) | Source IP netblocks from which we allow a higher rate of requests. | `list(string)` | n/a | yes |
| <a name="input_aws_environment"></a> [aws\_environment](#input\_aws\_environment) | AWS Environment | `string` | n/a | yes |
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | AWS region | `string` | `"eu-west-1"` | no |
| <a name="input_backend_public_base_rate_limit"></a> [backend\_public\_base\_rate\_limit](#input\_backend\_public\_base\_rate\_limit) | For the backend ALB. Number of requests to allow in a 5 minute period before rate limiting is applied. | `number` | n/a | yes |
Expand Down
24 changes: 16 additions & 8 deletions terraform/projects/infra-public-wafs/backend_public_rule.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ resource "aws_wafv2_web_acl" "backend_public" {
# the waf is enabled and processing requests
rule {
name = "x-always-block_web_acl_rule"
priority = 1
priority = 10

override_action {
none {}
Expand All @@ -30,18 +30,26 @@ resource "aws_wafv2_web_acl" "backend_public" {
}
}

# this rule matches any request from our NAT gateway IPs and allows it.
rule {
name = "allow-govuk-infra"
priority = 2
name = "rate-limit-exemptions"
priority = 20

action {
allow {}
}

statement {
ip_set_reference_statement {
arn = aws_wafv2_ip_set.govuk_requesting_ips.arn
or_statement {
statement {
ip_set_reference_statement {
arn = aws_wafv2_ip_set.govuk_requesting_ips.arn
}
}
statement {
ip_set_reference_statement {
arn = aws_wafv2_ip_set.high_request_rate.arn
}
}
}
}

Expand All @@ -57,7 +65,7 @@ resource "aws_wafv2_web_acl" "backend_public" {
# this is checked every 30s
rule {
name = "backend-public-base-rate-warning"
priority = 3
priority = 30

action {
count {}
Expand All @@ -81,7 +89,7 @@ resource "aws_wafv2_web_acl" "backend_public" {
# this is checked every 30s
rule {
name = "backend-public-base-rate-limit"
priority = 4
priority = 40

action {
block {
Expand Down
8 changes: 4 additions & 4 deletions terraform/projects/infra-public-wafs/cache_public_rule.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ resource "aws_wafv2_ip_set" "govuk_requesting_ips" {
addresses = concat(var.traffic_replay_ips, local.nat_gateway_ips, var.eks_egress_ips)
}

resource "aws_wafv2_ip_set" "external_partner_ips" {
name = "external_partner_ips"
description = "The IP addresses are used by our partners."
resource "aws_wafv2_ip_set" "high_request_rate" {
name = "high_request_rate"
description = "Source addresses from which we allow a higher ratelimit."
scope = "REGIONAL"
ip_address_version = "IPV4"
addresses = var.allow_external_ips
addresses = var.allow_high_request_rate_from_cidrs
}

resource "aws_cloudwatch_log_group" "public_cache_waf" {
Expand Down
10 changes: 0 additions & 10 deletions terraform/projects/infra-public-wafs/standard_config.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@

resource "aws_shield_protection" "ckan_public_lb" {
name = "${var.stackname}-ckan-public-lb_shield"
resource_arn = data.terraform_remote_state.infra_public_services.outputs.ckan_public_lb_id
}

resource "aws_wafv2_web_acl_association" "ckan_public_web_acl" {
resource_arn = data.terraform_remote_state.infra_public_services.outputs.ckan_public_lb_id
web_acl_arn = aws_wafv2_web_acl.default.arn
}

resource "aws_shield_protection" "deploy_public_lb" {
name = "${var.stackname}-deploy-public-lb_shield"
resource_arn = data.terraform_remote_state.infra_public_services.outputs.deploy_public_lb_id
Expand Down
4 changes: 2 additions & 2 deletions terraform/projects/infra-public-wafs/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ variable "eks_egress_ips" {
description = "An array of CIDR blocks for the corresponding EKS environment's NAT gateway IPs"
}

variable "allow_external_ips" {
variable "allow_high_request_rate_from_cidrs" {
type = list(string)
description = "An array of CIDR blocks that are our partners using to send traffic to us"
description = "Source IP netblocks from which we allow a higher rate of requests."
}

variable "waf_log_retention_days" {
Expand Down