Skip to content

Commit 97be701

Browse files
authored
Use a cloudfront function to inject the origin key (#229)
1 parent 5fe9ff5 commit 97be701

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

terraform/envs/prod/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module "dynamo" {
5555
}
5656

5757
resource "random_password" "origin_verify_key" {
58-
length = 20
58+
length = 16
5959
special = false
6060
keepers = {
6161
force_recreation = formatdate("DD-MMM-YYYY", plantimestamp())

terraform/envs/qa/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module "dynamo" {
5757
}
5858

5959
resource "random_password" "origin_verify_key" {
60-
length = 20
60+
length = 16
6161
special = false
6262
keepers = {
6363
force_recreation = formatdate("DD-MMM-YYYY", plantimestamp())

terraform/modules/frontend/main.tf

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ resource "aws_cloudfront_distribution" "app_cloudfront_distribution" {
100100
origin_protocol_policy = "https-only"
101101
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
102102
}
103-
custom_header {
104-
name = "X-Origin-Verify"
105-
value = var.OriginVerifyKey
106-
}
107103
}
108104
default_root_object = "index.html"
109105
aliases = [var.CorePublicDomain]
@@ -140,6 +136,10 @@ resource "aws_cloudfront_distribution" "app_cloudfront_distribution" {
140136
cache_policy_id = aws_cloudfront_cache_policy.headers_no_cookies.id
141137
origin_request_policy_id = "b689b0a8-53d0-40ab-baf2-68738e2966ac"
142138
compress = true
139+
function_association {
140+
event_type = "viewer-request"
141+
function_arn = aws_cloudfront_function.origin_key_injection.arn
142+
}
143143
}
144144
ordered_cache_behavior {
145145
path_pattern = "/api/v1/organizations"
@@ -150,6 +150,10 @@ resource "aws_cloudfront_distribution" "app_cloudfront_distribution" {
150150
cache_policy_id = "658327ea-f89d-4fab-a63d-7e88639e58f6"
151151
origin_request_policy_id = "b689b0a8-53d0-40ab-baf2-68738e2966ac"
152152
compress = true
153+
function_association {
154+
event_type = "viewer-request"
155+
function_arn = aws_cloudfront_function.origin_key_injection.arn
156+
}
153157
}
154158
ordered_cache_behavior {
155159
path_pattern = "/api/*"
@@ -160,6 +164,10 @@ resource "aws_cloudfront_distribution" "app_cloudfront_distribution" {
160164
cache_policy_id = aws_cloudfront_cache_policy.no_cache.id
161165
origin_request_policy_id = "b689b0a8-53d0-40ab-baf2-68738e2966ac"
162166
compress = true
167+
function_association {
168+
event_type = "viewer-request"
169+
function_arn = aws_cloudfront_function.origin_key_injection.arn
170+
}
163171
}
164172
price_class = "PriceClass_100"
165173
}
@@ -176,10 +184,6 @@ resource "aws_cloudfront_distribution" "ical_cloudfront_distribution" {
176184
origin_protocol_policy = "https-only"
177185
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
178186
}
179-
custom_header {
180-
name = "X-Origin-Verify"
181-
value = var.OriginVerifyKey
182-
}
183187
}
184188
aliases = [var.IcalPublicDomain]
185189
enabled = true
@@ -192,6 +196,10 @@ resource "aws_cloudfront_distribution" "ical_cloudfront_distribution" {
192196
cached_methods = ["GET", "HEAD"]
193197
cache_policy_id = aws_cloudfront_cache_policy.headers_no_cookies.id
194198
origin_request_policy_id = "b689b0a8-53d0-40ab-baf2-68738e2966ac"
199+
function_association {
200+
event_type = "viewer-request"
201+
function_arn = aws_cloudfront_function.origin_key_injection.arn
202+
}
195203
}
196204
viewer_certificate {
197205
acm_certificate_arn = var.CoreCertificateArn
@@ -206,6 +214,19 @@ resource "aws_cloudfront_distribution" "ical_cloudfront_distribution" {
206214
price_class = "PriceClass_100"
207215
}
208216

217+
resource "aws_cloudfront_function" "origin_key_injection" {
218+
name = "${var.ProjectId}-origin-verification-injection"
219+
comment = "Injects origin verification key into requests"
220+
runtime = "cloudfront-js-2.0"
221+
code = <<EOT
222+
function handler(event) {
223+
var request = event.request;
224+
request.headers['x-origin-verify'] = { value: "${var.OriginVerifyKey}" };
225+
return request;
226+
}
227+
EOT
228+
}
229+
209230
resource "aws_cloudfront_function" "core_frontend_redirect" {
210231
name = "${var.ProjectId}-spa-rewrite"
211232
comment = "Handles SPA routing by rewriting URIs to index.html"

0 commit comments

Comments
 (0)