Skip to content

merge upstream #1

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 50 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
332876c
Create README.md
lkdavies Aug 9, 2024
04d3d50
Update verify-jwt.js
lkdavies Aug 9, 2024
b137bd6
Update ABUriMappingFunction.js
lkdavies Aug 9, 2024
6577620
Create expired-jwt.json
lkdavies Aug 9, 2024
cb68a21
Rename invalid-jwt.json to invalid-jwt.json
lkdavies Aug 9, 2024
5fecae3
Rename invalid-nbe-jwt.json to invalid-nbe-jwt.json
lkdavies Aug 9, 2024
ba0fc7c
Rename missing-jwt.json to missing-jwt.json
lkdavies Aug 9, 2024
ec26de2
Rename valid-jwt.json to valid-jwt.json
lkdavies Aug 9, 2024
aebcaa8
Delete verify-jwt directory
lkdavies Aug 9, 2024
c9f864f
Create normalize-query-string.js
lkdavies Aug 9, 2024
9e85f34
Create README.md
lkdavies Aug 9, 2024
ce8899c
Create kvs-key-value-pairs.js
lkdavies Aug 9, 2024
920ecfb
Create README.md
lkdavies Aug 9, 2024
ce1542f
Create README.md
lkdavies Aug 9, 2024
46c76b4
Update README.md
lkdavies Aug 9, 2024
b89dd05
Update README.md
lkdavies Aug 9, 2024
23125bb
Update README.md
lkdavies Aug 13, 2024
4a3c686
Update README.md
lkdavies Aug 13, 2024
85732aa
Update verify-jwt.js
lkdavies Aug 13, 2024
d1561f6
Update README.md
lkdavies Aug 13, 2024
0289868
Update kvs-key-value-pairs.js
lkdavies Aug 13, 2024
faa87f9
Update kvs-key-value-pairs.js
lkdavies Aug 13, 2024
bfb6668
Update kvs-key-value-pairs.js
lkdavies Aug 19, 2024
4adcdc3
Update verify-jwt.js
lkdavies Aug 19, 2024
257053b
Update verify-jwt.js
lkdavies Aug 20, 2024
b71db44
Update verify-jwt.js
lkdavies Aug 20, 2024
c12a682
Update kvs-key-value-pairs.js
lkdavies Aug 20, 2024
b7511cd
Merge pull request #45 from aws-samples/KVS-updates
lkdavies Aug 20, 2024
b3ff756
Update index.js
lkdavies Sep 17, 2024
469a73e
Update example metadata to treat JavaScript Runtime 1.0 and 2.0 for C…
Laren-AWS Oct 10, 2024
33ceb55
Merge pull request #49 from Laren-AWS/poc/tributary
lkdavies Oct 28, 2024
fbaba7d
Update index.js
lkdavies Oct 29, 2024
d45fd1f
Update index.js
lkdavies Oct 29, 2024
eeccf8b
Update index.js
lkdavies Oct 29, 2024
9df16aa
Update index.js
lkdavies Oct 29, 2024
4577e08
Update index.js
lkdavies Oct 29, 2024
5b65b2e
Update index.js
lkdavies Oct 29, 2024
e7e660e
Update index.js
lkdavies Oct 29, 2024
5e567e4
Update index.js
lkdavies Oct 29, 2024
2e59510
Update index.js
lkdavies Oct 29, 2024
795e91b
Update index.js
lkdavies Oct 29, 2024
ab43a63
Update index.js
lkdavies Oct 29, 2024
8c3554b
Update index.js
lkdavies Oct 29, 2024
718db13
Update index.js
lkdavies Oct 29, 2024
bc4d5ee
Update index.js
lkdavies Oct 29, 2024
640e3d5
Merge pull request #50 from aws-samples/2.0-runtime
lkdavies Oct 29, 2024
c662e10
Adding origin selection using CloudFront Functions example
Nov 21, 2024
0cd43d3
Merge pull request #51 from aws-samples/origin-selection
dbrown-git Nov 21, 2024
d50825e
clean up duplicate folder
Nov 21, 2024
b0493be
Merge pull request #52 from aws-samples/cleanup
dbrown-git Nov 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 204 additions & 0 deletions .doc_gen/metadata/cloudfront-functions_metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
cloudfront_functions_add_cache_control_header:
title: Add a cache control header to a &CF; Functions viewer response event
title_abbrev: Add a cache control header
synopsis: add a cache control header to a &CF; Functions viewer response event.

