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

Custom Block Response #56

Merged
merged 16 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ No modules.
| <a name="input_rules"></a> [rules](#input\_rules) | List of WAF rules. | `any` | `[]` | no |
| <a name="input_scope"></a> [scope](#input\_scope) | Specifies whether this is for an AWS CloudFront distribution or for a regional application. Valid values are CLOUDFRONT or REGIONAL. To work with CloudFront, you must also specify the region us-east-1 (N. Virginia) on the AWS provider. | `string` | `"REGIONAL"` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags (key-value pairs) passed to resources. | `map(string)` | `{}` | no |
| <a name="input_visibility_config"></a> [visibility\_config](#input\_visibility\_config) | Visibility config for WAFv2 web acl. https://www.terraform.io/docs/providers/aws/r/wafv2_web_acl.html#visibility-configuration | `map(string)` | `{}` | no |
| <a name="input_visibility_config"></a> [visibility\_config](#input\_visibility\_config) | Visibility config for WAFv2 web acl. https://www.terraform.io/docs/providers/aws/r/wafv2_web_acl.
| <a name="custom_response_code"></a> [custom\_response\_code](#input\_custom\_response\_code) | A custom HTTP response status code for block actions. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl#custom-response | `number` | 403 | no |

## Outputs

Expand Down
12 changes: 10 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ resource "aws_wafv2_web_acl" "main" {

dynamic "block" {
for_each = var.allow_default_action ? [] : [1]
content {}
content {
custom_response {
response_code = var.custom_response_code
Copy link
Contributor

@Ohid25 Ohid25 May 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
response_code = var.custom_response_code
response_code = lookup(rule.custom_response, 403)

Instead of using a variable, a lookup function should be used since these arguments are filled in via the rules variable. This would eliminate the need to create a standalone variable for this.

Also, from the docs you sent I can see that there are two other arguments that can be used here based on terraform docs:

    "Block": {
     "CustomResponse": {
      "CustomResponseBodyKey": "CustomResponseBodyKey1",
      "ResponseCode": 404,
      "ResponseHeaders": [
       {
        "Name": "BlockActionHeader1Name",
        "Value": "BlockActionHeader1Value"
       }
      ]
     }

Can you please add these too?

}
}
}
}

Expand All @@ -43,7 +47,11 @@ resource "aws_wafv2_web_acl" "main" {

dynamic "block" {
for_each = lookup(rule.value, "action", {}) == "block" ? [1] : []
content {}
content {
custom_response {
response_code = var.custom_response_code
}
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,9 @@ variable "description" {
description = "A friendly description of the WebACL"
default = null
}

variable "custom_response_code" {
type = number
description = "The HTTP response code for non managed rule group statements block actions."
default = 403
}