Skip to content

Commit f44212b

Browse files
authored
fix: correctly encode bytes for V2 signature (#382)
* fix: correctly encode bytes for V2 signature V2 signature was passing a string to the sign_bytes function instead of bytes. This works fine for most credentials (since their sign_bytes implementations accept strings) but not for impersonated credentials. V4 signature encodes the string before calling sign_bytes, so I do the same here. We should also look into clarifying the contract for the sign_bytes interface in the auth library. Fixes #373 * fix py2 failure
1 parent 79d27da commit f44212b

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

google/cloud/storage/_signing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def get_signed_query_params_v2(credentials, expiration, string_to_sign):
7777
signed payload.
7878
"""
7979
ensure_signed_credentials(credentials)
80-
signature_bytes = credentials.sign_bytes(string_to_sign)
80+
signature_bytes = credentials.sign_bytes(string_to_sign.encode("ascii"))
8181
signature = base64.b64encode(signature_bytes)
8282
service_account_name = credentials.signer_email
8383
return {

tests/unit/test__signing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def test_it(self):
255255
"Signature": base64.b64encode(sig_bytes),
256256
}
257257
self.assertEqual(result, expected)
258-
credentials.sign_bytes.assert_called_once_with(string_to_sign)
258+
credentials.sign_bytes.assert_called_once_with(string_to_sign.encode("ascii"))
259259

260260

261261
class Test_get_canonical_headers(unittest.TestCase):
@@ -420,7 +420,7 @@ def _generate_helper(
420420

421421
string_to_sign = "\n".join(elements)
422422

423-
credentials.sign_bytes.assert_called_once_with(string_to_sign)
423+
credentials.sign_bytes.assert_called_once_with(string_to_sign.encode("ascii"))
424424

425425
scheme, netloc, path, qs, frag = urllib_parse.urlsplit(url)
426426
expected_scheme, expected_netloc, _, _, _ = urllib_parse.urlsplit(

0 commit comments

Comments
 (0)