category: CloudFront Functions examples
languages:
JavaScript:
versions:
- sdk_version: 102
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/add-cache-control-header
excerpts:
- description:
snippet_files:
- add-cache-control-header/index.js
services:
cloudfront:
cloudfront_functions_add_cors_header:
title: Add a CORS header to a &CF; Functions viewer response event
title_abbrev: Add a CORS header
synopsis: add a CORS header to a &CF; Functions viewer response event.

category: CloudFront Functions examples
languages:
JavaScript:
versions:
- sdk_version: 102
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/add-cors-header
excerpts:
- description:
snippet_files:
- add-cors-header/index.js
services:
cloudfront:
cloudfront_functions_add_origin_header:
title: Add an origin header to a &CF; Functions viewer request event
title_abbrev: Add an origin header
synopsis: add an origin header to a &CF; Functions viewer request event.

category: CloudFront Functions examples
languages:
JavaScript:
versions:
- sdk_version: 102
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/add-origin-header
excerpts:
- description:
snippet_files:
- add-origin-header/index.js
services:
cloudfront:
cloudfront_functions_add_security_headers:
title: Add HTTP security headers to a &CF; Functions viewer response event
title_abbrev: Add HTTP security headers
synopsis: add HTTP security headers to a &CF; Functions viewer response event.

category: CloudFront Functions examples
languages:
JavaScript:
versions:
- sdk_version: 102
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/add-security-headers
excerpts:
- description:
snippet_files:
- add-security-headers/index.js
services:
cloudfront:
cloudfront_functions_add_true_client_ip_header:
title: Add a true client IP header to a &CF; Functions viewer request event
title_abbrev: Add a true client IP header
synopsis: add a true client IP header to a &CF; Functions viewer request event.

category: CloudFront Functions examples
languages:
JavaScript:
versions:
- sdk_version: 102
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/add-true-client-ip-header
excerpts:
- description:
snippet_files:
- add-true-client-ip-header/index.js
services:
cloudfront:
cloudfront_functions_kvs_conditional_read:
title: Rewrite a request URI based on KeyValueStore configuration for a &CF; Functions viewer request event
title_abbrev: Rewrite a request URI
synopsis: rewrite a request URI based on KeyValueStore configuration for a &CF; Functions viewer request event.

category: CloudFront Functions examples
languages:
JavaScript:
versions:
- sdk_version: 102
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/kvs-conditional-read
excerpts:
- description:
snippet_files:
- kvs-conditional-read/ABUriMappingFunction.js
services:
cloudfront:
cloudfront_functions_kvs_jwt_verify:
title: Validate a simple token in a &CF; Functions viewer request
title_abbrev: Validate a simple token
synopsis: validate a simple token in a &CF; Functions viewer request.

category: CloudFront Functions examples
languages:
JavaScript:
versions:
- sdk_version: 102
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/kvs-jwt-verify
excerpts:
- description:
snippet_files:
- kvs-jwt-verify/verify-jwt.js
services:
cloudfront:
cloudfront_functions_kvs_key_value_pairs:
title: Use key-value pairs in a &CF; Functions viewer request
title_abbrev: Use key-value pairs
synopsis: use key-value pairs in a &CF; Functions viewer request.

category: CloudFront Functions examples
languages:
JavaScript:
versions:
- sdk_version: 102
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/kvs-key-value-pairs
excerpts:
- description:
snippet_files:
- kvs-key-value-pairs/kvs-key-value-pairs.js
services:
cloudfront:
cloudfront_functions_normalize_query_string_parameters:
title: Normalize query string parameters in a &CF; Functions viewer request
title_abbrev: Normalize query string parameters
synopsis: normalize query string parameters in a &CF; Functions viewer request.

category: CloudFront Functions examples
languages:
JavaScript:
versions:
- sdk_version: 102
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/normalize-query-string-parameters
excerpts:
- description:
snippet_files:
- normalize-query-string-parameters/normalize-query-string.js
services:
cloudfront:
cloudfront_functions_redirect_based_on_country:
title: Redirect to a new URL in a &CF; Functions viewer request event
title_abbrev: Redirect to a new URL
synopsis: redirect to a new URL in a &CF; Functions viewer request event.

