Skip to content

Commit fc407e1

Browse files
committed
Enable header check for multiple headers
1 parent 970f97b commit fc407e1

File tree

4 files changed

+107
-15
lines changed

4 files changed

+107
-15
lines changed

aws/waf/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Note: For each rule, if you are providing a country list, you can only specify e
9090
| <a name="input_allowed_ip_list"></a> [allowed\_ip\_list](#input\_allowed\_ip\_list) | List of allowed IP addresses, these IP addresses will be exempted from any configured rules | `list(string)` | `[]` | no |
9191
| <a name="input_aws_managed_rule_groups"></a> [aws\_managed\_rule\_groups](#input\_aws\_managed\_rule\_groups) | Rule statement values used to run the rules that are defined in a managed rule group. You may review this list for the available AWS managed rule groups - https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups-list.html | <pre>map(object({<br> name = string # Name of the Managed rule group<br> priority = number # Relative processing order for rules processed by AWS WAF. All rules are processed from lowest priority to the highest.<br> 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`.<br> country_list = optional(list(string), []) # List of countries to apply the managed rule 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.<br> exempt_country_list = optional(list(string), []) # List of countries to exempt from the managed 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.<br> }))</pre> | n/a | yes |
9292
| <a name="input_block_ip_list"></a> [block\_ip\_list](#input\_block\_ip\_list) | List of IP addresses to be blocked and denied access to the ingress / cloudfront. | `list(string)` | `[]` | no |
93-
| <a name="input_header_match_rules"></a> [header\_match\_rules](#input\_header\_match\_rules) | Rule statement to inspect and match the header for an incoming request. | <pre>map(object({<br> name = string # Name of the header match rule group<br> priority = number # Relative processing order for header match rule relative to other rules processed by AWS WAF.<br> header_name = string # This is the name of the header to inspect for all incoming requests.<br> header_value = string # This is the value to look out for a matching header name for all incoming requests<br> 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.<br> }))</pre> | `null` | no |
93+
| <a name="input_header_match_rules"></a> [header\_match\_rules](#input\_header\_match\_rules) | Rule statement to inspect and match the header for an incoming request. | <pre>map(object({<br> name = string # Name of the header match rule group<br> priority = number # Relative processing order for header match rule relative to other rules processed by AWS WAF.<br> header_values = map(object({ # Header values contains a map of headers to inspect. You can provide multiple headers and values, all headers will be inspected together with `AND` logic.<br> header_name = string # This is the name of the header to inspect for all incoming requests.<br> header_value = string # This is the value to look out for a matching header name for all incoming requests<br> not_statement = optional(bool, false) # This indicates if the result this header match should be negated. The negated result will be joined with other header match results using `AND` logic if more than 1 header is provided.<br> }))<br> 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.<br> }))</pre> | `null` | no |
9494
| <a name="input_name"></a> [name](#input\_name) | Friendly name of the WebACL. | `string` | n/a | yes |
9595
| <a name="input_rate_limit_rules"></a> [rate\_limit\_rules](#input\_rate\_limit\_rules) | Rule statement to track and rate limits requests when they are coming at too fast a rate.. For more details, visit - https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups-list.html | <pre>map(object({<br> name = string # Name of the Rate limit rule group<br> priority = number # Relative processing order for rate limit rule relative to other rules processed by AWS WAF.<br> limit = optional(number, 2000) # This is the limit on requests from any single IP address within a 5 minute period<br> count_override = optional(bool, false) # 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.<br> country_list = optional(list(string), []) # List of countries to apply the rate limit 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.<br> exempt_country_list = optional(list(string), []) # List of countries to exempt from the rate limit. 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.<br> }))</pre> | n/a | yes |
9696
| <a name="input_resource_arn"></a> [resource\_arn](#input\_resource\_arn) | The Amazon Resource Name (ARN) of the resource to associate with the web ACL. This must be an ARN of an Application Load Balancer or an Amazon API Gateway stage. Value is required if scope is REGIONAL | `string` | `null` | no |
@@ -101,4 +101,5 @@ Note: For each rule, if you are providing a country list, you can only specify e
101101
| Name | Description |
102102
|------|-------------|
103103
| <a name="output_aws_waf_arn"></a> [aws\_waf\_arn](#output\_aws\_waf\_arn) | The arn for AWS WAF WebACL. |
104+
| <a name="output_waf_logs_sns_topic_arn"></a> [waf\_logs\_sns\_topic\_arn](#output\_waf\_logs\_sns\_topic\_arn) | The arn for the SNS topic to receive the AWS WAF logs |
104105
<!-- END_TF_DOCS -->

aws/waf/main.tf

+93-10
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,104 @@ resource "aws_wafv2_web_acl" "main" {
3131
block {}
3232
}
3333
}
34-
statement {
35-
byte_match_statement {
36-
field_to_match {
37-
single_header {
38-
name = lower(rule.value["header_name"])
34+
dynamic "statement" {
35+
for_each = length(rule.value["header_values"]) == 1 ? rule.value["header_values"] : {}
36+
content {
37+
dynamic "byte_match_statement" {
38+
for_each = statement.value["not_statement"] == false ? [1] : []
39+
content {
40+
field_to_match {
41+
single_header {
42+
name = lower(statement.value["header_name"])
43+
}
44+
}
45+
46+
positional_constraint = "CONTAINS"
47+
48+
search_string = statement.value["header_value"]
49+
50+
text_transformation {
51+
priority = 1
52+
type = "LOWERCASE"
53+
}
54+
}
55+
}
56+
dynamic "not_statement" {
57+
for_each = statement.value["not_statement"] == true ? [1] : []
58+
content {
59+
statement {
60+
byte_match_statement {
61+
field_to_match {
62+
single_header {
63+
name = lower(statement.value["header_name"])
64+
}
65+
}
66+
67+
positional_constraint = "CONTAINS"
68+
69+
search_string = statement.value["header_value"]
70+
71+
text_transformation {
72+
priority = 1
73+
type = "LOWERCASE"
74+
}
75+
}
76+
}
3977
}
4078
}
79+
}
80+
}
81+
dynamic "statement" {
82+
for_each = length(rule.value["header_values"]) > 1 ? rule.value["header_values"] : {}
83+
content {
84+
and_statement {
85+
dynamic "statement" {
86+
for_each = rule.value["header_values"]
87+
content {
88+
dynamic "byte_match_statement" {
89+
for_each = statement.value["not_statement"] == false ? [1] : []
90+
content {
91+
field_to_match {
92+
single_header {
93+
name = lower(statement.value["header_name"])
94+
}
95+
}
4196

42-
positional_constraint = "CONTAINS"
97+
positional_constraint = "CONTAINS"
4398

44-
search_string = rule.value["header_value"]
99+
search_string = statement.value["header_value"]
45100

46-
text_transformation {
47-
priority = 1
48-
type = "LOWERCASE"
101+
text_transformation {
102+
priority = 1
103+
type = "LOWERCASE"
104+
}
105+
}
106+
}
107+
dynamic "not_statement" {
108+
for_each = statement.value["not_statement"] == true ? [1] : []
109+
content {
110+
statement {
111+
byte_match_statement {
112+
field_to_match {
113+
single_header {
114+
name = lower(statement.value["header_name"])
115+
}
116+
}
117+
118+
positional_constraint = "CONTAINS"
119+
120+
search_string = statement.value["header_value"]
121+
122+
text_transformation {
123+
priority = 1
124+
type = "LOWERCASE"
125+
}
126+
}
127+
}
128+
}
129+
}
130+
}
131+
}
49132
}
50133
}
51134
}

aws/waf/outputs.tf

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ output "aws_waf_arn" {
22
description = "The arn for AWS WAF WebACL."
33
value = aws_wafv2_web_acl.main.arn
44
}
5+
6+
output "waf_logs_sns_topic_arn" {
7+
description = "The arn for the SNS topic to receive the AWS WAF logs"
8+
value = aws_sns_topic.waf_logs_sns_subscription.arn
9+
}

aws/waf/variables.tf

+7-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ variable "rate_limit_rules" {
4646
variable "header_match_rules" {
4747
description = "Rule statement to inspect and match the header for an incoming request."
4848
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
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_values = map(object({ # Header values contains a map of headers to inspect. You can provide multiple headers and values, all headers will be inspected together with `AND` logic.
52+
header_name = string # This is the name of the header to inspect for all incoming requests.
53+
header_value = string # This is the value to look out for a matching header name for all incoming requests
54+
not_statement = optional(bool, false) # This indicates if the result this header match should be negated. The negated result will be joined with other header match results using `AND` logic if more than 1 header is provided.
55+
}))
5356
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.
5457
}))
5558

0 commit comments

Comments
 (0)