forked from ustaxcourt/ef-cms
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #392 from ustaxcourt/cloudfront-for-documents
386: Serve documents from app host.
- Loading branch information
Showing
13 changed files
with
287 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
resource "aws_iam_role" "strip_basepath_lambda_role" { | ||
name = "strip_basepath_lambda_role_${var.environment}" | ||
|
||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Principal": { | ||
"Service": ["lambda.amazonaws.com", "edgelambda.amazonaws.com"] | ||
}, | ||
"Effect": "Allow", | ||
"Sid": "" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
|
||
resource "aws_iam_role_policy" "strip_basepath_lambda_policy" { | ||
name = "strip_basepath_lambda_policy_${var.environment}" | ||
role = aws_iam_role.strip_basepath_lambda_role.id | ||
|
||
policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"logs:CreateLogGroup", | ||
"logs:CreateLogStream", | ||
"logs:PutLogEvents" | ||
], | ||
"Resource": "arn:aws:logs:*:*:*" | ||
} | ||
] | ||
} | ||
EOF | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* | ||
* @params {object} params the params object | ||
* @param {string} documentUrl URL to the document in regionalEndpoint.com/bucket/path format | ||
* @param {boolean} useTempBucket If the document is in the temporary documents or not | ||
* @param {object} applicationContext the application context | ||
* | ||
* @returns {string} the translated URL | ||
*/ | ||
exports.documentUrlTranslator = ({ | ||
applicationContext, | ||
documentUrl, | ||
useTempBucket, | ||
}) => { | ||
if (applicationContext.environment.stage === 'local') { | ||
return documentUrl; | ||
} | ||
|
||
const url = new URL(documentUrl); | ||
const path = useTempBucket ? 'temp-documents' : 'documents'; | ||
|
||
url.host = applicationContext.getAppEndpoint(); | ||
url.pathname = [path, ...url.pathname.split('/').slice(2)].join('/'); | ||
|
||
return url.toString(); | ||
}; |
47 changes: 47 additions & 0 deletions
47
shared/src/business/utilities/documentUrlTranslator.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const { applicationContext } = require('../test/createTestApplicationContext'); | ||
const { documentUrlTranslator } = require('./documentUrlTranslator'); | ||
|
||
describe('documentUrlTranslator', () => { | ||
const documentUrl = | ||
'https://s3.region-name.amazonaws.com/bucketName/documentPath?AWSAccessKeyId=KeyId&Expires=999&Signature=SignatureString'; | ||
|
||
test('the original url is returned when the environment’s stage is local', () => { | ||
applicationContext.environment.stage = 'local'; | ||
|
||
const translatedUrl = documentUrlTranslator({ | ||
applicationContext, | ||
documentUrl, | ||
useTempBucket: false, | ||
}); | ||
|
||
expect(translatedUrl).toBe(documentUrl); | ||
}); | ||
|
||
test('temporary documents are rewritten to the app host’s cloudfront temp-documents endpoint', () => { | ||
applicationContext.environment.stage = 'prod'; | ||
|
||
const translatedUrl = documentUrlTranslator({ | ||
applicationContext, | ||
documentUrl, | ||
useTempBucket: true, | ||
}); | ||
|
||
const expectedUrl = | ||
'https://localhost:1234/temp-documents/documentPath?AWSAccessKeyId=KeyId&Expires=999&Signature=SignatureString'; | ||
expect(translatedUrl).toBe(expectedUrl); | ||
}); | ||
|
||
test('non-temporary documents are rewritten to the app host’s cloudfront document endpoint', () => { | ||
applicationContext.environment.stage = 'prod'; | ||
|
||
const translatedUrl = documentUrlTranslator({ | ||
applicationContext, | ||
documentUrl, | ||
useTempBucket: false, | ||
}); | ||
|
||
const expectedUrl = | ||
'https://localhost:1234/documents/documentPath?AWSAccessKeyId=KeyId&Expires=999&Signature=SignatureString'; | ||
expect(translatedUrl).toBe(expectedUrl); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
web-client/terraform/common/cloudfront-edge/strip-basepath-lambda.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Removes the base path (the top-level folder) from the request URL. | ||
* | ||
* @param {object} event the AWS event object | ||
* @param {object} context the context | ||
* @param {object} callback the callback | ||
*/ | ||
exports.handler = (event, context, callback) => { | ||
const { request } = event.Records[0].cf; | ||
request.uri = `/${[...request.uri.split('/').slice(2)].join('/')}`; | ||
callback(null, request); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.