8000 Change error for generating signed url. · googleapis/google-cloud-python@6c00bf2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6c00bf2

Browse files
committed
Change error for generating signed url.
1 parent 771bc7a commit 6c00bf2

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

gcloud/credentials.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ def _get_signed_query_params(credentials, expiration, string_to_sign):
9999
:returns: Query parameters matching the signing credentials with a
100100
signed payload.
101101
"""
102+
if not hasattr(credentials, 'sign_blob'):
103+
raise AttributeError('you need a private key to sign credentials.'
104+
'the credentials you are currently using %s '
105+
'just contains a token. see https://googlecloud'
106+
'platform.github.io/gcloud-python/stable/gcloud-'
107+
'auth.html#setting-up-a-service-account for more '
108+
'details.' % type(credentials))
109+
102110
_, signature_bytes = credentials.sign_blob(string_to_sign)
103111
signature = base64.b64encode(signature_bytes)
104112
service_account_name = credentials.service_account_email

gcloud/test_credentials.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _callFUT(self, *args, **kwargs):
4040
return generate_signed_url(*args, **kwargs)
4141

4242
def _generate_helper(self, response_type=None, response_disposition=None,
43-
generation=None):
43+
generation=None, credentials=None):
4444
import base64
4545
from six.moves.urllib.parse import parse_qs
4646
from six.moves.urllib.parse import urlsplit
@@ -50,7 +50,7 @@ def _generate_helper(self, response_type=None, response_disposition=None,
5050
ENDPOINT = 'http://api.example.com'
5151
RESOURCE = '/name/path'
5252
SIGNED = base64.b64encode(b'DEADBEEF')
53-
CREDENTIALS = _Credentials()
53+
CREDENTIALS = credentials or _Credentials()
5454

5555
def _get_signed_query_params(*args):
5656
credentials, expiration = args[:2]
@@ -90,15 +90,19 @@ def _get_signed_query_params(*args):
9090
self.assertEqual(frag, '')
9191

9292
def test_w_expiration_int(self):
93-
self._generate_helper()
93+
self._generate_helper(credentials=_Credentials())
94+
95+
def test_w_google_credentials(self):
96+
self._generate_helper(credentials=_GoogleCredentials())
9497

9598
def test_w_custom_fields(self):
9699
response_type = 'text/plain'
97100
response_disposition = 'attachment; filename=blob.png'
98101
generation = '123'
99102
self._generate_helper(response_type=response_type,
100103
response_disposition=response_disposition,
101-
generation=generation)
104+
generation=generation,
105+
credentials=_Credentials())
102106

103107

104108
class Test__get_signed_query_params(unittest2.TestCase):
@@ -226,6 +230,12 @@ def sign_blob(self, bytes_to_sign):
226230
return None, self._sign_result
227231

228232

233+
class _GoogleCredentials(object):
234+
235+
def __init__(self, service_account_email='testing@example.com'):
236+
self.service_account_email = service_account_email
237+
238+
229239
class _Client(object):
230240

231241
def __init__(self):

0 commit comments

Comments
 (0)
0