[ aws . cloudfront ]

sign-cookies

Description

Generate CloudFront signed cookies for a given resource. Supports RSA and ECDSA private keys. The key type is auto-detected from the PEM header. The output maps each cookie name to its value; add the cookie attributes (Domain, Path, Secure, HttpOnly, etc.) that your application needs when setting them. A custom policy is used when –date-greater-than or –ip-address is provided, or when the resource contains a * wildcard; otherwise a canned policy is used. The set of cookies depends on the policy type and hash algorithm, so use –query to read individual cookies in scripts.

Synopsis

  sign-cookies
--resource <value>
--key-pair-id <value>
--private-key <value>
--date-less-than <value>
[--date-greater-than <value>]
[--ip-address <value>]
[--hash-algorithm <value>]
[--debug]
[--endpoint-url <value>]
[--no-verify-ssl]
[--no-paginate]
[--output <value>]
[--query <value>]
[--profile <value>]
[--region <value>]
[--version <value>]
[--color <value>]
[--no-sign-request]
[--ca-bundle <value>]
[--cli-read-timeout <value>]
[--cli-connect-timeout <value>]
[--cli-binary-format <value>]
[--no-cli-pager]
[--cli-auto-prompt]
[--no-cli-auto-prompt]
[--cli-error-format <value>]

Options

