Skip to content

Commit 3007caf

Browse files
robg-nhsrobg-test
andauthored
[NDR-13] WAF ACL for CloudFront resources (#289)
* [NDR-13] CloudFront Firewall Implementation * [NDR-13] Docs and Formatting * Updated name of resource from cloudwatch to cloudfront * Default web_acl_id to "" --------- Co-authored-by: robg-nhs <rob.gaskin1@nhs.net>
1 parent 283e969 commit 3007caf

File tree

11 files changed

+55
-21
lines changed

11 files changed

+55
-21
lines changed

infrastructure/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
| Name | Version |
1010
|------|---------|
11-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.86.1 |
11+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.95.0 |
1212

1313
## Modules
1414

@@ -39,8 +39,9 @@
3939
| <a name="module_bulk-upload-report-alarm-topic"></a> [bulk-upload-report-alarm-topic](#module\_bulk-upload-report-alarm-topic) | ./modules/sns | n/a |
4040
| <a name="module_bulk-upload-report-lambda"></a> [bulk-upload-report-lambda](#module\_bulk-upload-report-lambda) | ./modules/lambda | n/a |
4141
| <a name="module_bulk_upload_report_dynamodb_table"></a> [bulk\_upload\_report\_dynamodb\_table](#module\_bulk\_upload\_report\_dynamodb\_table) | ./modules/dynamo_db | n/a |
42-
| <a name="module_cloudfront-distribution-lg"></a> [cloudfront-distribution-lg](#module\_cloudfront-distribution-lg) | ./modules/cloudfront/ | n/a |
42+
| <a name="module_cloudfront-distribution-lg"></a> [cloudfront-distribution-lg](#module\_cloudfront-distribution-lg) | ./modules/cloudfront | n/a |
4343
| <a name="module_cloudfront_edge_dynamodb_table"></a> [cloudfront\_edge\_dynamodb\_table](#module\_cloudfront\_edge\_dynamodb\_table) | ./modules/dynamo_db | n/a |
44+
| <a name="module_cloudfront_firewall_waf_v2"></a> [cloudfront\_firewall\_waf\_v2](#module\_cloudfront\_firewall\_waf\_v2) | ./modules/firewall_waf_v2 | n/a |
4445
| <a name="module_create-doc-ref-gateway"></a> [create-doc-ref-gateway](#module\_create-doc-ref-gateway) | ./modules/gateway | n/a |
4546
| <a name="module_create-doc-ref-lambda"></a> [create-doc-ref-lambda](#module\_create-doc-ref-lambda) | ./modules/lambda | n/a |
4647
| <a name="module_create-token-gateway"></a> [create-token-gateway](#module\_create-token-gateway) | ./modules/gateway | n/a |

infrastructure/cloudfront.tf

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
module "cloudfront_firewall_waf_v2" {
2+
source = "./modules/firewall_waf_v2"
3+
cloudfront_acl = true
4+
5+
environment = var.environment
6+
owner = var.owner
7+
count = local.is_sandbox ? 0 : 1
8+
providers = { aws = aws.us_east_1 }
9+
}
10+
111
module "cloudfront-distribution-lg" {
2-
source = "./modules/cloudfront/"
12+
source = "./modules/cloudfront"
313
bucket_domain_name = "${terraform.workspace}-${var.lloyd_george_bucket_name}.s3.eu-west-2.amazonaws.com"
414
bucket_id = module.ndr-lloyd-george-store.bucket_id
515
qualifed_arn = module.edge-presign-lambda.qualified_arn
616
depends_on = [module.edge-presign-lambda.qualified_arn, module.ndr-lloyd-george-store.bucket_id, module.ndr-lloyd-george-store.bucket_domain_name]
7-
}
17+
web_acl_id = try(module.cloudfront_firewall_waf_v2[0].arn, "")
18+
}

infrastructure/firewall.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module "firewall_waf_v2" {
2-
source = "./modules/firewall_waf_v2"
3-
environment = var.environment
4-
owner = var.owner
5-
count = local.is_sandbox ? 0 : 1
2+
source = "./modules/firewall_waf_v2"
3+
cloudfront_acl = false
4+
environment = var.environment
5+
owner = var.owner
6+
count = local.is_sandbox ? 0 : 1
67
}
78

89
resource "aws_wafv2_web_acl_association" "web_acl_association" {
@@ -13,4 +14,5 @@ resource "aws_wafv2_web_acl_association" "web_acl_association" {
1314
module.ndr-ecs-fargate-app,
1415
module.firewall_waf_v2[0]
1516
]
16-
}
17+
}
18+

infrastructure/modules/app_config/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ No requirements.
66

77
| Name | Version |
88
|------|---------|
9-
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
9+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.96.0 |
1010

1111
## Modules
1212

infrastructure/modules/cloudfront/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ No requirements.
66

77
| Name | Version |
88
|------|---------|
9-
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
9+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.95.0 |
1010

1111
## Modules
1212

@@ -28,6 +28,7 @@ No modules.
2828
| <a name="input_bucket_domain_name"></a> [bucket\_domain\_name](#input\_bucket\_domain\_name) | Domain name to assign CloudFront distribution to | `string` | n/a | yes |
2929
| <a name="input_bucket_id"></a> [bucket\_id](#input\_bucket\_id) | Bucket ID to assign CloudFront distribution to | `string` | n/a | yes |
3030
| <a name="input_qualifed_arn"></a> [qualifed\_arn](#input\_qualifed\_arn) | Lambda@Edge function association | `string` | n/a | yes |
31+
| <a name="input_web_acl_id"></a> [web\_acl\_id](#input\_web\_acl\_id) | Web ACL to associate this Cloudfront distribution with | `string` | n/a | yes |
3132

3233
## Outputs
3334

infrastructure/modules/cloudfront/main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ resource "aws_cloudfront_distribution" "distribution" {
3636
locations = ["GB"]
3737
}
3838
}
39+
web_acl_id = var.web_acl_id
3940
}
4041

4142
resource "aws_cloudfront_origin_request_policy" "viewer_policy" {
@@ -90,4 +91,5 @@ resource "aws_cloudfront_cache_policy" "nocache" {
9091
query_string_behavior = "none"
9192
}
9293
}
93-
}
94+
}
95+

infrastructure/modules/cloudfront/variable.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,11 @@ variable "bucket_id" {
1111
variable "qualifed_arn" {
1212
type = string
1313
description = "Lambda@Edge function association"
14-
}
14+
}
15+
16+
variable "web_acl_id" {
17+
type = string
18+
description = "Web ACL to associate this Cloudfront distribution with"
19+
default = ""
20+
}
21+

infrastructure/modules/firewall_waf_v2/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ No requirements.
66

77
| Name | Version |
88
|------|---------|
9-
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
9+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.95.0 |
1010

1111
## Modules
1212

@@ -25,6 +25,7 @@ No modules.
2525

2626
| Name | Description | Type | Default | Required |
2727
|------|-------------|------|---------|:--------:|
28+
| <a name="input_cloudfront_acl"></a> [cloudfront\_acl](#input\_cloudfront\_acl) | n/a | `bool` | n/a | yes |
2829
| <a name="input_environment"></a> [environment](#input\_environment) | n/a | `string` | n/a | yes |
2930
| <a name="input_owner"></a> [owner](#input\_owner) | n/a | `string` | n/a | yes |
3031

infrastructure/modules/firewall_waf_v2/main.tf

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

66
default_action {
77
allow {}
@@ -50,6 +50,9 @@ resource "aws_wafv2_web_acl" "waf_v2_acl" {
5050
for_each = rule.value["excluded_rules"]
5151
content {
5252
name = excluded_rule.value
53+
action_to_use {
54+
allow {}
55+
}
5356
}
5457
}
5558

@@ -97,4 +100,5 @@ resource "aws_wafv2_web_acl" "waf_v2_acl" {
97100
Environment = var.environment
98101
Workspace = terraform.workspace
99102
}
100-
}
103+
}
104+

infrastructure/modules/firewall_waf_v2/regex.tf

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

66
# Allow pages involving images
77
regular_expression {
@@ -24,7 +24,7 @@ resource "aws_wafv2_regex_pattern_set" "large_body_uri" {
2424
resource "aws_wafv2_regex_pattern_set" "xss_body_uri" {
2525
name = "${terraform.workspace}-fw-waf-body-xss"
2626
description = "A regex to allow specific pages to bypass XSS checks on body"
27-
scope = "REGIONAL"
27+
scope = var.cloudfront_acl ? "CLOUDFRONT" : "REGIONAL"
2828

2929
# Allow pages involving images
3030
regular_expression {
@@ -42,7 +42,7 @@ resource "aws_wafv2_regex_pattern_set" "xss_body_uri" {
4242
resource "aws_wafv2_regex_pattern_set" "exclude_cms_uri" {
4343
name = "${terraform.workspace}-fw-waf-cms-exclude"
4444
description = "A regex to allow CMS calls to bypass firewalls"
45-
scope = "REGIONAL"
45+
scope = var.cloudfront_acl ? "CLOUDFRONT" : "REGIONAL"
4646

4747
# Allow pages involving images
4848
regular_expression {

0 commit comments

Comments
 (0)