Skip to content

Commit d5338ab

Browse files
Merge branch 'main' into PRM-134-v2
2 parents 2853b72 + bc1c4c1 commit d5338ab

File tree

9 files changed

+111
-26
lines changed

9 files changed

+111
-26
lines changed

.github/workflows/terraform-deploy-feature-to-sandbox.yml

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# .github/workflows/terraform-dev
2-
name: 'Deploy Feature Branch to Sandbox'
2+
name: "Deploy Feature Branch to Sandbox"
33

44
on:
55
workflow_dispatch:
66
inputs:
77
buildBranch:
8-
description: 'Feature branch to push to sandbox.'
8+
description: "Feature branch to push to sandbox."
99
required: true
10-
type: 'string'
10+
type: "string"
1111
sandboxWorkspace:
12-
description: 'Which Sandbox to push to.'
12+
description: "Which Sandbox to push to."
1313
required: true
14-
type: 'string'
14+
type: "string"
1515
environment:
16-
default: 'development'
17-
description: 'Which environment should this run against'
16+
default: "development"
17+
description: "Which environment should this run against"
1818
required: true
19-
type: 'string'
19+
type: "string"
2020

2121
permissions:
2222
pull-requests: write
@@ -29,11 +29,10 @@ jobs:
2929
environment: ${{ github.event.inputs.environment }}
3030

3131
steps:
32-
# Checkout the repository to the GitHub Actions runner
33-
- name: Checkout
32+
- name: Checkout Base
3433
uses: actions/checkout@v4
3534
with:
36-
ref: ${{ github.event.inputs.buildBranch}}
35+
ref: main
3736

3837
- name: Configure AWS Credentials
3938
uses: aws-actions/configure-aws-credentials@v4
@@ -53,23 +52,51 @@ jobs:
5352
terraform_version: 1.11.4
5453
terraform_wrapper: false
5554

56-
- name: Terraform Init
57-
id: init
55+
- name: Terraform Init Base
56+
id: base_init
5857
run: terraform init -backend-config=backend.conf
5958
working-directory: ./infrastructure
6059
shell: bash
6160

62-
- name: Terraform Set Workspace
63-
id: workspace
61+
- name: Terraform Set Workspace Base
62+
id: base_workspace
6463
run: terraform workspace select -or-create ${{ github.event.inputs.sandboxWorkspace}}
6564
working-directory: ./infrastructure
6665
shell: bash
6766

68-
# Checks that all Terraform configuration files adhere to a canonical format
67+
- name: Terraform Plan Base
68+
id: base_plan
69+
run: |
70+
terraform plan -input=false -no-color -var-file="${{vars.TF_VARS_FILE}}" -out tf-base.plan
71+
working-directory: ./infrastructure
72+
shell: bash
73+
74+
- name: Terraform Apply Base
75+
run: terraform apply -auto-approve -input=false tf-base.plan
76+
working-directory: ./infrastructure
77+
78+
- name: Checkout Branch
79+
uses: actions/checkout@v4
80+
with:
81+
ref: ${{ github.event.inputs.buildBranch}}
82+
83+
# Checks that all Terraform configuration files adhere to a canonical format.
6984
- name: Terraform Format
7085
run: terraform fmt -check
7186
working-directory: ./infrastructure
7287

88+
- name: Terraform Init
89+
id: init
90+
run: terraform init -backend-config=backend.conf
91+
working-directory: ./infrastructure
92+
shell: bash
93+
94+
- name: Terraform Set Workspace
95+
id: workspace
96+
run: terraform workspace select ${{ github.event.inputs.sandboxWorkspace}}
97+
working-directory: ./infrastructure
98+
shell: bash
99+
73100
- name: Terraform Plan
74101
id: plan
75102
run: |

infrastructure/README.md

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

180180
| Name | Type |
181181
|------|------|
182+
| [aws_api_gateway_account.logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_account) | resource |
182183
| [aws_api_gateway_api_key.api_key_pdm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_api_key) | resource |
183184
| [aws_api_gateway_api_key.apim](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_api_key) | resource |
184185
| [aws_api_gateway_authorizer.repo_authoriser](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_authorizer) | resource |
@@ -262,6 +263,7 @@
262263
| [aws_iam_policy.ses_send_email_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
263264
| [aws_iam_policy.ssm_access_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
264265
| [aws_iam_policy.ssm_access_policy_authoriser](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
266+
| [aws_iam_role.api_gateway_cloudwatch](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
265267
| [aws_iam_role.cognito_unauthenticated](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
266268
| [aws_iam_role.create_post_presign_url_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
267269
| [aws_iam_role.cross_account_backup_iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
@@ -274,6 +276,7 @@
274276
| [aws_iam_role.splunk_sqs_forwarder](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
275277
| [aws_iam_role.stitch_presign_url_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
276278
| [aws_iam_role_policy.splunk_access_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
279+
| [aws_iam_role_policy_attachment.api_gateway_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
277280
| [aws_iam_role_policy_attachment.backup_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
278281
| [aws_iam_role_policy_attachment.cloudwatch_rum_cognito_unauth](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
279282
| [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)