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

CDN samples dir with snippet for signing urls #1383

Merged
merged 13 commits into from
Mar 8, 2018

Conversation

djmailhot
Copy link
Contributor

Add a new directory for CDN.

Include a snippet for signing urls, as specified by the Cloud Storage documentation at https://cloud.google.com/storage/docs/access-control/create-signed-urls-program

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Mar 5, 2018
@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for the commit author(s). If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again. If the bot doesn't comment, it means it doesn't think anything has changed.

@googlebot googlebot added cla: no This human has *not* signed the Contributor License Agreement. and removed cla: yes This human has signed the Contributor License Agreement. labels Mar 5, 2018
cdn/snippets.py Outdated

import argparse
import base64
<<<<<<< HEAD
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like merge conflict indicator is a leftover here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

cdn/snippets.py Outdated

args = parser.parse_args()

args.url = "http://35.186.234.33/index.html"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete these 3 to not to overwrite the args?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

cdn/snippets.py Outdated
hmac.new(key, url_to_sign, hashlib.sha1).digest())

return '{url}&Signature={signature}'.format(
url=url_to_sign, signature=signature)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you put a region tag from the beginning of imports to the end of def sign_url (so that we don't include what's below in the documentation)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@ahmetb
Copy link
Contributor

ahmetb commented Mar 5, 2018

Sample flow and algorithm LGTM. Needs a Python approver.

@ahmetb ahmetb requested a review from theacodes March 5, 2018 22:49
Copy link
Contributor

@theacodes theacodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no tests, not even a sanity test that just imports this. All samples must have tests.

cdn/snippets.py Outdated
import datetime
import hashlib
import hmac
import urlparse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'll need to use six in order to get python 2/3 compatibility.

cdn/snippets.py Outdated
expiration_timestamp = int((expiration_datetime - epoch).total_seconds())

url_to_sign = '{url}{separator}Expires={expires}&KeyName={key_name}'.format(
url=stripped_url,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indented way too far. Indents in python are 4 spaces with a single indent level for continuations.

cdn/snippets.py Outdated
args = parser.parse_args()

if args.command == 'sign-url':
expiration_datetime = datetime.datetime.utcfromtimestamp(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't put any logic here, move this into the sample.

Copy link
Contributor

@ahmetb ahmetb Mar 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this part is correct (and is coherent with other language samples). We want sign_url to accept a language-native date type (in this case datetime).

So here, we're converting Unix epoch CLI arg to datetime to pass to sign_url. Most people would be integrating sign_url to their programs, and not this part. Hence, we should probably keep it as is.

cdn/snippets.py Outdated

Returns:
Returns the Signed URL appended with the query parameters based on the
specified configuration. Roughly of the form:
Copy link
Contributor

@ahmetb ahmetb Mar 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Roughly of the form: {url}{separator}Expires={expiration}&KeyName={key_name}&Signature={signature}

seems unnecessary. it's kind of implicit from the docs, gcloud and the way feature works in general.

@djmailhot
Copy link
Contributor Author

nox > Ran multiple sessions:
nox > * lint(sample='./cdn'): success
nox > * py27(sample='./cdn'): success
nox > * py36(sample='./cdn'): success
nox > * readmegen(sample='./cdn'): success

BOOM

@ahmetb
Copy link
Contributor

ahmetb commented Mar 8, 2018

CLA bot is glitchy nowadays. This is likely ready to merge.

@theacodes theacodes merged commit ee0ee22 into GoogleCloudPlatform:master Mar 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: no This human has *not* signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants