-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add cdn signed url sample #4011
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
Open
zabdalimov
wants to merge
6
commits into
GoogleCloudPlatform:main
Choose a base branch
from
zabdalimov:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9d3a5b4
feat: add cdn signed url sample
zabdalimov fbd048a
chore: add license headers to cdn signed urls snippet
zabdalimov ca4d386
Merge branch 'main' into main
glasnt 65e297d
Merge branch 'main' into main
glasnt 93a02ec
Apply suggestions from code review
glasnt 78f6186
Delete .github/workflows/cdn-signed-urls.yaml
glasnt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2018 Google LLC | ||
glasnt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// 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] | ||
glasnt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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] | ||
glasnt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
module.exports = { | ||
signUrl, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2018 Google LLC | ||
glasnt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// 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')); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is no longer required (will update)