--resource (string) [required] The URL of the protected content. It may contain * and ? wildcards (for example https://d111111abcdef8.cloudfront.net/videos/*) to grant access to every matching URL. Wildcards require a custom policy, which is only selected when the resource contains * or when –date-greater-than or –ip-address is given.

--key-pair-id (string) [required] The active CloudFront key pair Id for the key pair that you’re using to generate the signature.

--private-key (blob) [required] file://path/to/your/private-key.pem or fileb://path/to/your/private-key.pem. Both RSA and ECDSA (P-256) keys are supported in PEM format (PKCS#1, PKCS#8, or SEC1); the key type is detected automatically.

--date-less-than (string) [required] The expiration date and time. Supported formats include: YYYY-MM-DD (which means 0AM UTC of that day), YYYY-MM-DDThh:mm:ss (with default timezone as UTC), YYYY-MM-DDThh:mm:ss+hh:mm or YYYY-MM-DDThh:mm:ss-hh:mm (with offset), or EpochTime (which always means UTC). Do NOT use YYYYMMDD, because it will be treated as EpochTime.

--date-greater-than (string) An optional start date and time. It must be before –date-less-than. Supported formats include: YYYY-MM-DD (which means 0AM UTC of that day), YYYY-MM-DDThh:mm:ss (with default timezone as UTC), YYYY-MM-DDThh:mm:ss+hh:mm or YYYY-MM-DDThh:mm:ss-hh:mm (with offset), or EpochTime (which always means UTC). Do NOT use YYYYMMDD, because it will be treated as EpochTime.

--ip-address (string) An optional IPv4 address or IPv4 address range to allow client making the GET request from. Format: x.x.x.x/x or x.x.x.x

--hash-algorithm (string) The hash algorithm used to sign, either SHA1 or SHA256. SHA256 is recommended for its stronger security posture. RSA keys default to SHA1. ECDSA keys always use SHA256 and cannot be used with SHA1.

Global Options

--debug (boolean)

Turn on debug logging.

--endpoint-url (string)

Override command’s default URL with the given URL.

--no-verify-ssl (boolean)

By default, the AWS CLI uses SSL when communicating with AWS services. For each SSL connection, the AWS CLI will verify SSL certificates. This option overrides the default behavior of verifying SSL certificates.

--no-paginate (boolean)

Disable automatic pagination. If automatic pagination is disabled, the AWS CLI will only make one call, for the first page of results.

--output (string)

The formatting style for command output.

  • json
  • text
  • table
  • yaml
  • yaml-stream
  • off

--query (string)

A JMESPath query to use in filtering the response data.

--profile (string)

Use a specific profile from your credential file.

--region (string)

The region to use. Overrides config/env settings.

--version (string)

Display the version of this tool.

--color (string)

Turn on/off color output.

  • on
  • off
  • auto

--no-sign-request (boolean)

Do not sign requests. Credentials will not be loaded if this argument is provided.

--ca-bundle (string)

The CA certificate bundle to use when verifying SSL certificates. Overrides config/env settings.

--cli-read-timeout (int)

The maximum socket read time in seconds. If the value is set to 0, the socket read will be blocking and not timeout. The default value is 60 seconds.

--cli-connect-timeout (int)

The maximum socket connect time in seconds. If the value is set to 0, the socket connect will be blocking and not timeout. The default value is 60 seconds.

--cli-binary-format (string)

The formatting style to be used for binary blobs. The default format is base64. The base64 format expects binary blobs to be provided as a base64 encoded string. The raw-in-base64-out format preserves compatibility with AWS CLI V1 behavior and binary values must be passed literally. When providing contents from a file that map to a binary blob fileb:// will always be treated as binary and use the file contents directly regardless of the cli-binary-format setting. When using file:// the file contents will need to properly formatted for the configured cli-binary-format.

  • base64
  • raw-in-base64-out

--no-cli-pager (boolean)

Disable cli pager for output.

--cli-auto-prompt (boolean)

Automatically prompt for CLI input parameters.

--no-cli-auto-prompt (boolean)

Disable automatically prompt for CLI input parameters.

--cli-error-format (string)

The formatting style for error output. By default, errors are displayed in enhanced format.

  • legacy
  • json
  • yaml
  • text
  • table
  • enhanced

Examples

Note

To use the following examples, you must have the AWS CLI installed and configured. See the Getting started guide in the AWS CLI User Guide for more information.

Unless otherwise stated, all examples have unix-like quotation rules. These examples will need to be adapted to your terminal’s quoting rules. See Using quotation marks with strings in the AWS CLI User Guide .

Example 1: To generate CloudFront signed cookies

The following sign-cookies example generates signed cookies that grant access to every object in a CloudFront distribution. To generate signed cookies, you need the key pair ID (called the Access Key ID in the AWS Management Console) and the private key of the trusted signer’s CloudFront key pair. Because the resource contains a wildcard, a custom policy is used.

aws cloudfront sign-cookies \
    --resource "https://d111111abcdef8.cloudfront.net/*" \
    --key-pair-id APKAEIBAERJR2EXAMPLE \
    --private-key file://cf-signer-priv-key.pem \
    --date-less-than 2020-01-01 \
    --hash-algorithm SHA256

Contents of cf-signer-priv-key.pem:

-----BEGIN RSA PRIVATE KEY-----
EXAMPLEPRIVATEKEYMIIEowIBAAKCAQEAxPMbCA2Ks0lnd7IR+3pwwd3H/7jPGwj8
bLUmore7bX+oeGpZ6QmLAe/1UOWcmZX2u70dYcSIzB1ofZtcn4cJEXAMPLEKEY...
-----END RSA PRIVATE KEY-----

Output:

{
    "CloudFront-Policy": "eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiaHR0cHM6Ly9kMTExMTExYWJjZGVmOC5jbG91ZGZyb250Lm5ldC8qIiwiQ29uZGl0aW9uIjp7IkRhdGVMZXNzVGhhbiI6eyJBV1M6RXBvY2hUaW1lIjoxNTc3ODM2ODAwfX19XX0_",
    "CloudFront-Signature": "Uu8FSzfyZ1pUJfnPTfpZpXoR3cHtu6Vk41gOodDpfCGmY4j~TBvOMBUsXzrcBjAqRuVV9b0Aa1HBpHeY-TR3G3Jbb2Yn2cxNSlAfC4tIYwWOkwypCZl4csuHdFC8XaN0yA0G8GRHDsxNPHKY6lq3ypbnJcXDSzzCixTAgJpOO7M_",
    "CloudFront-Key-Pair-Id": "APKAEIBAERJR2EXAMPLE",
    "CloudFront-Hash-Algorithm": "SHA256"
}

Set each cookie in the response to the viewer, adding the cookie attributes (such as Domain, Path, Secure and HttpOnly) that your application needs.

For more information, see Use signed cookies in the Amazon CloudFront Developer Guide.

Example 2: To extract a single cookie value

The following sign-cookies example generates signed cookies and returns only the value of the CloudFront-Signature cookie. The set of cookies depends on the policy type and hash algorithm, so the columns of --output text vary between invocations. Use --query to select a cookie by name.

aws cloudfront sign-cookies \
    --resource "https://d111111abcdef8.cloudfront.net/*" \
    --key-pair-id APKAEIBAERJR2EXAMPLE \
    --private-key file://cf-signer-priv-key.pem \
    --date-less-than 2020-01-01 \
    --hash-algorithm SHA256 \
    --query '"CloudFront-Signature"' \
    --output text

Output:

Uu8FSzfyZ1pUJfnPTfpZpXoR3cHtu6Vk41gOodDpfCGmY4j~TBvOMBUsXzrcBjAqRuVV9b0Aa1HBpHeY-TR3G3Jbb2Yn2cxNSlAfC4tIYwWOkwypCZl4csuHdFC8XaN0yA0G8GRHDsxNPHKY6lq3ypbnJcXDSzzCixTAgJpOO7M_

For more information, see Use signed cookies in the Amazon CloudFront Developer Guide.