Skip to content

Commit 05473aa

Browse files
Merge pull request #42 from NHSDigital/CCM-8397_WafIpAllowLists
CCM-8397 Permit GHA IP Whitelists
2 parents 79a2c2c + e7ca27d commit 05473aa

File tree

7 files changed

+106
-9
lines changed

7 files changed

+106
-9
lines changed

infrastructure/terraform/components/cdn/cloudfront_distribution_cdn.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ resource "aws_cloudfront_distribution" "main" {
1010

1111
restrictions {
1212
geo_restriction {
13-
restriction_type = "whitelist"
14-
locations = ["GB"]
13+
restriction_type = "none" # Moved to WAF
14+
locations = [] # Moved to WAF
1515
}
1616
}
1717

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data "github_ip_ranges" "main" {}

infrastructure/terraform/components/cdn/provider.tf

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ provider "aws" {
1616
tags = local.default_tags
1717
}
1818
}
19+
20+
provider "github" {}

infrastructure/terraform/components/cdn/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ variable "log_level" {
6363
default = "INFO"
6464
}
6565

66+
variable "enable_github_actions_ip_access" {
67+
type = bool
68+
description = "Should the Github actions runner IP addresses be permitted access to this distribution. This should not be enabled in production environments"
69+
default = false
70+
}
71+
6672
variable "log_retention_in_days" {
6773
type = number
6874
description = "The retention period in days for the Cloudwatch Logs events to be retained, default of 0 is indefinite"

infrastructure/terraform/components/cdn/versions.tf

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ terraform {
44
source = "hashicorp/aws"
55
version = "~> 5.50"
66
}
7+
github = {
8+
source = "integrations/github"
9+
version = "~> 6.0"
10+
}
11+
712
}
813

914
required_version = ">= 1.9.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
resource "aws_wafv2_ip_set" "github_actions_ipv4" {
2+
count = var.enable_github_actions_ip_access ? 1:0
3+
4+
provider = aws.us-east-1
5+
6+
name = "${local.csi}-github-actions-ipv4"
7+
description = "Public references for github actions runner IP addresses"
8+
scope = "CLOUDFRONT"
9+
ip_address_version = "IPV4"
10+
addresses = data.github_ip_ranges.main.actions_ipv4
11+
}
12+
13+
resource "aws_wafv2_ip_set" "github_actions_ipv6" {
14+
count = var.enable_github_actions_ip_access ? 1:0
15+
16+
provider = aws.us-east-1
17+
18+
name = "${local.csi}-github-actions-ipv6"
19+
description = "Public references for github actions runner IP addresses"
20+
scope = "CLOUDFRONT"
21+
ip_address_version = "IPV6"
22+
addresses = data.github_ip_ranges.main.actions_ipv6
23+
}

infrastructure/terraform/components/cdn/wafv2_web_acl.tf

+67-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,76 @@ resource "aws_wafv2_web_acl" "main" {
22
provider = aws.us-east-1
33

44
name = local.csi
5-
description = "${var.environment} with no IP Allowlist"
5+
description = "${var.environment} WAF"
66
scope = "CLOUDFRONT"
77

88
default_action {
99
allow {}
1010
}
1111

12+
dynamic "rule" {
13+
for_each = var.enable_github_actions_ip_access ? [1] : []
14+
15+
content {
16+
name = "GithubActionsIPRestriction"
17+
priority = 10
18+
19+
action {
20+
allow {}
21+
}
22+
23+
statement {
24+
or_statement {
25+
statement {
26+
ip_set_reference_statement {
27+
arn = aws_wafv2_ip_set.github_actions_ipv4[0].arn
28+
}
29+
}
30+
31+
statement {
32+
ip_set_reference_statement {
33+
arn = aws_wafv2_ip_set.github_actions_ipv6[0].arn
34+
}
35+
}
36+
}
37+
}
38+
39+
visibility_config {
40+
metric_name = "${local.csi}_gha_ip_restrictions_metric"
41+
cloudwatch_metrics_enabled = true
42+
sampled_requests_enabled = true
43+
}
44+
}
45+
}
46+
47+
rule {
48+
name = "GeoLocationTrafficWhitelist"
49+
priority = 20
50+
51+
action {
52+
block {}
53+
}
54+
55+
statement {
56+
not_statement {
57+
statement {
58+
geo_match_statement {
59+
country_codes = ["GB"]
60+
}
61+
}
62+
}
63+
}
64+
65+
visibility_config {
66+
cloudwatch_metrics_enabled = true
67+
sampled_requests_enabled = true
68+
metric_name = "${local.csi}_geo_location_whitelist"
69+
}
70+
}
71+
1272
rule {
1373
name = "AWSManagedRulesCommonRuleSet"
14-
priority = 10
74+
priority = 30
1575
override_action {
1676
none {}
1777
}
@@ -37,7 +97,7 @@ resource "aws_wafv2_web_acl" "main" {
3797

3898
rule {
3999
name = "AWSManagedRulesKnownBadInputsRuleSet"
40-
priority = 20
100+
priority = 40
41101
override_action {
42102
none {}
43103
}
@@ -56,7 +116,7 @@ resource "aws_wafv2_web_acl" "main" {
56116

57117
rule {
58118
name = "AWSManagedRulesSQLiRuleSet"
59-
priority = 30
119+
priority = 50
60120
override_action {
61121
none {}
62122
}
@@ -75,7 +135,7 @@ resource "aws_wafv2_web_acl" "main" {
75135

76136
rule {
77137
name = "AWSManagedRulesAmazonIpReputationList"
78-
priority = 40
138+
priority = 60
79139
override_action {
80140
none {}
81141
}
@@ -93,8 +153,8 @@ resource "aws_wafv2_web_acl" "main" {
93153
}
94154

95155
rule {
96-
name = "rate-limit"
97-
priority = 50
156+
name = "RateLimit"
157+
priority = 100
98158
action {
99159
block {}
100160
}

0 commit comments

Comments
 (0)