AWS Certified Developer – Associate
CloudFront signed URLs vs signed cookies for DVA-C03
According to the Amazon CloudFront Developer Guide, signed URLs fit access to individual files and clients that cannot use cookies. Signed cookies fit access to many restricted files without changing your URLs. Both are signed with a private key, and CloudFront verifies them with a public key in a trusted key group on the cache behavior.
When to use a signed URL and when to use signed cookies
Both control who can fetch content through CloudFront. Use a signed URL for a single file, such as an installer download, or when the client does not support cookies. Use signed cookies for a set of files, such as every segment of an HLS video or a whole subscribers' area, or when the URLs must stay as they are.
A signed URL takes precedence. If a request has a signed URL, CloudFront decides on the URL alone and does not look at signed cookies. CloudFront also treats any URL with a query string parameter named Expires, Policy, Signature, Key-Pair-Id or Hash-Algorithm as a signed URL, so unsigned URLs must not use those names.
The DVA-C03 exam guide added content on presigned URLs for time-limited access to its security domain, and it names Amazon S3 and Amazon CloudFront as examples.
Canned policy vs custom policy
A canned policy covers one file and sets only an end time. A custom policy can use wildcards in Resource to cover many files, can set a start time, and can limit access to an IP address or range. A custom-policy URL is longer because it carries the base64-encoded policy.
Signed cookies are sent as three Set-Cookie headers before the viewer requests private content. With a canned policy they are CloudFront-Expires, CloudFront-Signature and CloudFront-Key-Pair-Id. With a custom policy CloudFront-Policy replaces CloudFront-Expires.
CloudFront checks the expiration at the time of each HTTP request. A download in progress finishes, but a player that uses Range GETs sends many requests, and each one after the expiration fails. Query string parameters must be in the URL before it is signed. Adding one afterwards returns HTTP 403.
Trusted key groups and signers
A signer is either a trusted key group or an AWS account that holds a CloudFront key pair. AWS recommends key groups: public keys are managed through the CloudFront API under IAM permissions, so rotation can be automated and the root user is not needed. CloudFront key pairs can only be managed by the root user in the console.
You upload a public key, add it to a key group, and add the key group to a cache behavior. From that moment CloudFront requires a signed URL or signed cookies for every file the behavior serves. To keep some paths public, give them a separate cache behavior with no signer.
Key-Pair-Id in the URL or cookie is the ID of the public key, not the ID of the key group. By default a distribution can have up to four key groups and a key group up to five public keys. Key pairs must be RSA 2048 or ECDSA 256.
Keeping the S3 origin private with origin access control
Signed URLs only protect the CloudFront path. To stop viewers from going straight to the bucket, attach an origin access control (OAC) to the S3 origin and allow the CloudFront service principal, cloudfront.amazonaws.com, in the bucket policy.
AWS recommends OAC over the older origin access identity (OAI). OAC supports objects encrypted with SSE-KMS, buckets in all Regions, and PUT and DELETE requests to S3. A bucket configured as a website endpoint is a custom origin and cannot use OAC or OAI.
Where the exam catches people
- Believing signed cookies still apply when the request also carries a signed URL: actually CloudFront decides on the signed URL alone.
- Believing
Key-Pair-Idtakes the key group's ID: actually it takes the public key ID that CloudFront returned when the key was uploaded. - Believing a canned policy can cover a whole path or restrict by IP address: actually wildcards, start times and IP conditions need a custom policy.
- Believing a query string can be appended to a signed URL later: actually parameters added after signing cause HTTP 403.
- Believing OAC can protect a bucket's S3 website endpoint: actually a website endpoint is a custom origin, and OAC needs a regular S3 bucket origin.
Written from these sources
- Decide to use signed URLs or signed cookies
- Use signed URLs
- Use signed cookies
- Set signed cookies using a canned policy
- Set signed cookies using a custom policy
- Specify signers that can create signed URLs and signed cookies
- Restrict access to an Amazon S3 origin
- Comparison of DVA-C02 and DVA-C03 (exam guide)