-
Notifications
You must be signed in to change notification settings - Fork 127
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
Changes from 11 commits
158c862
67b3ab1
76ea15c
f359974
70baae9
3e22c64
d76604b
e091118
b4c561b
0766d35
1cc10e7
a451063
68d6460
8eae1b6
b4ff784
128d9be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
provider "aws" { | ||
region = "eu-west-1" | ||
} | ||
|
||
module "waf" { | ||
source = "../.." | ||
|
||
name_prefix = var.name_prefix | ||
allow_default_action = true | ||
|
||
create_alb_association = false | ||
|
||
visibility_config = { | ||
cloudwatch_metrics_enabled = false | ||
metric_name = "${var.name_prefix}-waf-setup-waf-main-metrics" | ||
sampled_requests_enabled = false | ||
} | ||
|
||
rules = [ | ||
{ | ||
name = "ip-rate-based" | ||
priority = "6" | ||
action = "block" | ||
|
||
custom_response = true | ||
custom_response_code = 412 | ||
|
||
rate_based_statement = { | ||
limit = 2000 # Note this is by default in a 5-min span, ref: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl#rate_based_statement | ||
aggregate_key_type = "IP" | ||
} | ||
|
||
visibility_config = { | ||
cloudwatch_metrics_enabled = false | ||
metric_name = "IPRateBased-metric" | ||
sampled_requests_enabled = false | ||
} | ||
}, | ||
{ | ||
# Note: custom responses can not be applied to AWS managed rule groups directly. Must use a label technical, ref: https://aws.amazon.com/blogs/security/how-to-customize-behavior-of-aws-managed-rules-for-aws-waf/ | ||
name = "AWSManagedRulesBotControlRuleSet-rule-0" | ||
priority = "0" | ||
|
||
# Note: override_action is for managed rule sets only, otherwise would be action | ||
override_action = "none" | ||
|
||
managed_rule_group_statement = { | ||
name = "AWSManagedRulesBotControlRuleSet" | ||
vendor_name = "AWS", | ||
excluded_rule = [ | ||
"SignalNonBrowserUserAgent", | ||
"CategoryHttpLibrary", | ||
"SignalAutomatedBrowser", | ||
"CategoryMonitoring" | ||
] | ||
} | ||
|
||
visibility_config = { | ||
cloudwatch_metrics_enabled = false | ||
metric_name = "AWSManagedRulesBotControlRuleSet-metric" | ||
sampled_requests_enabled = false | ||
} | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
output "web_acl_name" { | ||
description = "The name of the WAFv2 WebACL." | ||
value = module.waf.web_acl_name | ||
} | ||
|
||
output "web_acl_arn" { | ||
description = "The ARN of the WAFv2 WebACL." | ||
value = module.waf.web_acl_arn | ||
} | ||
|
||
output "web_acl_capacity" { | ||
description = "The web ACL capacity units (WCUs) currently being used by this web ACL." | ||
value = module.waf.web_acl_capacity | ||
} | ||
|
||
output "web_acl_visibility_config_name" { | ||
description = "The web ACL visibility config name" | ||
value = module.waf.web_acl_visibility_config_name | ||
} | ||
|
||
output "web_acl_rule_names" { | ||
description = "List of created rule names" | ||
value = module.waf.web_acl_rule_names | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
variable "name_prefix" { | ||
description = "A prefix used for naming resources." | ||
type = string | ||
default = "example" | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,89 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
provider "aws" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
region = "eu-west-1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
module "waf" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
source = "../.." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
name_prefix = var.name_prefix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
allow_default_action = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
create_alb_association = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
visibility_config = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cloudwatch_metrics_enabled = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
metric_name = "${var.name_prefix}-waf-setup-waf-main-metrics" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sampled_requests_enabled = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
custom_response_bodies = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
key = "custom_response_body_1", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
content = "You are not authorized to access this resource.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
content_type = "TEXT_PLAIN" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
key = "custom_response_body_2", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
content = "You there, are not authorized to access this resource.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
content_type = "TEXT_PLAIN" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rules = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
name = "ip-rate-based" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
priority = "6" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
action = "block" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
custom_response = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
custom_response_code = 412 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
custom_response_key = "custom_response_body_1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
custom_response_headers = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
name = "X-Custom-Header" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
value = "You are not authorized to access this resource." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
name = "X-Custom-Header-2" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
value = "You are still not authorized to access this resource." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
nit: nested instead of flat |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rate_based_statement = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
limit = 2000 # Note this is by default in a 5-min span, ref: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl#rate_based_statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
aggregate_key_type = "IP" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
visibility_config = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cloudwatch_metrics_enabled = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
metric_name = "IPRateBased-metric" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sampled_requests_enabled = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Note: custom responses can not be applied to AWS managed rule groups directly. Must use a label technical, ref: https://aws.amazon.com/blogs/security/how-to-customize-behavior-of-aws-managed-rules-for-aws-waf/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
name = "AWSManagedRulesBotControlRuleSet-rule-0" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
priority = "0" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Note: override_action is for managed rule sets only, otherwise would be action | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
override_action = "none" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
managed_rule_group_statement = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
name = "AWSManagedRulesBotControlRuleSet" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vendor_name = "AWS", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
excluded_rule = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"SignalNonBrowserUserAgent", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"CategoryHttpLibrary", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"SignalAutomatedBrowser", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"CategoryMonitoring" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
visibility_config = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cloudwatch_metrics_enabled = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
metric_name = "AWSManagedRulesBotControlRuleSet-metric" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sampled_requests_enabled = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion. Was working on this very change. :) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
output "web_acl_name" { | ||
description = "The name of the WAFv2 WebACL." | ||
value = module.waf.web_acl_name | ||
} | ||
|
||
output "web_acl_arn" { | ||
description = "The ARN of the WAFv2 WebACL." | ||
value = module.waf.web_acl_arn | ||
} | ||
|
||
output "web_acl_capacity" { | ||
description = "The web ACL capacity units (WCUs) currently being used by this web ACL." | ||
value = module.waf.web_acl_capacity | ||
} | ||
|
||
output "web_acl_visibility_config_name" { | ||
description = "The web ACL visibility config name" | ||
value = module.waf.web_acl_visibility_config_name | ||
} | ||
|
||
output "web_acl_rule_names" { | ||
description = "List of created rule names" | ||
value = module.waf.web_acl_rule_names | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
variable "name_prefix" { | ||
description = "A prefix used for naming resources." | ||
type = string | ||
default = "example" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
visibility_config
will be required in both of the rules - see here: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl#visibility_configThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the catch here as well... nuked the
visibility_config
from the copy of my personal block because had local variables I didn't want to bother with here. But just added the block ✔️ .