28
28
from google .auth import exceptions
29
29
from google .auth .transport import requests
30
30
from google .cloud import _helpers
31
+ from google .cloud .storage ._helpers import _DEFAULT_UNIVERSE_DOMAIN
31
32
from google .cloud .storage ._helpers import _NOW
32
33
from google .cloud .storage ._helpers import _UTC
34
+ from google .cloud .storage .retry import DEFAULT_RETRY
33
35
34
36
35
37
# `google.cloud.storage._signing.NOW` is deprecated.
@@ -271,6 +273,7 @@ def generate_signed_url_v2(
271
273
query_parameters = None ,
272
274
service_account_email = None ,
273
275
access_token = None ,
276
+ universe_domain = None ,
274
277
):
275
278
"""Generate a V2 signed URL to provide query-string auth'n to a resource.
276
279
@@ -384,7 +387,9 @@ def generate_signed_url_v2(
384
387
# See https://github.com/googleapis/google-cloud-python/issues/922
385
388
# Set the right query parameters.
386
389
if access_token and service_account_email :
387
- signature = _sign_message (string_to_sign , access_token , service_account_email )
390
+ signature = _sign_message (
391
+ string_to_sign , access_token , service_account_email , universe_domain
392
+ )
388
393
signed_query_params = {
389
394
"GoogleAccessId" : service_account_email ,
390
395
"Expires" : expiration_stamp ,
@@ -432,6 +437,7 @@ def generate_signed_url_v4(
432
437
query_parameters = None ,
433
438
service_account_email = None ,
434
439
access_token = None ,
440
+ universe_domain = None ,
435
441
_request_timestamp = None , # for testing only
436
442
):
437
443
"""Generate a V4 signed URL to provide query-string auth'n to a resource.
@@ -623,7 +629,9 @@ def generate_signed_url_v4(
623
629
string_to_sign = "\n " .join (string_elements )
624
630
625
631
if access_token and service_account_email :
626
- signature = _sign_message (string_to_sign , access_token , service_account_email )
632
+ signature = _sign_message (
633
+ string_to_sign , access_token , service_account_email , universe_domain
634
+ )
627
635
signature_bytes = base64 .b64decode (signature )
628
636
signature = binascii .hexlify (signature_bytes ).decode ("ascii" )
629
637
else :
@@ -647,7 +655,12 @@ def get_v4_now_dtstamps():
647
655
return timestamp , datestamp
648
656
649
657
650
- def _sign_message (message , access_token , service_account_email ):
658
+ def _sign_message (
659
+ message ,
660
+ access_token ,
661
+ service_account_email ,
662
+ universe_domain = _DEFAULT_UNIVERSE_DOMAIN ,
663
+ ):
651
664
"""Signs a message.
652
665
653
666
:type message: str
@@ -669,17 +682,22 @@ def _sign_message(message, access_token, service_account_email):
669
682
message = _helpers ._to_bytes (message )
670
683
671
684
method = "POST"
672
- url = "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/{}:signBlob?alt=json" .format (
673
- service_account_email
674
- )
685
+ url = f"https://iamcredentials.{ universe_domain } /v1/projects/-/serviceAccounts/{ service_account_email } :signBlob?alt=json"
675
686
headers = {
676
687
"Authorization" : "Bearer " + access_token ,
677
688
"Content-type" : "application/json" ,
678
689
}
679
690
body = json .dumps ({"payload" : base64 .b64encode (message ).decode ("utf-8" )})
680
-
681
691
request = requests .Request ()
682
- response = request (url = url , method = method , body = body , headers = headers )
692
+
693
+ def retriable_request ():
694
+ response = request (url = url , method = method , body = body , headers = headers )
695
+ return response
696
+
697
+ # Apply the default retry object to the signBlob call.
698
+ retry = DEFAULT_RETRY
699
+ call = retry (retriable_request )
700
+ response = call ()
683
701
684
702
if response .status != http .client .OK :
685
703
raise exceptions .TransportError (
0 commit comments