To enforce dual-token authentication when viewers access a streaming resource, you configure separate routes. A route is a configuration that matches requests for Media CDN and directs HTTP traffic to an origin. Media CDN supports routes for either HTTP live streaming (HLS) or Dynamic Adaptive Streaming over HTTP (DASH) resources. For more information about routes in Media CDN, see Configure service routes.
For HLS streams, Media CDN supports dual-token authentication through cookies and URL query parameters (cookieless). For DASH streams, Media CDN supports only cookie-based dual-token authentication.
This page describes how to configure Media CDN routes to help protect content by using dual-token authentication.
Before you begin
Do the following:
For short-duration tokens, choose one of the following signature algorithms:
- Ed25519 signatures
- Symmetric-key hash-based message authentication codes (HMACs)
You can enable symmetric HMAC signing algorithms only for routes configured to generate new long-duration tokens. We recommend that you use Ed25519 signatures for optimal performance and security and symmetric-key HMACs only when necessary for compatibility with other CDNs.
Include the required keys in an
EdgeCacheKeysetresource.Tokens must be signed or verified with keys in an
EdgeCacheKeysetresource. The keyset must include the correct keys for the selected signature algorithm. The following table describes each of the signature algorithms and their required keys.Signature algorithm Required keys in the keyset Ed25519 Public keys HMAC-SHA1 Validation shared keys HMAC-SHA256 Validation shared keys As a best practice, make two separate keysets, one for short-duration tokens and another for long-duration tokens.
However, if you're using DASH and dynamic media presentation description (MPD) files, you must use the same keyset for both long-duration and short-duration tokens.
For long-duration tokens, choose one of the following token formats:
- Cookies
- URL query parameters
Set up short-duration tokens
Depending on the signature algorithm that you want to use, set up short-duration tokens by using one of the following options.
Ed25519 signature
Generate a private key:
openssl genpkey -algorithm ed25519 -outform PEM -out SSL_KEY_NAME.private.keyReplace
SSL_KEY_NAMEwith a key name.Generate a public key from the private key:
openssl pkey -outform DER -pubout -in SSL_KEY_NAME.private.key |\ tail -c +13 |\ python3 -c "import base64, sys; print(('%s' % base64.urlsafe_b64encode(sys.stdin.buffer.read()))[2:-1])"Create a new keyset with a single public key:
Console
In the Google Cloud console, go to the Media CDN page.
Click the Keysets tab.
Click Create keyset.
For Name, enter a unique keyset name.
Optional: For Description, enter a description for your keyset.
Optional: Click Add label, and then enter one or more key-value pairs for your keyset.
Click Add public key, and then do the following:
- For ID, enter an alphanumeric ID.
- Select Enter the value, and then specify the base64-encoded value of your Ed25519 public key.
Click Create keyset.
gcloud
Use the
gcloud edge-cache keysets createcommand.gcloud edge-cache keysets create SHORT_KEYSET_NAME \ --public-key='id=SSL_PUBLIC_KEY_NAME,value=SSL_PUBLIC_KEY_VALUE'
Replace the following:
SHORT_KEYSET_NAME: a unique keyset name—for example,prod-vod-keysetSSL_PUBLIC_KEY_NAME: the name of your SSL public keySSL_PUBLIC_KEY_VALUE: the value of your SSL public key
To review the keys associated with the keyset, use the
gcloud edge-cache keysets describecommand.gcloud edge-cache keysets describe prod-vod-keyset
The output is similar to the following:
name: prod-vod-keyset description: "Keyset for prod.example.com" publicKeys: - id: "key-20200918" value: "DThVLjhAKm3VYOvLBAwFZ5XbjVyF98Ias8NZU0WEM9w" - id: "key-20200808" value: "Lw7LDSaDUrbDdqpPA6JEmMF5BA5GPtd7sAjvsnh7uDA="
Terraform
Symmetric-key HMAC
If you haven't used Secret Manager before, configure Secret Manager.
Add a secret version in binary format.
Grant the Secret Manager Access role (
roles/secretmanager.secretAccessor) to the Media CDN service account:Console
In the Google Cloud console, go to the Secret Manager page.
Select the checkbox next to the name of the secret.
Click Show Info Panel.
In the info panel, click Add principal.
For New principals, enter the Media CDN service account in this format:
service-PROJECT_NUMBER@gcp-sa-mediaedgefill.iam.gserviceaccount.com
Replace
PROJECT_NUMBERwith your project number.For Select a role, select Secret Manager, and then select Secret Manager Secret Accessor.
gcloud
Use the
gcloud secrets add-iam-policy-bindingcommand.gcloud secrets add-iam-policy-binding projects/PROJECT_NUMBER/secrets/SECRET_ID \ --member="serviceAccount:service-PROJECT_NUMBER@gcp-sa-mediaedgefill.iam.gserviceaccount.com" \ --role="roles/secretmanager.secretAccessor"
Replace the following:
PROJECT_NUMBER: your project numberSECRET_ID: the ID of the secret
Access your secret versions and copy the secret paths, including version numbers.
Create a new keyset with the shared secrets in a validation shared key:
Console
In the Google Cloud console, go to the Media CDN page.
Click the Keysets tab.
Click Create keyset.
For Name, enter a unique keyset name.
Optional: For Description, enter a description for your keyset.
Optional: Click Add label, and then enter one or more key-value pairs for your keyset.
To specify a validation shared key, click Add validation shared key, and then do the following:
For Secret, select a secret from the list, enter a secret manually by specifying its resource ID, or create a new secret and then select it.
For Secret version, select a version from the list or create a new secret version and then select it.
Click Create keyset.
gcloud
Use the
gcloud edge-cache keysets createcommand.gcloud edge-cache keysets create SHORT_KEYSET_NAME \ --validation-shared-key='secret_version=projects/PROJECT_NUMBER/secrets/SECRET_ID/versions/KEY_VERSION'
Replace the following:
SHORT_KEYSET_NAME: a unique name for the keyset—for example,prod-vod-keysetPROJECT_NUMBER: your project IDSECRET_ID: the resource ID of the secretKEY_VERSION: the secret version that you want to use
Set up long-duration tokens
Google-owned and managed keys are scoped by keyset. That means that two different keysets have different Google-owned and managed keys. Google-owned and managed keys are rotated regularly.
Set up long-duration tokens by using one of the following options:
Console
In the Keys section, select Use Google-owned and managed key for dual-token authentication.
gcloud and YAML
Use either of the following options:
Create a Google-managed signing key:
gcloud edge-cache keysets create LONG_KEYSET_NAME \ --public-key='id=google-managed-key,managed=true'Replace
LONG_KEYSET_NAMEwith a key name—for example,prod-vod-keyset-long.Modify an existing keyset:
Export your keyset to a YAML file. Use the
gcloud edge-cache keysets exportcommand.gcloud edge-cache keysets export LONG_KEYSET_NAME \ --destination=prod-vod-keyset-long.yamlIn a text editor or in your configuration management tooling, edit your keyset configuration so that it looks similar to the following:
name: projects/my-project/locations/global/edgeCacheKeysets/LONG_KEYSET_NAME publicKeys: - id: some-key value: MC4CAQAwBQYDK2VwBCIEINV2iYugIWBuvGBJUQ_Ab69E4v4zcVqvgYHw-iZxGzcd - id: google-managed-key managed: trueImport the edited keyset. Use the
gcloud edge-cache keysets importcommand:gcloud edge-cache keysets import LONG_KEYSET_NAME \ --source=prod-vod-keyset-long.yaml
You can include additional public keys in the long-duration token keyset. The keyset may have at most three public keys. In practice, this means that the keyset can have two user-managed keys and one Google-owned and managed key.
Media CDN always uses the Google-owned and managed key to generate tokens. The user-managed keys can be used only for verification.
Including additional public keys is useful to let your player application access media playlists and media segments by using signed requests that are generated by your own private keys.
Configure token exchange
This section shows how to configure token exchange by creating multiple routes. A route lets you optimize behavior based on the type of content, client attributes, and your freshness requirements. In the following examples, we use routes to configure token exchange for each part of a media request.
Configure the primary manifest route to require short-duration tokens
Configure Media CDN to require short-duration tokens on the primary manifest route.
Console
Enable short-duration token authentication on the primary manifest route:
In the Google Cloud console, go to the Media CDN page.
To open the Details page of a service, click the service name.
To switch to the edit mode, click the Edit button.
To navigate to the Routing section, click Next.
Expand the host rule to which you'd like to add the primary manifest route rule.
Click Add route rule.
Alternatively, to edit a route rule, click Edit on the respective row.
In the Edit route rule pane, for Priority, set a value—for example,
1.For Description, provide a brief description that can help identify the rule in the list of rules.
In the Match section, click Add a match condition. Then, do the following:
- For Match type, select Path template match.
- For Path match, specify the names or path templates for the HLS primary playlist (M3U8) or DASH manifest (MPD) file. For more information, see Path matching.
Click Advanced configurations.
In the Route action section, click Add an item.
For Type, select CDN policy.
In the Signed request section, for Signed request mode, select Require tokens.
In the Signed request keyset section, do the following:
To specify the keyset for short-duration tokens, click Select a keyset and select your short-duration token keyset.
Alternatively, to create a new keyset that has the keys that you need, click Create new keyset. Then, select it.
For Signature algorithm, select Ed25519 using public keys.
For Token query parameter, retain the default value,
edge-cache-token.Alternatively, if you plan to use HLS manifest URL query parameters rather than cookies for authentication, specify the parameter in which to store the request tokens.
For Max time to live, specify, in seconds, the maximum lifespan of incoming request tokens.
To save the changes in the Route action section, click Done.
To save the changes in the route rule, click Save.
gcloud and YAML
Export your Media CDN configuration into a YAML file. Use the
gcloud edge-cache services exportcommand.gcloud edge-cache services export SERVICE_NAME \ --destination=FILENAME.yamlReplace the following:
SERVICE_NAME: the name of your serviceFILENAME: the name of your YAML file
To enable short-duration token authentication in the primary manifest route rule, in the route's
cdnPolicysection of the YAML file, specify asignedTokensOptionsconfiguration.pathMatchers: - name: "ROUTE_NAME" routeRules: - priority: 1 description: