Skip to content

Commit

Permalink
Enable header check for multiple headers
Browse files Browse the repository at this point in the history
  • Loading branch information
OlamideOl1 committed Apr 5, 2024
1 parent 970f97b commit fc407e1
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 15 deletions.
3 changes: 2 additions & 1 deletion aws/waf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Note: For each rule, if you are providing a country list, you can only specify e
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <a name="input_name"></a> [name](#input\_name) | Friendly name of the WebACL. | `string` | n/a | yes |
| <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 |
| <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 |
Expand All @@ -101,4 +101,5 @@ Note: For each rule, if you are providing a country list, you can only specify e
| Name | Description |
|------|-------------|
| <a name="output_aws_waf_arn"></a> [aws\_waf\_arn](#output\_aws\_waf\_arn) | The arn for AWS WAF WebACL. |
| <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 |
<!-- END_TF_DOCS -->
103 changes: 93 additions & 10 deletions aws/waf/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,104 @@ resource "aws_wafv2_web_acl" "main" {
block {}
}
}
statement {
byte_match_statement {
field_to_match {
single_header {
name = lower(rule.value["header_name"])
dynamic "statement" {
for_each = length(rule.value["header_values"]) == 1 ? rule.value["header_values"] : {}
content {
dynamic "byte_match_statement" {
for_each = statement.value["not_statement"] == false ? [1] : []
content {
field_to_match {
single_header {
name = lower(statement.value["header_name"])
}
}

positional_constraint = "CONTAINS"

search_string = statement.value["header_value"]

text_transformation {
priority = 1
type = "LOWERCASE"
}
}
}
dynamic "not_statement" {
for_each = statement.value["not_statement"] == true ? [1] : []
content {
statement {
byte_match_statement {
field_to_match {
single_header {
name = lower(statement.value["header_name"])
}
}

positional_constraint = "CONTAINS"

search_string = statement.value["header_value"]

text_transformation {
priority = 1
type = "LOWERCASE"
}
}
}
}
}
}
}
dynamic "statement" {
for_each = length(rule.value["header_values"]) > 1 ? rule.value["header_values"] : {}
content {
and_statement {
dynamic "statement" {
for_each = rule.value["header_values"]
content {
dynamic "byte_match_statement" {
for_each = statement.value["not_statement"] == false ? [1] : []
content {
field_to_match {
single_header {
name = lower(statement.value["header_name"])
}
}

positional_constraint = "CONTAINS"
positional_constraint = "CONTAINS"

search_string = rule.value["header_value"]
search_string = statement.value["header_value"]

text_transformation {
priority = 1
type = "LOWERCASE"
text_transformation {
priority = 1
type = "LOWERCASE"
}
}
}
dynamic "not_statement" {
for_each = statement.value["not_statement"] == true ? [1] : []
content {
statement {
byte_match_statement {
field_to_match {
single_header {
name = lower(statement.value["header_name"])
}
}

positional_constraint = "CONTAINS"

search_string = statement.value["header_value"]

text_transformation {
priority = 1
type = "LOWERCASE"
}
}
}
}
}
}
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions aws/waf/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ output "aws_waf_arn" {
description = "The arn for AWS WAF WebACL."
value = aws_wafv2_web_acl.main.arn
}

output "waf_logs_sns_topic_arn" {
description = "The arn for the SNS topic to receive the AWS WAF logs"
value = aws_sns_topic.waf_logs_sns_subscription.arn
}
11 changes: 7 additions & 4 deletions aws/waf/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ variable "rate_limit_rules" {
variable "header_match_rules" {
description = "Rule statement to inspect and match the header for an incoming request."
type = map(object({
name = string # Name of the header match rule group
priority = number # Relative processing order for header match rule relative to other rules processed by AWS WAF.
header_name = string # This is the name of the header to inspect for all incoming requests.
header_value = string # This is the value to look out for a matching header name for all incoming requests
name = string # Name of the header match rule group
priority = number # Relative processing order for header match rule relative to other rules processed by AWS WAF.
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.
header_name = string # This is the name of the header to inspect for all incoming requests.
header_value = string # This is the value to look out for a matching header name for all incoming requests
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.
}))
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.
}))

Expand Down

0 comments on commit fc407e1

Please sign in to comment.