-
Notifications
You must be signed in to change notification settings - Fork 78
Kvs updates #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Kvs updates #45
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
332876c
Create README.md
lkdavies 04d3d50
Update verify-jwt.js
lkdavies b137bd6
Update ABUriMappingFunction.js
lkdavies 6577620
Create expired-jwt.json
lkdavies cb68a21
Rename invalid-jwt.json to invalid-jwt.json
lkdavies 5fecae3
Rename invalid-nbe-jwt.json to invalid-nbe-jwt.json
lkdavies ba0fc7c
Rename missing-jwt.json to missing-jwt.json
lkdavies ec26de2
Rename valid-jwt.json to valid-jwt.json
lkdavies aebcaa8
Delete verify-jwt directory
lkdavies c9f864f
Create normalize-query-string.js
lkdavies 9e85f34
Create README.md
lkdavies ce8899c
Create kvs-key-value-pairs.js
lkdavies 920ecfb
Create README.md
lkdavies ce1542f
Create README.md
lkdavies 46c76b4
Update README.md
lkdavies b89dd05
Update README.md
lkdavies 23125bb
Update README.md
lkdavies 4a3c686
Update README.md
lkdavies 85732aa
Update verify-jwt.js
lkdavies d1561f6
Update README.md
lkdavies 0289868
Update kvs-key-value-pairs.js
lkdavies faa87f9
Update kvs-key-value-pairs.js
lkdavies bfb6668
Update kvs-key-value-pairs.js
lkdavies 4adcdc3
Update verify-jwt.js
lkdavies 257053b
Update verify-jwt.js
lkdavies b71db44
Update verify-jwt.js
lkdavies c12a682
Update kvs-key-value-pairs.js
lkdavies File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,5 @@ | ||
## Rewrite request URI | ||
|
||
**CloudFront Functions event type: viewer request** | ||
|
||
This example provides a dynamic request URI rewriting mechanism, allowing for A/B testing or gradual rollout of application versions, while also maintaining user stickiness to ensure a consistent experience. It rewrites the request URI based on a configuration stored in CloudFront KeyValueStore. To use this example, you [create a KeyValueStore](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/kvs-with-functions-create.html) for your secret and [associate the KeyValueStore with the CloudFront function](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/kvs-with-functions-associate.html). |
This file contains hidden or 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,60 @@ | ||
## Verify a JSON Web Token (JWT) using SHA256 HMAC signature | ||
|
||
**CloudFront Functions event type: viewer request** | ||
|
||
This function validates a JSON Web Token (JWT) in the query string of the incoming request. It is compatible with the CloudFront Functions [JavaScript 2.0 runtime](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-20.html) and uses [CloudFront KeyValueStore](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/kvs-with-functions.html) to store the secret. Using your CloudFront KeyValueStore ID is optional. | ||
|
||
JWT is an open, industry standard [RFC 7519](https://tools.ietf.org/html/rfc7519) method for representing claims securely between two parties. You can use JWTs to validate that a viewer has the right access to view the content being requested. You can use this type of tokenization to give a user of your site a URL that is timebound. After the predetermined expiry time is reached, the user no longer has access to the content on that URL. | ||
|
||
This function has two components. First, your origin or application must be able to generate a JWT and append that token as a query string to the URL. Second, you must use this sample function (or some variation of this function) on a viewer request event type to validate the JWT in the query string, ensuring that the URL hasn't been changed or tampered with and the expiry time hasn't passed. If the token is valid and the expiry time hasn't passed, the request passes through to CloudFront and the request is served. If the token is invalid or the expiry time has passed, the function generates and serves a 401 Unauthorized response to the viewer. | ||
|
||
In this example, your origin or application establishes a JWT. We have provided a simple bash script for building a JWT called `generate-jwt.sh`. There are many libraries across multiple different languages for signing and verifying JWTs available on [jwt.io](https://jwt.io/). | ||
|
||
The output of `generate-jwt.sh` is the JWT that the function will validate. Append the output to the URL as a query string in the following format `token=<generated JWT>` (for example, `token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJuYmYiOjE1MTYyMzkwMjIsImV4cCI6MTcxNjIzOTAyMn0.jyu6HjS95wU8iSofQ8nBlmPjFYODxn4PQAdFM-Cv8JY`). | ||
|
||
CloudFront already provides a [signed URLs](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-urls.html) feature that you can use instead of this function. A signed URL can include additional information, such as an expiration date and time, start date and time, and client IP address. This gives you more control over access to your content. However, creating a signed URL creates long and complex URLs and is more computationally costly to produce. If you need a simple and lightweight way to validate timebound URLs, this function can be easier than using CloudFront signed URLs. | ||
|
||
**Testing the function** | ||
|
||
To validate that the function is working as expected, you can use the JSON test objects in the `test-objects` directory. To test, you can use the `test-function` CLI command as shown in the following example: | ||
|
||
``` | ||
$ aws cloudfront test-function --if-match EXXXXXXXXXXXX --name kvs-jwt-verify --event-object fileb://kvs-jwt-verify/test-objects/valid-jwt.json | ||
``` | ||
|
||
If the function has been set up correctly, you should see a log entry saying the token is valid in the `FunctionExecutionLogs` and JWT token removed in the `FunctionOutput` JSON object: | ||
``` | ||
{ | ||
"TestResult": { | ||
"FunctionSummary": { | ||
"Name": "kvs-jwt-verify", | ||
"Status": "UNASSOCIATED", | ||
"FunctionConfig": { | ||
"Comment": "", | ||
"Runtime": "cloudfront-js-2.0", | ||
"KeyValueStoreAssociations": { | ||
"Quantity": 1, | ||
"Quantity": 1, | ||
"Items": [ | ||
{ | ||
"KeyValueStoreARN": "arn:aws:cloudfront::123456789012:key-value-store/6ed3b692-38e9-4952-b89b-bea9cexample" | ||
} | ||
] | ||
} | ||
}, | ||
"FunctionMetadata": { | ||
"FunctionARN": "arn:aws:cloudfront::123456789012:function/kvs-jwt-verify", | ||
"Stage": "DEVELOPMENT", | ||
"CreatedTime": "2021-04-09T22:02:12.937000+00:00", | ||
"LastModifiedTime": "2021-04-09T22:09:19.277000+00:00" | ||
} | ||
}, | ||
"ComputeUtilization": "19", | ||
"FunctionExecutionLogs": [ | ||
"Valid JWT token" | ||
], | ||
"FunctionErrorMessage": "", | ||
"FunctionOutput": "{\"request\":{\"headers\":{\"host\":{\"value\":\"www.example.com\"},\"accept\":{\"value\":\"text/html\"}},\"method\":\"GET\",\"querystring\":{\"test\":{\"value\":\"anotherQueryString\"}},\"uri\":\"/index.html\",\"cookies\":{}}}" | ||
} | ||
} | ||
``` |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
## Use key-value pairs | ||
|
||
**CloudFront Functions event type: viewer request** | ||
|
||
The example uses key-value pairs from an Amazon CloudFront [KeyValueStore](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/kvs-with-functions.html) in a CloudFront function. | ||
|
||
The example shows a function that uses the content of the URL in the HTTP request to look up a custom path in the key value store. CloudFront then uses that custom path to make the request. This function helps manage the multiple paths that are part of a website, such as updating the version of a blog platform on a website. For example, if the earlier blog has origin path ```/blog-v1``` and the new blog has origin path ```/blog-v2```, this function can look up the URL path of the incoming request and rewrite the URL path ```(/blog-v1)``` to the new version of the blog ```(/blog-v2)```. | ||
|
||
The example works with [JavaScript runtime 2.0](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-20.html). |
This file contains hidden or 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 @@ | ||
import cf from 'cloudfront'; | ||
|
||
// This fails if there is no key value store associated with the function | ||
const kvsHandle = cf.kvs(); | ||
|
||
// Remember to associate the KVS with your function before referencing KVS in your code. | ||
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/kvs-with-functions-associate.html | ||
async function handler(event) { | ||
const request = event.request; | ||
// Use the first segment of the pathname as key | ||
// For example http(s)://domain/<key>/something/else | ||
const pathSegments = request.uri.split('/') | ||
const key = pathSegments[1] | ||
try { | ||
// Replace the first path of the pathname with the value of the key | ||
// For example http(s)://domain/<value>/something/else | ||
pathSegments[1] = await kvsHandle.get(key); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're not defining |
||
const newUri = pathSegments.join('/'); | ||
console.log(`${request.uri} -> ${newUri}`) | ||
request.uri = newUri; | ||
} catch (err) { | ||
// No change to the pathname if the key is not found | ||
console.log(`${request.uri} | ${err}`); | ||
} | ||
return request; | ||
} |
This file contains hidden or 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,7 @@ | ||
## Normalize query string parameters | ||
|
||
**CloudFront functions event type: viewer request** | ||
|
||
You can normalize query string parameters to improve the cache hit ratio. | ||
|
||
The following example works with JavaScript runtime 1.0 and 2.0. The example shows how to improve your cache hit ratio by putting the query strings in alphabetical order before CloudFront forwards requests to your origin. |
15 changes: 15 additions & 0 deletions
15
normalize-query-string-parameters/normalize-query-string.js
This file contains hidden or 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,15 @@ | ||
function handler(event) { | ||
var qs=[]; | ||
for (var key in event.request.querystring) { | ||
if (event.request.querystring[key].multiValue) { | ||
event.request.querystring[key].multiValue.forEach((mv) => {qs.push(key + "=" + mv.value)}); | ||
} else { | ||
qs.push(key + "=" + event.request.querystring[key].value); | ||
} | ||
}; | ||
|
||
event.request.querystring = qs.sort().join('&'); | ||
|
||
|
||
return event.request; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You remove the KVS_ID from the code, but its still referenced in live 131. You will also need to remove it from line 131.