Certopolis

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-Id takes 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

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A developer must configure a CloudFront distribution to require signed URLs. Company policy forbids any use of the AWS account root user, and the team wants to automate key creation and rotation through the CloudFront API under IAM permissions. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Security building.

    Not that one: A is right. In the game your Security building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    The developer uploads the public key, adds it to a key group, and adds that key group to the cache behavior.

    Written from docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-trusted-signers.html

  2. A developer protects the /members/* path of a CloudFront distribution with signed cookies. One set of cookies must cover every file under the path and must work only from the IP address that the member signed in from. The application sets the cookies in its sign-in response. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Security building.

    Not that one: B is right. In the game your Security building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    A custom-policy signed cookie is sent as three Set-Cookie headers, CloudFront-Policy, CloudFront-Signature and CloudFront-Key-Pair-Id, and they must reach the viewer before it requests the private files, which the sign-in response does.

    Written from docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-cookies.html

  3. A streaming site sets CloudFront signed cookies that cover /films/* for 4 hours when a subscriber signs in. The player fetches each film in Range GETs so that viewers can seek, and a film can run 3 hours. For each film, the player receives a link from the application, and a link copied out of the player must stop giving access within 5 minutes. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Security building.

    Not that one: C is right. In the game your Security building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    And when a request carries a signed URL, CloudFront decides on that URL alone and ignores signed cookies. A 5-minute signed URL therefore breaks a 3-hour film either way. The unsigned URL, authorized by the 4-hour cookies on every Range GET, works for the whole film, and a copied link grants nothing without the cookies.

    Written from docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-urls.html

Ask for something