Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cdn signed url sample #4011

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/cdn-signed-urls.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: cdn-signed-urls
on:
push:
branches:
- main
paths:
- 'cdn/signed-urls/**'
- '.github/workflows/cdn-signed-urls.yaml'
pull_request:
types:
- opened
- reopened
- synchronize
- labeled
paths:
- 'cdn/signed-urls/**'
- '.github/workflows/cdn-signed-urls.yaml'
schedule:
- cron: '0 0 * * 0'
jobs:
test:
# Ref: https://github.com/google-github-actions/auth#usage
permissions:
contents: 'read'
id-token: 'write'
if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run'
uses: ./.github/workflows/test.yaml
with:
name: 'cdn-signed-urls'
path: 'cdn/signed-urls'
flakybot:
# Ref: https://github.com/google-github-actions/auth#usage
permissions:
contents: 'read'
id-token: 'write'
if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail
uses: ./.github/workflows/flakybot.yaml
needs: [test]
1 change: 1 addition & 0 deletions .github/workflows/utils/workflows.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"asset/snippets",
"auth",
"batch",
"cdn/signed-urls",
"cloudbuild",
"cloud-language",
"cloud-tasks/snippets",
Expand Down
18 changes: 18 additions & 0 deletions cdn/signed-urls/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "signed-urls-samples",
"private": true,
"license": "Apache-2.0",
"author": "Google LLC",
"engines": {
"node": ">=16.0.0"
},
"files": [
"*.js"
],
"scripts": {
"test": "mocha -p -j 2 **/*.test.js"
},
"devDependencies": {
"mocha": "^10.0.0"
}
}
48 changes: 48 additions & 0 deletions cdn/signed-urls/signurl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START nodejs_cdn_signed_urls]
const crypto = require('crypto');

/**
* Sign url to access Google Cloud CDN resource secured by key.
* @param url the Cloud CDN endpoint to sign
* @param keyName name of the signing key configured in the backend service/bucket
* @param keyValue value of the signing key
* @param expirationDate the date that the signed URL expires
* @return signed CDN URL
*/
function signUrl(url, keyName, keyValue, expirationDate) {
const urlObject = new URL(url);
urlObject.searchParams.set(
'Expires',
Math.floor(expirationDate.valueOf() / 1000).toString()
);
urlObject.searchParams.set('KeyName', keyName);

const signature = crypto
.createHmac('sha1', Buffer.from(keyValue, 'base64'))
.update(urlObject.href)
.digest('base64url');
urlObject.searchParams.set('Signature', signature);

return urlObject.href;
}
// [END nodejs_cdn_signed_urls]

module.exports = {
signUrl,
};
39 changes: 39 additions & 0 deletions cdn/signed-urls/test/signurl.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {signUrl} = require('../signurl');
const assert = require('node:assert/strict');

describe('signUrl', () => {
it('should return signed url with corresponding parameters', () => {
const url = new URL('https://cdn.example.com/test-path');
const keyName = 'test-key-name';
const keyValue = '0zY26LFZ2yAa3fERaKiKDQ==';
const expirationDate = new Date();

const resultUrl = new URL(
signUrl(url.href, keyName, keyValue, expirationDate)
);
assert.strictEqual(resultUrl.hostname, url.hostname);
assert.strictEqual(resultUrl.pathname, url.pathname);
assert.strictEqual(resultUrl.searchParams.get('KeyName'), keyName);
assert.strictEqual(
resultUrl.searchParams.get('Expires'),
Math.floor(expirationDate.valueOf() / 1000).toString()
);
assert.ok(resultUrl.searchParams.get('Signature'));
});
});