From e1527926dc97ba080be4f80fe7ddc611ca6b0b8b Mon Sep 17 00:00:00 2001 From: Robert Fairburn <8029478+rfairburn@users.noreply.github.com> Date: Fri, 22 Aug 2025 07:34:09 -0500 Subject: [PATCH] Initial support for bypassing URLs on the allowlist (#94) --- addons/waf-alb/README.md | 15 +++++++++---- addons/waf-alb/main.tf | 44 +++++++++++++++++++++++++++++++++---- addons/waf-alb/variables.tf | 15 +++++++++++++ 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/addons/waf-alb/README.md b/addons/waf-alb/README.md index 61a8c00..2b228e4 100644 --- a/addons/waf-alb/README.md +++ b/addons/waf-alb/README.md @@ -9,7 +9,7 @@ No requirements. | Name | Version | |------|---------| -| [aws](#provider\_aws) | n/a | +| [aws](#provider\_aws) | 6.9.0 | ## Modules @@ -19,8 +19,11 @@ No modules. | Name | Type | |------|------| -| [aws_wafv2_ip_set.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_ip_set) | resource | -| [aws_wafv2_rule_group.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_rule_group) | resource | +| [aws_wafv2_ip_set.allowed](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_ip_set) | resource | +| [aws_wafv2_ip_set.blocked](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_ip_set) | resource | +| [aws_wafv2_regex_pattern_set.bypass_urls](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_regex_pattern_set) | resource | +| [aws_wafv2_rule_group.allowed](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_rule_group) | resource | +| [aws_wafv2_rule_group.blocked](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_rule_group) | resource | | [aws_wafv2_web_acl.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl) | resource | | [aws_wafv2_web_acl_association.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl_association) | resource | @@ -28,10 +31,14 @@ No modules. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| +| [allowed\_addresses](#input\_allowed\_addresses) | n/a | `list(string)` | `[]` | no | | [blocked\_addresses](#input\_blocked\_addresses) | n/a | `list(string)` | `[]` | no | -| [blocked\_countries](#input\_blocked\_countries) | n/a | `list(string)` |
[
"BI",
"BY",
"CD",
"CF",
"CU",
"IQ",
"IR",
"LB",
"LY",
"SD",
"SO",
"SS",
"SY",
"VE",
"ZW",
"RU"
]
| no | +| [blocked\_countries](#input\_blocked\_countries) | n/a | `list(string)` |
[
"BI",
"BY",
"CD",
"CF",
"CU",
"IQ",
"IR",
"LB",
"LY",
"SD",
"SO",
"SS",
"SY",
"VE",
"ZW",
"RU"
]
| no | +| [bypass\_urls](#input\_bypass\_urls) | Optional list of regex patterns for URL paths that bypass the IP allowlist WAF rule.
The patterns must be valid AWS WAF regex strings. | `list(string)` | `[]` | no | +| [capacity](#input\_capacity) | The capacity required to handle the rules. | `number` | `2` | no | | [lb\_arn](#input\_lb\_arn) | n/a | `any` | n/a | yes | | [name](#input\_name) | n/a | `any` | n/a | yes | +| [waf\_type](#input\_waf\_type) | n/a | `string` | `"blocklist"` | no | ## Outputs diff --git a/addons/waf-alb/main.tf b/addons/waf-alb/main.tf index a62daf3..5b3d210 100644 --- a/addons/waf-alb/main.tf +++ b/addons/waf-alb/main.tf @@ -6,7 +6,7 @@ resource "aws_wafv2_rule_group" "blocked" { count = var.waf_type == "blocklist" ? 1 : 0 name = var.name scope = "REGIONAL" - capacity = 2 + capacity = var.capacity rule { name = "countries" @@ -69,7 +69,7 @@ resource "aws_wafv2_rule_group" "allowed" { count = var.waf_type == "allowlist" ? 1 : 0 name = var.name scope = "REGIONAL" - capacity = 2 + capacity = var.capacity rule { name = "specific" @@ -80,8 +80,30 @@ resource "aws_wafv2_rule_group" "allowed" { } statement { - ip_set_reference_statement { - arn = aws_wafv2_ip_set.allowed[0].arn + or_statement { + # Dynamic bypass regex statements + dynamic "statement" { + for_each = length(var.bypass_urls) > 0 ? [1] : [] + content { + regex_pattern_set_reference_statement { + arn = aws_wafv2_regex_pattern_set.bypass_urls[0].arn + + field_to_match { + uri_path {} + } + + text_transformation { + priority = 0 + type = "NONE" + } + } + } + } + statement { + ip_set_reference_statement { + arn = aws_wafv2_ip_set.allowed[0].arn + } + } } } @@ -150,6 +172,20 @@ resource "aws_wafv2_ip_set" "allowed" { addresses = var.allowed_addresses } +resource "aws_wafv2_regex_pattern_set" "bypass_urls" { + # Only works for allowlists + count = length(var.bypass_urls) > 0 && var.waf_type == "allowlist" ? 1 : 0 + + name = var.name + scope = "REGIONAL" + + dynamic "regular_expression" { + for_each = toset(var.bypass_urls) + content { + regex_string = regular_expression.value + } + } +} resource "aws_wafv2_web_acl_association" "main" { resource_arn = var.lb_arn diff --git a/addons/waf-alb/variables.tf b/addons/waf-alb/variables.tf index 2c16acc..d4b7eb3 100644 --- a/addons/waf-alb/variables.tf +++ b/addons/waf-alb/variables.tf @@ -21,3 +21,18 @@ variable "allowed_addresses" { type = list(string) default = [] } + +variable "capacity" { + description = "The capacity required to handle the rules." + type = number + default = 2 +} + +variable "bypass_urls" { + description = <