Skip to content

Commit 3e3af7c

Browse files
authored
[NDR-144] Remove BotControl from firewall associated with API (#345)
* [NDR-144] adding api var and logic to exclude botcontrol * [NDR-144] quick fix * [NDR-144] deponds on * [NDR-144] changing name if api * [NDR-144] temp remove sandbox restriction * [NDR-144] regex patter name change * [NDR-144] remove sandbox block * [NDR-144] removing index while count is removed * [NDR-144] and again * [NDR - 144] re-adding sandbox count * [NDR-144] variable description --------- Co-authored-by: Sam Whyte <[email protected]>
1 parent 4efe6aa commit 3e3af7c

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

infrastructure/firewall.tf

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ module "firewall_waf_v2" {
66
count = local.is_sandbox ? 0 : 1
77
}
88

9+
module "firewall_waf_v2_api" {
10+
source = "./modules/firewall_waf_v2"
11+
cloudfront_acl = false
12+
environment = var.environment
13+
owner = var.owner
14+
count = local.is_sandbox ? 0 : 1
15+
api = true
16+
}
17+
918
resource "aws_wafv2_web_acl_association" "web_acl_association" {
1019
resource_arn = module.ndr-ecs-fargate-app.load_balancer_arn
1120
web_acl_arn = module.firewall_waf_v2[0].arn
@@ -18,10 +27,10 @@ resource "aws_wafv2_web_acl_association" "web_acl_association" {
1827

1928
resource "aws_wafv2_web_acl_association" "api_gateway" {
2029
resource_arn = aws_api_gateway_stage.ndr_api.arn
21-
web_acl_arn = module.firewall_waf_v2[0].arn
30+
web_acl_arn = module.firewall_waf_v2_api[0].arn
2231
count = local.is_sandbox ? 0 : 1
2332
depends_on = [
2433
aws_api_gateway_stage.ndr_api,
25-
module.firewall_waf_v2[0]
34+
module.firewall_waf_v2_api[0]
2635
]
2736
}

infrastructure/modules/firewall_waf_v2/local.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ locals {
22

33
image_regex = "^\\/images(\\/\\w+)+\\/$"
44

5-
waf_rules = [
5+
waf_rules_raw = [
66
{
77
name = "AWSCoreRuleSet"
88
managed_rule_name = "AWSManagedRulesCommonRuleSet"
@@ -47,8 +47,14 @@ locals {
4747
}
4848
]
4949

50+
# Filter out AWSBotControl if var.api is true
51+
waf_rules = [
52+
for rule in local.waf_rules_raw : rule
53+
if !(var.api && rule.name == "AWSBotControl")
54+
]
55+
5056
waf_rules_map = zipmap(
5157
range(0, length(local.waf_rules)),
5258
local.waf_rules
5359
)
54-
}
60+
}

infrastructure/modules/firewall_waf_v2/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "aws_wafv2_web_acl" "waf_v2_acl" {
2-
name = "${terraform.workspace}-${var.cloudfront_acl ? "cloudfront" : ""}-fw-waf-v2"
2+
name = "${terraform.workspace}${var.api ? "-api" : var.cloudfront_acl ? "-cloudfront" : ""}-fw-waf-v2"
33
description = "A WAF to secure the Repo application."
44
scope = var.cloudfront_acl ? "CLOUDFRONT" : "REGIONAL"
55

infrastructure/modules/firewall_waf_v2/regex.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "aws_wafv2_regex_pattern_set" "large_body_uri" {
2-
name = "${terraform.workspace}-fw-waf-body-size"
2+
name = "${terraform.workspace}-fw-waf-body-size${var.api ? "-api" : ""}"
33
description = "A set of regex to allow specific pages to bypass the large body check"
44
scope = var.cloudfront_acl ? "CLOUDFRONT" : "REGIONAL"
55

@@ -22,7 +22,7 @@ resource "aws_wafv2_regex_pattern_set" "large_body_uri" {
2222
}
2323

2424
resource "aws_wafv2_regex_pattern_set" "xss_body_uri" {
25-
name = "${terraform.workspace}-fw-waf-body-xss"
25+
name = "${terraform.workspace}-fw-waf-body-xss${var.api ? "-api" : ""}"
2626
description = "A regex to allow specific pages to bypass XSS checks on body"
2727
scope = var.cloudfront_acl ? "CLOUDFRONT" : "REGIONAL"
2828

@@ -40,7 +40,7 @@ resource "aws_wafv2_regex_pattern_set" "xss_body_uri" {
4040
}
4141

4242
resource "aws_wafv2_regex_pattern_set" "exclude_cms_uri" {
43-
name = "${terraform.workspace}-fw-waf-cms-exclude"
43+
name = "${terraform.workspace}-fw-waf-cms-exclude${var.api ? "-api" : ""}"
4444
description = "A regex to allow CMS calls to bypass firewalls"
4545
scope = var.cloudfront_acl ? "CLOUDFRONT" : "REGIONAL"
4646

@@ -55,4 +55,4 @@ resource "aws_wafv2_regex_pattern_set" "exclude_cms_uri" {
5555
Environment = var.environment
5656
Workspace = terraform.workspace
5757
}
58-
}
58+
}

infrastructure/modules/firewall_waf_v2/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ variable "cloudfront_acl" {
1010
type = bool
1111
}
1212

13+
variable "api" {
14+
type = bool
15+
description = "True if using the firewall for an api - removes AWSBotControl"
16+
default = false
17+
}
18+

0 commit comments

Comments
 (0)