Skip to content

Commit 028ea3c

Browse files
committed
Change error for generating signed url.
1 parent 56b1a01 commit 028ea3c

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

gcloud/credentials.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,20 @@ def _get_signed_query_params(credentials, expiration, string_to_sign):
9595
:type string_to_sign: string
9696
:param string_to_sign: The string to be signed by the credentials.
9797
98+
:raises AttributeError: If :meth: sign_blob is unavailable.
99+
98100
:rtype: dict
99101
:returns: Query parameters matching the signing credentials with a
100102
signed payload.
101103
"""
104+
if not hasattr(credentials, 'sign_blob'):
105+
raise AttributeError('you need a private key to sign credentials.'
106+
'the credentials you are currently using %s '
107+
'just contains a token. see https://googlecloud'
108+
'platform.github.io/gcloud-python/stable/gcloud-'
109+
'auth.html#setting-up-a-service-account for more '
110+
'details.' % type(credentials))
111+
102112
_, signature_bytes = credentials.sign_blob(string_to_sign)
103113
signature = base64.b64encode(signature_bytes)
104114
service_account_name = credentials.service_account_email
@@ -115,6 +125,8 @@ def _get_expiration_seconds(expiration):
115125
:type expiration: int, long, datetime.datetime, datetime.timedelta
116126
:param expiration: When the signed URL should expire.
117127
128+
:raises TypeError: When expiration is not an integer.
129+
118130
:rtype: int
119131
:returns: a timestamp as an absolute number of seconds.
120132
"""

gcloud/test_credentials.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ def test_w_custom_fields(self):
101101
generation=generation)
102102

103103

104+
class Test_generate_signed_url_exception(unittest2.TestCase):
105+
def test_with_google_credentials(self):
106+
import time
107+
from gcloud.credentials import generate_signed_url
108+
RESOURCE = '/name/path'
109+
110+
credentials = _GoogleCredentials()
111+
expiration = int(time.time() + 5)
112+
self.assertRaises(AttributeError, generate_signed_url, credentials,
113+
resource=RESOURCE, expiration=expiration)
114+
115+
104116
class Test__get_signed_query_params(unittest2.TestCase):
105117

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

111123
def test_it(self):
112124
import base64
113-
from gcloud._testing import _Monkey
114-
from gcloud import credentials as MUT
115125

116126
SIG_BYTES = b'DEADBEEF'
117127
ACCOUNT_NAME = object()
@@ -226,6 +236,12 @@ def sign_blob(self, bytes_to_sign):
226236
return None, self._sign_result
227237

228238

239+
class _GoogleCredentials(object):
240+
241+
def __init__(self, service_account_email='testing@example.com'):
242+
self.service_account_email = service_account_email
243+
244+
229245
class _Client(object):
230246

231247
def __init__(self):

0 commit comments

Comments
 (0)