Skip to content

Commit 2a7dbd5

Browse files
Merge remote-tracking branch 'origin/main' into PRMT-466
2 parents efa094f + 5ff7826 commit 2a7dbd5

File tree

8 files changed

+68
-10
lines changed

8 files changed

+68
-10
lines changed

infrastructure/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178

179179
| Name | Type |
180180
|------|------|
181+
| [aws_api_gateway_account.logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_account) | resource |
181182
| [aws_api_gateway_api_key.api_key_pdm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_api_key) | resource |
182183
| [aws_api_gateway_api_key.apim](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_api_key) | resource |
183184
| [aws_api_gateway_authorizer.repo_authoriser](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_authorizer) | resource |
@@ -260,6 +261,7 @@
260261
| [aws_iam_policy.ses_send_email_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
261262
| [aws_iam_policy.ssm_access_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
262263
| [aws_iam_policy.ssm_access_policy_authoriser](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
264+
| [aws_iam_role.api_gateway_cloudwatch](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
263265
| [aws_iam_role.cognito_unauthenticated](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
264266
| [aws_iam_role.create_post_presign_url_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
265267
| [aws_iam_role.cross_account_backup_iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
@@ -272,6 +274,7 @@
272274
| [aws_iam_role.splunk_sqs_forwarder](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
273275
| [aws_iam_role.stitch_presign_url_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
274276
| [aws_iam_role_policy.splunk_access_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
277+
| [aws_iam_role_policy_attachment.api_gateway_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
275278
| [aws_iam_role_policy_attachment.backup_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
276279
| [aws_iam_role_policy_attachment.cloudwatch_rum_cognito_unauth](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
277280
| [aws_iam_role_policy_attachment.create_post_presign_url](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |

infrastructure/api.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,19 @@ resource "aws_api_gateway_stage" "ndr_api" {
9393
stage_name = var.environment
9494
xray_tracing_enabled = var.enable_xray_tracing
9595

96-
depends_on = [aws_cloudwatch_log_group.api_gateway_stage]
96+
depends_on = [
97+
aws_cloudwatch_log_group.api_gateway_stage
98+
]
9799
}
98100

99101
resource "aws_cloudwatch_log_group" "api_gateway_stage" {
100102
# Name must follow this format to allow execution logging
101103
# https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html
102104
name = "API-Gateway-Execution-Logs_${aws_api_gateway_rest_api.ndr_doc_store_api.id}/${var.environment}"
103105
retention_in_days = 0
106+
depends_on = [
107+
aws_api_gateway_account.logging
108+
]
104109
}
105110

106111
resource "aws_api_gateway_method_settings" "api_gateway_stage" {

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/iam.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,32 @@ resource "aws_iam_role_policy_attachment" "ods_report_presign_url" {
193193
role = aws_iam_role.ods_report_presign_url_role.name
194194
policy_arn = aws_iam_policy.s3_document_data_policy_for_ods_report_lambda.arn
195195
}
196+
197+
resource "aws_iam_role" "api_gateway_cloudwatch" {
198+
count = local.is_sandbox ? 0 : 1
199+
name = "${terraform.workspace}_NdrAPIGatewayLogs"
200+
201+
assume_role_policy = jsonencode({
202+
Version = "2012-10-17"
203+
Statement = [
204+
{
205+
Action = "sts:AssumeRole"
206+
Effect = "Allow"
207+
Principal = {
208+
Service = "apigateway.amazonaws.com"
209+
}
210+
},
211+
]
212+
})
213+
}
214+
215+
resource "aws_iam_role_policy_attachment" "api_gateway_logs" {
216+
count = local.is_sandbox ? 0 : 1
217+
role = aws_iam_role.api_gateway_cloudwatch[0].name
218+
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
219+
}
220+
221+
resource "aws_api_gateway_account" "logging" {
222+
count = local.is_sandbox ? 0 : 1
223+
cloudwatch_role_arn = aws_iam_role.api_gateway_cloudwatch[0].arn
224+
}

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)