|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2017 Google, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""This application demonstrates how to perform operations on data (content) |
| 18 | +when using Google Cloud CDN (Content Delivery Network). |
| 19 | +
|
| 20 | +For more information, see the README.md under /cdn and the documentation |
| 21 | +at https://cloud.google.com/cdn/docs. |
| 22 | +""" |
| 23 | + |
| 24 | +import argparse |
| 25 | +import base64 |
| 26 | +import datetime |
| 27 | +import hashlib |
| 28 | +import hmac |
| 29 | + |
| 30 | +from six.moves import urllib |
| 31 | + |
| 32 | + |
| 33 | +# [BEGIN sign_url] |
| 34 | +def sign_url(url, key_name, base64_key, expiration_time): |
| 35 | + """Gets the Signed URL string for the specified URL and configuration. |
| 36 | +
|
| 37 | + Args: |
| 38 | + url: URL to sign as a string. |
| 39 | + key_name: name of the signing key as a string. |
| 40 | + base64_key: signing key as a base64 encoded string. |
| 41 | + expiration_time: expiration time as a UTC datetime object. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + Returns the Signed URL appended with the query parameters based on the |
| 45 | + specified configuration. |
| 46 | + """ |
| 47 | + stripped_url = url.strip() |
| 48 | + parsed_url = urllib.parse.urlsplit(stripped_url) |
| 49 | + query_params = urllib.parse.parse_qs( |
| 50 | + parsed_url.query, keep_blank_values=True) |
| 51 | + epoch = datetime.datetime.utcfromtimestamp(0) |
| 52 | + expiration_timestamp = int((expiration_time - epoch).total_seconds()) |
| 53 | + decoded_key = base64.urlsafe_b64decode(base64_key) |
| 54 | + |
| 55 | + url_pattern = u'{url}{separator}Expires={expires}&KeyName={key_name}' |
| 56 | + |
| 57 | + url_to_sign = url_pattern.format( |
| 58 | + url=stripped_url, |
| 59 | + separator='&' if query_params else '?', |
| 60 | + expires=expiration_timestamp, |
| 61 | + key_name=key_name) |
| 62 | + |
| 63 | + digest = hmac.new( |
| 64 | + decoded_key, url_to_sign.encode('utf-8'), hashlib.sha1).digest() |
| 65 | + signature = base64.urlsafe_b64encode(digest).decode('utf-8') |
| 66 | + |
| 67 | + signed_url = u'{url}&Signature={signature}'.format( |
| 68 | + url=url_to_sign, signature=signature) |
| 69 | + |
| 70 | + print(signed_url) |
| 71 | +# [END sign_url] |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + parser = argparse.ArgumentParser( |
| 76 | + description=__doc__, |
| 77 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 78 | + |
| 79 | + subparsers = parser.add_subparsers(dest='command') |
| 80 | + |
| 81 | + sign_url_parser = subparsers.add_parser( |
| 82 | + 'sign-url', |
| 83 | + help="Sign a URL to grant temporary authorized access.") |
| 84 | + sign_url_parser.add_argument( |
| 85 | + 'url', help='The URL to sign.') |
| 86 | + sign_url_parser.add_argument( |
| 87 | + 'key_name', |
| 88 | + help='Key name for the signing key.') |
| 89 | + sign_url_parser.add_argument( |
| 90 | + 'base64_key', |
| 91 | + help='The base64 encoded signing key.') |
| 92 | + sign_url_parser.add_argument( |
| 93 | + 'expiration_time', |
| 94 | + type=lambda d: datetime.datetime.utcfromtimestamp(float(d)), |
| 95 | + help='Expiration time expessed as seconds since the epoch.') |
| 96 | + |
| 97 | + args = parser.parse_args() |
| 98 | + |
| 99 | + if args.command == 'sign-url': |
| 100 | + sign_url( |
| 101 | + args.url, args.key_name, args.base64_key, args.expiration_time) |
0 commit comments