69
69
_DEFAULT_AWS_REGIONAL_CREDENTIAL_VERIFICATION_URL = (
70
70
"https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15"
71
71
)
72
+ # IMDSV2 session token lifetime. This is set to a low value because the session token is used immediately.
73
+ _IMDSV2_SESSION_TOKEN_TTL_SECONDS = "300"
72
74
73
75
74
76
class RequestSigner (object ):
@@ -476,9 +478,9 @@ def get_aws_region(self, context, request):
476
478
else response .data
477
479
)
478
480
479
- if response .status != 200 :
481
+ if response .status != http_client . OK :
480
482
raise exceptions .RefreshError (
481
- "Unable to retrieve AWS region" , response_body
483
+ "Unable to retrieve AWS region: {}" . format ( response_body )
482
484
)
483
485
484
486
# This endpoint will return the region in format: us-east-2b.
@@ -487,16 +489,19 @@ def get_aws_region(self, context, request):
487
489
488
490
def _get_imdsv2_session_token (self , request ):
489
491
if request is not None and self ._imdsv2_session_token_url is not None :
490
- headers = {"X-aws-ec2-metadata-token-ttl-seconds" : "300" }
492
+ headers = {
493
+ "X-aws-ec2-metadata-token-ttl-seconds" : _IMDSV2_SESSION_TOKEN_TTL_SECONDS
494
+ }
491
495
492
496
imdsv2_session_token_response = request (
493
497
url = self ._imdsv2_session_token_url , method = "PUT" , headers = headers
494
498
)
495
499
496
- if imdsv2_session_token_response .status != 200 :
500
+ if imdsv2_session_token_response .status != http_client . OK :
497
501
raise exceptions .RefreshError (
498
- "Unable to retrieve AWS Session Token" ,
499
- imdsv2_session_token_response .data ,
502
+ "Unable to retrieve AWS Session Token: {}" .format (
503
+ imdsv2_session_token_response .data
504
+ )
500
505
)
501
506
502
507
return imdsv2_session_token_response .data
@@ -545,7 +550,7 @@ def _get_metadata_security_credentials(
545
550
546
551
if response .status != http_client .OK :
547
552
raise exceptions .RefreshError (
548
- "Unable to retrieve AWS security credentials" , response_body
553
+ "Unable to retrieve AWS security credentials: {}" . format ( response_body )
549
554
)
550
555
551
556
credentials_response = json .loads (response_body )
@@ -593,7 +598,7 @@ def _get_metadata_role_name(self, request, imdsv2_session_token):
593
598
594
599
if response .status != http_client .OK :
595
600
raise exceptions .RefreshError (
596
- "Unable to retrieve AWS role name" , response_body
601
+ "Unable to retrieve AWS role name {}" . format ( response_body )
597
602
)
598
603
599
604
return response_body
@@ -690,7 +695,7 @@ def __init__(
690
695
"regional_cred_verification_url"
691
696
)
692
697
693
- # Get the environment ID. Currently, only one version supported (v1 ).
698
+ # Get the environment ID, i.e. "aws1". Currently, only one version supported (1 ).
694
699
matches = re .match (r"^(aws)([\d]+)$" , environment_id )
695
700
if matches :
696
701
env_id , env_version = matches .groups ()
@@ -701,7 +706,7 @@ def __init__(
701
706
raise exceptions .InvalidResource (
702
707
"No valid AWS 'credential_source' provided"
703
708
)
704
- elif int ( env_version or "" ) != 1 :
709
+ elif env_version is None or int ( env_version ) != 1 :
705
710
raise exceptions .InvalidValue (
706
711
"aws version '{}' is not supported in the current build." .format (
707
712
env_version
@@ -784,15 +789,12 @@ def retrieve_subject_token(self, request):
784
789
request_headers ["x-goog-cloud-target-resource" ] = self ._target_resource
785
790
786
791
# Serialize AWS signed request.
787
- # Keeping inner keys in sorted order makes testing easier for Python
788
- # versions <=3.5 as the stringified JSON string would have a predictable
789
- # key order.
790
792
aws_signed_req = {}
791
793
aws_signed_req ["url" ] = request_options .get ("url" )
792
794
aws_signed_req ["method" ] = request_options .get ("method" )
793
795
aws_signed_req ["headers" ] = []
794
796
# Reformat header to GCP STS expected format.
795
- for key in sorted ( request_headers .keys () ):
797
+ for key in request_headers .keys ():
796
798
aws_signed_req ["headers" ].append (
797
799
{"key" : key , "value" : request_headers [key ]}
798
800
)
0 commit comments