category: CloudFront Functions examples
languages:
JavaScript:
versions:
- sdk_version: 102
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/redirect-based-on-country
excerpts:
- description:
snippet_files:
- redirect-based-on-country/index.js
services:
cloudfront:
cloudfront_functions_url_rewrite_single_page_apps:
title: Add index.html to request URLs without a file name in a &CF; Functions viewer request event
title_abbrev: Add index.html to request URLs
synopsis: add index.html to request URLs without a file name in a &CF; Functions viewer request event.

category: CloudFront Functions examples
languages:
JavaScript:
versions:
- sdk_version: 102
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/url-rewrite-single-page-apps
excerpts:
- description:
snippet_files:
- url-rewrite-single-page-apps/index.js
services:
cloudfront:
cloudfront_functions_select_origin_based_on_country:
title: Route requests to an origin closer to the viewer in a &CF; Functions viewer request event
title_abbrev: Select origin closer to the viewer
synopsis: route requests to an origin closer to the viewer in a &CF; Functions viewer request event.

category: CloudFront Functions examples
languages:
JavaScript:
versions:
- sdk_version: 102
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/select-origin-based-on-country
excerpts:
- description:
snippet_files:
- select-origin-based-on-country/index.js
services:
cloudfront:
8 changes: 5 additions & 3 deletions add-cache-control-header/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
function handler(event) {
async function handler(event) {
var response = event.response;
var headers = response.headers;

// Set the cache-control header
headers['cache-control'] = {value: 'public, max-age=63072000'};
if (response.statusCode >= 200 && response.statusCode < 400) {
// Set the cache-control header
headers['cache-control'] = {value: 'public, max-age=63072000'};
}

// Return response to viewers
return response;
Expand Down
2 changes: 1 addition & 1 deletion add-cors-header/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function handler(event) {
async function handler(event) {
var request = event.request;
var response = event.response;

Expand Down
4 changes: 2 additions & 2 deletions add-origin-header/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function handler(event) {
async function handler(event) {
var request = event.request;
var headers = request.headers;
var host = request.headers.host.value;
Expand All @@ -8,4 +8,4 @@ function handler(event) {
headers.origin = {value:`https://${host}`};

return request;
}
}
2 changes: 1 addition & 1 deletion add-security-headers/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function handler(event) {
async function handler(event) {
var response = event.response;
var headers = response.headers;

Expand Down
4 changes: 2 additions & 2 deletions add-true-client-ip-header/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function handler(event) {
async function handler(event) {
var request = event.request;
var clientIP = event.viewer.ip;

//Add the true-client-ip header to the incoming request
request.headers['true-client-ip'] = {value: clientIP};

return request;
}
}
4 changes: 2 additions & 2 deletions kvs-conditional-read/ABUriMappingFunction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cf from 'cloudfront';

// Replace KVS_ID with actual KVS ID
// (Optional) Replace KVS_ID with actual KVS ID
const kvsId = "KVS_ID";
// enable stickiness by setting a cookie from origin or using another edge function
const stickinessCookieName = "appversion";
Expand Down Expand Up @@ -77,4 +77,4 @@ function log(message) {
if (loggingEnabled) {
console.log(message);
}
}
}
5 changes: 5 additions & 0 deletions kvs-conditional-read/README.md
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).
60 changes: 60 additions & 0 deletions kvs-jwt-verify/README.md
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\":{}}}"
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
"accept": { "value": "text/html", "multivalue": [ { "value": "text/html" }, { "value": "application/xhtml+xml" } ] }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
"accept": { "value": "text/html", "multivalue": [ { "value": "text/html" }, { "value": "application/xhtml+xml" } ] }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
"accept": { "value": "text/html", "multivalue": [ { "value": "text/html" }, { "value": "application/xhtml+xml" } ] }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"accept": { "value": "text/html", "multivalue": [ { "value": "text/html" }, { "value": "application/xhtml+xml" } ] }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
"accept": { "value": "text/html", "multivalue": [ { "value": "text/html" }, { "value": "application/xhtml+xml" } ] }
}
}
}
}
Loading