Skip to content

Commit 28d12eb

Browse files
author
olamide
committed
Add waf rule to inspect for header values
1 parent aa22804 commit 28d12eb

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

aws/waf/main.tf

+64
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,70 @@ resource "aws_wafv2_web_acl" "main" {
1313
metric_name = "${var.name}-cloudfront-web-acl"
1414
}
1515

16+
dynamic "header_rule" {
17+
for_each = var.header_match_rules
18+
content {
19+
name = "${header_rule.value["name"]}-header-match-rule"
20+
priority = header_rule.value["priority"]
21+
22+
dynamic "action" {
23+
for_each = header_rule.value["count_override"] == true ? [1] : []
24+
content {
25+
count {}
26+
}
27+
}
28+
dynamic "action" {
29+
for_each = header_rule.value["count_override"] == false ? [1] : []
30+
content {
31+
block {}
32+
}
33+
}
34+
statement {
35+
byte_match_statement {
36+
field_to_match {
37+
single_header = lower(header_rule.value["header_name"])
38+
}
39+
40+
positional_constraint = "CONTAINS"
41+
42+
search_string = header_rule.value["header_value"]
43+
44+
text_transformation {
45+
priority = 1
46+
type = "LOWERCASE"
47+
}
48+
49+
dynamic "scope_down_statement" {
50+
for_each = length(concat(rule.value["country_list"], rule.value["exempt_country_list"])) > 0 ? [1] : []
51+
content {
52+
dynamic "geo_match_statement" {
53+
for_each = length(rule.value["country_list"]) > 0 ? [1] : []
54+
content {
55+
country_codes = rule.value["country_list"]
56+
}
57+
}
58+
dynamic "not_statement" {
59+
for_each = length(rule.value["exempt_country_list"]) > 0 ? [1] : []
60+
content {
61+
statement {
62+
geo_match_statement {
63+
country_codes = rule.value["exempt_country_list"]
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
72+
visibility_config {
73+
cloudwatch_metrics_enabled = true
74+
sampled_requests_enabled = true
75+
metric_name = "${rule.value["name"]}-header-match-rule"
76+
}
77+
}
78+
}
79+
1680
dynamic "rule" {
1781
for_each = var.rate_limit_rules
1882
content {

aws/waf/variables.tf

+15
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ variable "rate_limit_rules" {
4343
}))
4444
}
4545

46+
variable "header_match_rules" {
47+
description = "Rule statement to inspect and match the header for an incoming request."
48+
type = map(object({
49+
name = string # Name of the header match rule group
50+
priority = number # Relative processing order for header match rule relative to other rules processed by AWS WAF.
51+
header_name = string # This is the name of the header to inspect for all incoming requests.
52+
header_value = string # This is the value to look out for a matching header name for all incoming requests
53+
count_override = optional(bool, true) # If true, this will override the rule action setting to `count`, if false, the rule action will be set to `block`. Default value is false.
54+
country_list = optional(list(string), []) # List of countries to apply the header match to. If populated, from other countries will be ignored by this rule. IF empty, the rule will apply to all traffic. You must either specify country_list or exempt_country_list, but not both.
55+
exempt_country_list = optional(list(string), []) # List of countries to exempt from the header match rule. If populated, the selected countries will be ignored by this rule. IF empty, the rule will apply to all traffic. You must either specify country_list or exempt_country_list, but not both.
56+
}))
57+
58+
default = null
59+
}
60+
4661
variable "allowed_ip_list" {
4762
description = "List of allowed IP addresses, these IP addresses will be exempted from any configured rules"
4863
type = list(string)

0 commit comments

Comments
 (0)