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

Change error for generating signed URL #1966

Merged
Merged
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
12 changes: 12 additions & 0 deletions gcloud/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,20 @@ def _get_signed_query_params(credentials, expiration, string_to_sign):
:type string_to_sign: string
:param string_to_sign: The string to be signed by the credentials.

:raises AttributeError: If :meth: sign_blob is unavailable.

:rtype: dict
:returns: Query parameters matching the signing credentials with a
signed payload.
"""
if not hasattr(credentials, 'sign_blob'):
raise AttributeError('you need a private key to sign credentials.'
'the credentials you are currently using %s '
'just contains a token. see https://googlecloud'
'platform.github.io/gcloud-python/stable/gcloud-'
'auth.html#setting-up-a-service-account for more '
'details.' % type(credentials))

_, signature_bytes = credentials.sign_blob(string_to_sign)
signature = base64.b64encode(signature_bytes)
service_account_name = credentials.service_account_email
Expand All @@ -115,6 +125,8 @@ def _get_expiration_seconds(expiration):
:type expiration: int, long, datetime.datetime, datetime.timedelta
:param expiration: When the signed URL should expire.

:raises TypeError: When expiration is not an integer.

:rtype: int
:returns: a timestamp as an absolute number of seconds.
"""
Expand Down
20 changes: 18 additions & 2 deletions gcloud/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ def test_w_custom_fields(self):
generation=generation)


class Test_generate_signed_url_exception(unittest2.TestCase):
def test_with_google_credentials(self):
import time
from gcloud.credentials import generate_signed_url
RESOURCE = '/name/path'

credentials = _GoogleCredentials()
expiration = int(time.time() + 5)
self.assertRaises(AttributeError, generate_signed_url, credentials,
resource=RESOURCE, expiration=expiration)


class Test__get_signed_query_params(unittest2.TestCase):

def _callFUT(self, credentials, expiration, string_to_sign):
Expand All @@ -110,8 +122,6 @@ def _callFUT(self, credentials, expiration, string_to_sign):

def test_it(self):
import base64
from gcloud._testing import _Monkey
from gcloud import credentials as MUT

SIG_BYTES = b'DEADBEEF'
ACCOUNT_NAME = object()
Expand Down Expand Up @@ -226,6 +236,12 @@ def sign_blob(self, bytes_to_sign):
return None, self._sign_result


class _GoogleCredentials(object):

def __init__(self, service_account_email='testing@example.com'):
self.service_account_email = service_account_email


class _Client(object):

def __init__(self):
Expand Down