# AWS CloudFront Functions

> Deploy Centinel Analytica on a CloudFront distribution with two CloudFront Functions and no Lambda.

Source: https://docs.centinelanalytica.com/platforms/cdn-edge/aws-cloudfront-functions

## Overview

This guide installs Centinel Analytica with CloudFront Functions. Two functions run at every edge location:

* The viewer-request function sends each request to the Centinel validator. It forwards the request, or it serves the block page or the interstitial page.
* The viewer-response function sets the session cookie on allowed responses.

You do not deploy a Lambda function. You do not use the us-east-1 region.

For the Lambda\@Edge integration, see [AWS CloudFront Lambda@Edge](https://docs.centinelanalytica.com/platforms/cdn-edge/aws-cloudfront.md).

## Prerequisites

* Centinel secret key
* CloudFront distribution you can edit, on a paid pricing plan
* AWS Console access with CloudFront permissions

The functions send a request to the validator. CloudFront does not allow these requests on the free pricing plan, on multi-tenant distributions, or on continuous-deployment distributions. On these distributions, the function allows every request without validation.

## Install

1. **Add the validator origin**

   1. Open **CloudFront → Distributions** and select the distribution to protect.
   2. In the **Origins** tab, choose **Create origin**.
   3. Set **Origin domain** to `validator.centinelanalytica.com`.
   4. Set **Name** to `centinel-validator`.
   5. Set **Protocol** to **HTTPS only** and **Minimum origin SSL protocol** to **TLSv1.2**.
   6. Set **Origin HTTP version** to **HTTP/2**. HTTP/2 keeps the connection to the validator open. This makes validator requests faster.
   7. Under **Add custom header**, add the header `x-api-key` with your secret key as the value.
   8. Save the origin.

   CloudFront adds the custom header to each request that the function sends. The secret key is not in the function code.

2. **Create the viewer-request function**

   1. Download [`centinel-cloudfront-functions-viewer-request.js`](https://docs.centinelanalytica.com/downloads/centinel-cloudfront-functions-viewer-request.js).
   2. Go to **CloudFront → Functions → Create function**.
   3. Name the function (for example `centinel-viewer-request`) and select the runtime **cloudfront-js-2.0**.
   4. Paste the file content into the code editor and save it.
   5. In the **Publish** tab, publish the function.

3. **Create the viewer-response function**

   1. Download [`centinel-cloudfront-functions-viewer-response.js`](https://docs.centinelanalytica.com/downloads/centinel-cloudfront-functions-viewer-response.js).
   2. Create a second function (for example `centinel-viewer-response`) with the runtime **cloudfront-js-2.0**.
   3. Paste the file content, save it, and publish it.

4. **Associate the functions**

   1. Open the distribution and edit the cache behavior to protect.
   2. In **Function associations**, set **Viewer request** to **CloudFront Functions** and select the viewer-request function.
   3. Set **Viewer response** to **CloudFront Functions** and select the viewer-response function.
   4. Save the behavior.

   Without the viewer-response function, allowed visitors do not get a session cookie. Centinel then validates each of their requests again.

5. **Send the TLS fingerprints**

   1. Edit the origin request policy of the cache behavior.
   2. Add the headers `CloudFront-Viewer-JA3-Fingerprint` and `CloudFront-Viewer-JA4-Fingerprint`.
   3. Save the policy.

   The viewer-request function sends each request header that it receives to the validator.

## Configure

Edit these constants at the top of the viewer-request function before you publish it.

| Setting                      | Default              | Purpose                                          |
| ---------------------------- | -------------------- | ------------------------------------------------ |
| `CENTINEL_ORIGIN_ID`         | `centinel-validator` | Name of the validator origin.                    |
| `CENTINEL_PROTECTED_PATHS`   | empty (all paths)    | Paths to protect. `*` matches one path segment.  |
| `CENTINEL_UNPROTECTED_PATHS` | 34 static extensions | Paths to skip. `*.ext` matches at any depth.     |
| `CENTINEL_LOG_ENABLED`       | `true`               | Logs the reason when a request is not validated. |

CloudFront Functions have limits that change how the integration behaves:

* **Fails open.** A timeout, an error response, or a reply that CloudFront cannot read allows the request. The validator request times out after 1 second. You cannot change this limit.
* **10 KB request body.** When a request is larger, the function sends only the `_centinel` cookie in the Cookie header. Then it removes the largest headers until the request fits.
* **10 KB validator reply.** A block page or interstitial page larger than approximately 7 KB of HTML does not fit. The request then goes to the origin. Keep custom pages under 7 KB of HTML.
* **10 KB function size.** Keep the function under 10 KB.
* **No cookie on error responses.** CloudFront does not run the viewer-response function when the origin returns status 400 or higher.

The viewer-request function adds the `x-centinel-set-cookie` request header to allowed requests. This header also goes to your origin. It does not change how your origin responds.

## Verify

* Wait until the distribution status is **Deployed**.
* Open the distribution domain and make sure that pages load.
* In the browser developer tools, make sure that the response sets the `_centinel` cookie.
* To troubleshoot, find the log lines that start with `[Centinel]` in CloudWatch Logs, in the log group `/aws/cloudfront/function/<function-name>` in us-east-1.

The console **Test** tab cannot send network requests, so test the function on a deployed distribution.

## Changelog

* **v1.0.0** — Initial release
